add(backend): html templates render paste view
This commit is contained in:
parent
46c866df5a
commit
b3c6c35833
2 changed files with 25 additions and 4 deletions
19
main.go
19
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)
|
||||
}
|
||||
|
|
10
templates/paste.html
Normal file
10
templates/paste.html
Normal 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>
|
Loading…
Reference in a new issue