Paste view #2
2 changed files with 43 additions and 5 deletions
33
main.go
33
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -10,20 +11,42 @@ import (
|
|||
|
||||
var currentConfig config
|
||||
|
||||
type pasteView struct {
|
||||
Content string
|
||||
Key string
|
||||
}
|
||||
|
||||
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") {
|
||||
pasteContent := db.HGet(ctx, clearPath, "content").Val()
|
||||
_, err := io.WriteString(w, pasteContent)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
|
|
15
templates/paste.html
Normal file
15
templates/paste.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>{{.Key}}</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, user-scalable=no, initial-scale=1.0"
|
||||
name="viewport">
|
||||
<meta content="ie=edge" http-equiv="X-UA-Compatible">
|
||||
<title>Plak {{.Key}}• Plakken</title>
|
||||
<link href="/static/style.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<pre><code>{{.Content}}</code></pre>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue