add(backend): add url to have raw paste

This commit is contained in:
Ada 2023-10-04 00:10:29 +02:00
parent 7d45a1792b
commit bbf0967a46
2 changed files with 16 additions and 4 deletions

10
db.go
View file

@ -28,9 +28,15 @@ func insertPaste(key string, content string, secret string, ttl time.Duration) {
content: content,
secret: secret,
}
connectDB().HSet(ctx, key, "content", hash.content)
connectDB().HSet(ctx, key, "secret", hash.secret)
db := connectDB()
db.HSet(ctx, key, "content", hash.content)
db.HSet(ctx, key, "secret", hash.secret)
if ttl > -1 {
connectDB().Do(ctx, key, ttl)
}
}
func getContent(key string) string {
db := connectDB()
return db.HGet(ctx, key, "content").Val()
}

10
main.go
View file

@ -10,7 +10,7 @@ import (
var currentConfig config
func handleRequest(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
path := strings.ReplaceAll(r.URL.Path, "/raw", "")
switch r.Method {
case "GET":
if path == "/" {
@ -21,7 +21,13 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
http.Handle("/static/", http.StripPrefix("/static/", fs))
} else {
if urlExist(path) {
io.WriteString(w, "exist")
pasteContent := getContent(path)
fmt.Println(pasteContent)
if strings.HasSuffix("/raw", path) {
io.WriteString(w, pasteContent)
} else {
io.WriteString(w, pasteContent)
}
} else {
w.WriteHeader(http.StatusNotFound)
}