From b3c6c358334edfe3e6c4357e7624f2be6907b5eb Mon Sep 17 00:00:00 2001 From: Ada Date: Sat, 7 Oct 2023 19:53:55 +0200 Subject: [PATCH] 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