2023-10-02 02:45:41 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-10-03 20:41:03 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
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
|
|
|
|
|
|
|
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
2023-10-04 00:10:29 +02:00
|
|
|
path := strings.ReplaceAll(r.URL.Path, "/raw", "")
|
2023-10-03 20:41:03 +02:00
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
if path == "/" {
|
|
|
|
http.ServeFile(w, r, "./static/index.html")
|
2023-10-02 19:22:56 +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 {
|
|
|
|
if urlExist(path) {
|
2023-10-04 00:10:29 +02:00
|
|
|
pasteContent := getContent(path)
|
|
|
|
fmt.Println(pasteContent)
|
|
|
|
if strings.HasSuffix("/raw", path) {
|
|
|
|
io.WriteString(w, pasteContent)
|
|
|
|
} else {
|
|
|
|
io.WriteString(w, pasteContent)
|
|
|
|
}
|
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-02 19:22:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-02 02:45:41 +02:00
|
|
|
func main() {
|
2023-10-02 19:22:56 +02:00
|
|
|
currentConfig = setConfig()
|
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-02 15:03:04 +02:00
|
|
|
err := http.ListenAndServe(listen, nil)
|
2023-10-02 02:45:41 +02:00
|
|
|
if err != nil {
|
2023-10-03 20:41:03 +02:00
|
|
|
fmt.Println(err)
|
2023-10-02 02:45:41 +02:00
|
|
|
}
|
|
|
|
}
|