Paste view #2

Merged
ada merged 3 commits from view-paste into main 2023-10-10 19:10:16 +02:00
2 changed files with 25 additions and 4 deletions
Showing only changes of commit 4663925d5d - Show all commits

19
main.go
View file

@ -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
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>