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, content: content,
secret: secret, secret: secret,
} }
connectDB().HSet(ctx, key, "content", hash.content) db := connectDB()
connectDB().HSet(ctx, key, "secret", hash.secret) db.HSet(ctx, key, "content", hash.content)
db.HSet(ctx, key, "secret", hash.secret)
if ttl > -1 { if ttl > -1 {
connectDB().Do(ctx, key, ttl) 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 var currentConfig config
func handleRequest(w http.ResponseWriter, r *http.Request) { func handleRequest(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := strings.ReplaceAll(r.URL.Path, "/raw", "")
switch r.Method { switch r.Method {
case "GET": case "GET":
if path == "/" { if path == "/" {
@ -21,7 +21,13 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
http.Handle("/static/", http.StripPrefix("/static/", fs)) http.Handle("/static/", http.StripPrefix("/static/", fs))
} else { } else {
if urlExist(path) { 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 { } else {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} }