Compare commits
No commits in common. "6567a7c0cd3fbe3f28da8a383dfe2bce4b983c1b" and "8dc45aeb01d3a0bbfd1da293894647027b7d5a17" have entirely different histories.
6567a7c0cd
...
8dc45aeb01
3 changed files with 17 additions and 33 deletions
26
db.go
26
db.go
|
@ -3,20 +3,19 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ctx = context.Background()
|
var ctx = context.Background()
|
||||||
|
|
||||||
func connectDB() *redis.Client {
|
func connectDB() *redis.Client {
|
||||||
localDb := redis.NewClient(&redis.Options{
|
db := redis.NewClient(&redis.Options{
|
||||||
Addr: currentConfig.redisAddr,
|
Addr: currentConfig.redisAddr,
|
||||||
Username: currentConfig.redisUser,
|
Username: currentConfig.redisUser,
|
||||||
Password: currentConfig.redisPassword,
|
Password: currentConfig.redisPassword,
|
||||||
DB: currentConfig.redisDB,
|
DB: currentConfig.redisDB,
|
||||||
})
|
})
|
||||||
return localDb
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertPaste(key string, content string, secret string, ttl time.Duration) {
|
func insertPaste(key string, content string, secret string, ttl time.Duration) {
|
||||||
|
@ -29,24 +28,15 @@ func insertPaste(key string, content string, secret string, ttl time.Duration) {
|
||||||
content: content,
|
content: content,
|
||||||
secret: secret,
|
secret: secret,
|
||||||
}
|
}
|
||||||
err := db.HSet(ctx, key, "content", hash.content)
|
db := connectDB()
|
||||||
if err != nil {
|
db.HSet(ctx, key, "content", hash.content)
|
||||||
log.Println(err)
|
db.HSet(ctx, key, "secret", hash.secret)
|
||||||
}
|
|
||||||
err = db.HSet(ctx, key, "secret", hash.secret)
|
|
||||||
if ttl > -1 {
|
if ttl > -1 {
|
||||||
db.Do(ctx, key, ttl)
|
connectDB().Do(ctx, key, ttl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getContent(key string) string {
|
func getContent(key string) string {
|
||||||
content := db.HGet(ctx, key, "content").Val()
|
db := connectDB()
|
||||||
return content
|
return db.HGet(ctx, key, "content").Val()
|
||||||
}
|
|
||||||
|
|
||||||
func deleteContent(key string) {
|
|
||||||
err := db.Del(ctx, key)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
15
main.go
15
main.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
@ -11,7 +10,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var currentConfig config
|
var currentConfig config
|
||||||
var db *redis.Client
|
|
||||||
|
|
||||||
type pasteView struct {
|
type pasteView struct {
|
||||||
Content string
|
Content string
|
||||||
|
@ -21,6 +19,7 @@ type pasteView struct {
|
||||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "")
|
clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "")
|
||||||
|
db := connectDB()
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
if path == "/" {
|
if path == "/" {
|
||||||
|
@ -32,14 +31,14 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
if urlExist(clearPath) {
|
if urlExist(clearPath) {
|
||||||
if strings.HasSuffix(path, "/raw") {
|
if strings.HasSuffix(path, "/raw") {
|
||||||
pasteContent := getContent(clearPath)
|
pasteContent := db.HGet(ctx, clearPath, "content").Val()
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
_, err := io.WriteString(w, pasteContent)
|
_, err := io.WriteString(w, pasteContent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pasteContent := getContent(path)
|
pasteContent := db.HGet(ctx, path, "content").Val()
|
||||||
s := pasteView{Content: pasteContent, Key: strings.TrimPrefix(path, "/")}
|
s := pasteView{Content: pasteContent, Key: strings.TrimPrefix(path, "/")}
|
||||||
t, err := template.ParseFiles("templates/paste.html")
|
t, err := template.ParseFiles("templates/paste.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,8 +68,11 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
urlItem := strings.Split(path, "/")
|
urlItem := strings.Split(path, "/")
|
||||||
if urlExist("/" + urlItem[2]) {
|
if urlExist("/" + urlItem[2]) {
|
||||||
secret := r.URL.Query().Get("secret")
|
secret := r.URL.Query().Get("secret")
|
||||||
if verifySecret("/"+urlItem[2], secret) {
|
if secret == db.HGet(ctx, "/"+urlItem[2], "secret").Val() {
|
||||||
deleteContent("/" + urlItem[2])
|
err := db.Del(ctx, "/"+urlItem[2])
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
@ -85,7 +87,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db = connectDB()
|
|
||||||
currentConfig = getConfig()
|
currentConfig = getConfig()
|
||||||
listen := currentConfig.host + ":" + currentConfig.port
|
listen := currentConfig.host + ":" + currentConfig.port
|
||||||
http.HandleFunc("/", handleRequest)
|
http.HandleFunc("/", handleRequest)
|
||||||
|
|
9
utils.go
9
utils.go
|
@ -28,13 +28,6 @@ func generateSecret() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlExist(url string) bool {
|
func urlExist(url string) bool {
|
||||||
exist := db.Exists(ctx, url).Val()
|
exist := connectDB().Exists(ctx, url).Val()
|
||||||
return exist == 1
|
return exist == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifySecret(url string, secret string) bool {
|
|
||||||
if secret == db.HGet(ctx, url, "secret").Val() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue