From bbf0967a46360da0d369222b72dd952576d8a756 Mon Sep 17 00:00:00 2001 From: Ada Date: Wed, 4 Oct 2023 00:10:29 +0200 Subject: [PATCH] add(backend): add url to have raw paste --- db.go | 10 ++++++++-- main.go | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/db.go b/db.go index 149fda1..e2a7cbd 100644 --- a/db.go +++ b/db.go @@ -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() +} diff --git a/main.go b/main.go index fb83c5e..96dc5aa 100644 --- a/main.go +++ b/main.go @@ -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) }