Paste view #2
1 changed files with 17 additions and 5 deletions
22
main.go
22
main.go
|
@ -11,19 +11,31 @@ import (
|
||||||
var currentConfig config
|
var currentConfig config
|
||||||
|
|
||||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
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 {
|
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(path) {
|
if urlExist(clearPath) {
|
||||||
_, err := io.WriteString(w, "This plak exists")
|
if strings.HasSuffix(path, "/raw") {
|
||||||
if err != nil {
|
content := db.HGet(ctx, clearPath, "content").Val()
|
||||||
return
|
_, err := io.WriteString(w, content)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
content := db.HGet(ctx, path, "content").Val()
|
||||||
|
_, err := io.WriteString(w, content)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
|
Loading…
Reference in a new issue