chore: better path joining

This commit is contained in:
Guanran Wang 2024-08-30 06:36:57 +08:00
parent ab47d5c35a
commit 4b2fd8dd04
Signed by: nyancat
GPG key ID: 91F97D9ED12639CF

13
main.go
View file

@ -5,7 +5,9 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"net/url"
"os" "os"
"path"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -46,15 +48,20 @@ or: cmd | curl --data-binary @- %s
log.Fatalln(err) log.Fatalln(err)
} }
os.WriteFile("db/"+uuid.String(), body, 0644) os.WriteFile(path.Join("db", uuid.String()), body, 0644)
fmt.Fprintf(w, hostURL+uuid.String()) url, err := url.JoinPath(hostURL, uuid.String())
if err != nil {
log.Fatalln(err)
}
fmt.Fprintf(w, url)
log.Printf("paste created: uuid=%s", uuid.String()) log.Printf("paste created: uuid=%s", uuid.String())
default: default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
} }
}) })
http.Handle("/", http.FileServer(http.Dir("db"))) http.Handle("/", http.FileServer(http.Dir(databasePath)))
log.Printf("Starting server on %s", listenAddr)
log.Fatal(http.ListenAndServe(listenAddr, nil)) log.Fatal(http.ListenAndServe(listenAddr, nil))
} }