From 984c1cf3b654961e729653b3886f64aa3b70728d Mon Sep 17 00:00:00 2001 From: Ada Date: Fri, 6 Oct 2023 00:16:55 +0200 Subject: [PATCH 1/3] add(backend): paste view --- main.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index d165827..a6bb946 100644 --- a/main.go +++ b/main.go @@ -11,19 +11,31 @@ import ( var currentConfig config func handleRequest(w http.ResponseWriter, r *http.Request) { - path := strings.ReplaceAll(r.URL.Path, "/raw", "") + path := r.URL.Path + clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "") + db := connectDB() switch r.Method { case "GET": if path == "/" { http.ServeFile(w, r, "./static/index.html") + } else if strings.HasPrefix(path, "/static/") { fs := http.FileServer(http.Dir("./static")) http.Handle("/static/", http.StripPrefix("/static/", fs)) } else { - if urlExist(path) { - _, err := io.WriteString(w, "This plak exists") - if err != nil { - return + if urlExist(clearPath) { + if strings.HasSuffix(path, "/raw") { + content := db.HGet(ctx, clearPath, "content").Val() + _, err := io.WriteString(w, content) + if err != nil { + log.Println(err) + } + } else { + content := db.HGet(ctx, path, "content").Val() + _, err := io.WriteString(w, content) + if err != nil { + log.Println(err) + } } } else { w.WriteHeader(http.StatusNotFound) -- 2.45.2 From 4663925d5de27dd983fe691eb2a45704ceaa74a7 Mon Sep 17 00:00:00 2001 From: Ada Date: Sat, 7 Oct 2023 19:53:55 +0200 Subject: [PATCH 2/3] add(backend): html templates render paste view --- main.go | 19 +++++++++++++++---- templates/paste.html | 10 ++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 templates/paste.html diff --git a/main.go b/main.go index a6bb946..eda667a 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "html/template" "io" "log" "net/http" @@ -10,6 +11,11 @@ import ( var currentConfig config +type pasteView struct { + Content string + Key string +} + func handleRequest(w http.ResponseWriter, r *http.Request) { path := r.URL.Path clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "") @@ -25,14 +31,19 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { } else { if urlExist(clearPath) { if strings.HasSuffix(path, "/raw") { - content := db.HGet(ctx, clearPath, "content").Val() - _, err := io.WriteString(w, content) + pasteContent := db.HGet(ctx, clearPath, "content").Val() + _, err := io.WriteString(w, pasteContent) if err != nil { log.Println(err) } } else { - content := db.HGet(ctx, path, "content").Val() - _, err := io.WriteString(w, content) + pasteContent := db.HGet(ctx, path, "content").Val() + s := pasteView{Content: pasteContent, Key: strings.TrimPrefix(path, "/")} + t, err := template.ParseFiles("templates/paste.html") + if err != nil { + log.Println(err) + } + err = t.Execute(w, s) if err != nil { log.Println(err) } diff --git a/templates/paste.html b/templates/paste.html new file mode 100644 index 0000000..7412d47 --- /dev/null +++ b/templates/paste.html @@ -0,0 +1,10 @@ + + + + + {{.Key}} + + +
{{.Content}}
+ + \ No newline at end of file -- 2.45.2 From b10ac09fcdd660d3501813c80b1fae79cf6778f7 Mon Sep 17 00:00:00 2001 From: Ada Date: Tue, 10 Oct 2023 18:20:11 +0200 Subject: [PATCH 3/3] changes(front): Use css for paste view --- templates/paste.html | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/templates/paste.html b/templates/paste.html index 7412d47..ad86f72 100644 --- a/templates/paste.html +++ b/templates/paste.html @@ -1,8 +1,13 @@ - {{.Key}} + + + + Plak {{.Key}}• Plakken +
{{.Content}}
-- 2.45.2