2023-10-02 02:45:41 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-10-03 20:41:03 +02:00
|
|
|
"fmt"
|
2023-10-07 19:53:55 +02:00
|
|
|
"html/template"
|
2023-10-03 20:41:03 +02:00
|
|
|
"io"
|
2023-10-05 23:18:02 +02:00
|
|
|
"log"
|
2023-10-02 02:45:41 +02:00
|
|
|
"net/http"
|
2023-10-02 19:22:56 +02:00
|
|
|
"strings"
|
2023-10-02 02:45:41 +02:00
|
|
|
)
|
|
|
|
|
2023-10-02 19:22:56 +02:00
|
|
|
var currentConfig config
|
|
|
|
|
2023-10-07 19:53:55 +02:00
|
|
|
type pasteView struct {
|
|
|
|
Content string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
2023-10-02 19:22:56 +02:00
|
|
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
2023-10-06 00:16:55 +02:00
|
|
|
path := r.URL.Path
|
|
|
|
clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "")
|
|
|
|
db := connectDB()
|
2023-10-03 20:41:03 +02:00
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
if path == "/" {
|
|
|
|
http.ServeFile(w, r, "./static/index.html")
|
2023-10-06 00:16:55 +02:00
|
|
|
|
2023-10-03 20:41:03 +02:00
|
|
|
} else if strings.HasPrefix(path, "/static/") {
|
|
|
|
fs := http.FileServer(http.Dir("./static"))
|
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
|
|
|
} else {
|
2023-10-06 00:16:55 +02:00
|
|
|
if urlExist(clearPath) {
|
|
|
|
if strings.HasSuffix(path, "/raw") {
|
2023-10-07 19:53:55 +02:00
|
|
|
pasteContent := db.HGet(ctx, clearPath, "content").Val()
|
2023-10-12 21:46:30 +02:00
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
2023-10-07 19:53:55 +02:00
|
|
|
_, err := io.WriteString(w, pasteContent)
|
2023-10-06 00:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-07 19:53:55 +02:00
|
|
|
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)
|
2023-10-06 00:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2023-10-04 01:49:14 +02:00
|
|
|
}
|
2023-10-03 20:41:03 +02:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "POST":
|
|
|
|
if path == "/" {
|
|
|
|
secret := generateSecret()
|
|
|
|
url := "/" + generateUrl()
|
|
|
|
content := r.FormValue("content")
|
|
|
|
insertPaste(url, content, secret, -1)
|
|
|
|
http.Redirect(w, r, url, http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
}
|
2023-10-12 21:46:30 +02:00
|
|
|
case "DELETE":
|
|
|
|
if strings.HasPrefix(path, "/delete") {
|
|
|
|
urlItem := strings.Split(path, "/")
|
|
|
|
if urlExist("/" + urlItem[2]) {
|
|
|
|
secret := r.URL.Query().Get("secret")
|
|
|
|
if secret == db.HGet(ctx, "/"+urlItem[2], "secret").Val() {
|
|
|
|
err := db.Del(ctx, "/"+urlItem[2])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
}
|
2023-10-02 19:22:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 02:45:41 +02:00
|
|
|
func main() {
|
2023-10-05 23:18:02 +02:00
|
|
|
currentConfig = getConfig()
|
2023-10-02 15:03:04 +02:00
|
|
|
listen := currentConfig.host + ":" + currentConfig.port
|
2023-10-02 19:22:56 +02:00
|
|
|
http.HandleFunc("/", handleRequest)
|
2023-10-02 02:45:41 +02:00
|
|
|
|
2023-10-05 23:18:02 +02:00
|
|
|
if currentConfig.host == "" {
|
|
|
|
fmt.Println("Listening on port " + listen)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Listening on " + listen)
|
|
|
|
}
|
|
|
|
|
2023-10-02 15:03:04 +02:00
|
|
|
err := http.ListenAndServe(listen, nil)
|
2023-10-02 02:45:41 +02:00
|
|
|
if err != nil {
|
2023-10-05 23:18:02 +02:00
|
|
|
log.Fatal(err)
|
2023-10-02 02:45:41 +02:00
|
|
|
}
|
|
|
|
}
|