add(backend): html templates render paste view

This commit is contained in:
Ada 2023-10-07 19:53:55 +02:00
parent 984c1cf3b6
commit 4663925d5d
2 changed files with 25 additions and 4 deletions

19
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"html/template"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -10,6 +11,11 @@ import (
var currentConfig config var currentConfig config
type pasteView struct {
Content string
Key string
}
func handleRequest(w http.ResponseWriter, r *http.Request) { func handleRequest(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "") clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "")
@ -25,14 +31,19 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
} else { } else {
if urlExist(clearPath) { if urlExist(clearPath) {
if strings.HasSuffix(path, "/raw") { if strings.HasSuffix(path, "/raw") {
content := db.HGet(ctx, clearPath, "content").Val() pasteContent := db.HGet(ctx, clearPath, "content").Val()
_, err := io.WriteString(w, content) _, err := io.WriteString(w, pasteContent)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
} else { } else {
content := db.HGet(ctx, path, "content").Val() pasteContent := db.HGet(ctx, path, "content").Val()
_, err := io.WriteString(w, content) 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 { if err != nil {
log.Println(err) log.Println(err)
} }

10
templates/paste.html Normal file
View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{.Key}}</title>
</head>
<body>
<pre><code>{{.Content}}</code></pre>
</body>
</html>