From 2be2cc0626f5dfe462fe0d09b6172a52a7b1143f Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 1 Jun 2022 15:02:52 +0200 Subject: [PATCH] minor fixes in webserver --- internal/s3Share.go | 2 +- internal/web/web.go | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/s3Share.go b/internal/s3Share.go index d7cf395..bc73102 100644 --- a/internal/s3Share.go +++ b/internal/s3Share.go @@ -23,7 +23,7 @@ func Start(config *types.AppConfig) { 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 { logrus.Fatal(err.Error()) } diff --git a/internal/web/web.go b/internal/web/web.go index 62bb9dd..f2da787 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -17,11 +17,17 @@ type createShare struct { 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 == "" { 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.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.WriteHeader(http.StatusOK) + w.WriteHeader(http.StatusCreated) 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) { 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) return } + + w.WriteHeader(http.StatusNoContent) }).Methods("DELETE") r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/"))) - logrus.Info("Starting webserver") - return http.ListenAndServe(addr, r) + return 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) slug := vars["path"][0:6] share, err := client.GetShare(r.Context(), slug)