Compare commits
No commits in common. "994d7054bb80ab5a3fabcb06ce55f6566ccc7f12" and "2d84849f3e7b848eba23d39aee86c1c861a3aa42" have entirely different histories.
994d7054bb
...
2d84849f3e
2 changed files with 5 additions and 43 deletions
33
main.go
33
main.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -11,42 +10,20 @@ 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 := strings.ReplaceAll(r.URL.Path, "/raw", "")
|
||||||
clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "")
|
|
||||||
db := connectDB()
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
if path == "/" {
|
if path == "/" {
|
||||||
http.ServeFile(w, r, "./static/index.html")
|
http.ServeFile(w, r, "./static/index.html")
|
||||||
|
|
||||||
} else if strings.HasPrefix(path, "/static/") {
|
} else if strings.HasPrefix(path, "/static/") {
|
||||||
fs := http.FileServer(http.Dir("./static"))
|
fs := http.FileServer(http.Dir("./static"))
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||||
} else {
|
} else {
|
||||||
if urlExist(clearPath) {
|
if urlExist(path) {
|
||||||
if strings.HasSuffix(path, "/raw") {
|
_, err := io.WriteString(w, "This plak exists")
|
||||||
pasteContent := db.HGet(ctx, clearPath, "content").Val()
|
if err != nil {
|
||||||
_, err := io.WriteString(w, pasteContent)
|
return
|
||||||
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 {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
<!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