minor fixes in webserver

This commit is contained in:
Djeeberjr 2022-06-01 15:02:52 +02:00
parent 22946b9dfd
commit 2be2cc0626
2 changed files with 14 additions and 7 deletions

View File

@ -23,7 +23,7 @@ func Start(config *types.AppConfig) {
client := client.NewClient(db, s3Client) client := client.NewClient(db, s3Client)
err = web.StartWebserver(config.Address, *client, config.APIUsername, config.APIPassword) err = web.StartWebserver(config.Address, client, config.APIUsername, config.APIPassword)
if err != nil { if err != nil {
logrus.Fatal(err.Error()) logrus.Fatal(err.Error())
} }

View File

@ -17,11 +17,17 @@ type createShare struct {
Key string `json:"key"` Key string `json:"key"`
} }
func StartWebserver(addr string, client client.Client, username, password string) error { func StartWebserver(addr string, client *client.Client, username, password string) error {
if username == "" || password == "" { if username == "" || password == "" {
return errors.New("API username and password must be set") return errors.New("API username and password must be set")
} }
r := CreateRouter(client, username, password)
return http.ListenAndServe(addr, r)
}
func CreateRouter(client *client.Client, username, password string) *mux.Router {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) {
@ -117,9 +123,9 @@ func StartWebserver(addr string, client client.Client, username, password string
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(share) json.NewEncoder(w).Encode(share)
}).Methods("POST") }).Methods("POST", "PUT")
r.HandleFunc("/api/share/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/api/share/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) {
if !checkAuth(w, r, username, password) { if !checkAuth(w, r, username, password) {
@ -134,15 +140,16 @@ func StartWebserver(addr string, client client.Client, username, password string
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
w.WriteHeader(http.StatusNoContent)
}).Methods("DELETE") }).Methods("DELETE")
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/"))) r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/")))
logrus.Info("Starting webserver") return r
return http.ListenAndServe(addr, r)
} }
func getShareHead(client client.Client, w http.ResponseWriter, r *http.Request) *types.Share { func getShareHead(client *client.Client, w http.ResponseWriter, r *http.Request) *types.Share {
vars := mux.Vars(r) vars := mux.Vars(r)
slug := vars["path"][0:6] slug := vars["path"][0:6]
share, err := client.GetShare(r.Context(), slug) share, err := client.GetShare(r.Context(), slug)