From 8a88613f8902d35c761dba885240649ea9addebd Mon Sep 17 00:00:00 2001 From: Ada Date: Sat, 24 Feb 2024 01:26:27 +0100 Subject: [PATCH] :sparkles: Implement basic POST request for curl / can now read body and create paste, for curl like client, web create with full settings is move to /create --- internal/constant/constants.go | 3 ++- internal/httpServer/server.go | 5 +++-- internal/web/plak/plak.go | 39 +++++++++++++++++++++++++++++++++- static/index.html | 2 +- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/internal/constant/constants.go b/internal/constant/constants.go index 0522e19..b99a4ce 100644 --- a/internal/constant/constants.go +++ b/internal/constant/constants.go @@ -3,5 +3,6 @@ package constant import "time" const ( - HTTPTimeout = 3 * time.Second + HTTPTimeout = 3 * time.Second + ExpirationCurlCreate = 604800 * time.Second // Second in one week ) diff --git a/internal/httpServer/server.go b/internal/httpServer/server.go index b2910d0..38976dc 100644 --- a/internal/httpServer/server.go +++ b/internal/httpServer/server.go @@ -18,7 +18,7 @@ type ServerConfig struct { Templates embed.FS } -func (config ServerConfig) home(w http.ResponseWriter, r *http.Request) { +func (config ServerConfig) home(w http.ResponseWriter, _ *http.Request) { index, err := config.Static.ReadFile("static/index.html") if err != nil { log.Println(err) @@ -41,7 +41,8 @@ func (config ServerConfig) router() { http.HandleFunc("GET /{$}", config.home) http.Handle("GET /static/{file}", http.FileServer(staticFiles)) http.HandleFunc("GET /{key}/{settings...}", WebConfig.View) - http.HandleFunc("POST /{$}", WebConfig.Create) + http.HandleFunc("POST /{$}", WebConfig.CurlCreate) + http.HandleFunc("POST /create/{$}", WebConfig.Create) http.HandleFunc("DELETE /{key}", WebConfig.Delete) } diff --git a/internal/web/plak/plak.go b/internal/web/plak/plak.go index f7fbe7c..b80c137 100644 --- a/internal/web/plak/plak.go +++ b/internal/web/plak/plak.go @@ -8,6 +8,7 @@ import ( "net/http" "time" + "git.gnous.eu/gnouseu/plakken/internal/constant" "git.gnous.eu/gnouseu/plakken/internal/database" "git.gnous.eu/gnouseu/plakken/internal/utils" "github.com/redis/go-redis/v9" @@ -72,7 +73,41 @@ func (config WebConfig) Create(w http.ResponseWriter, r *http.Request) { dbConf.InsertPaste(key, content, secret, time.Duration(expiration*int(time.Second))) } - http.Redirect(w, r, key, http.StatusSeeOther) + http.Redirect(w, r, "/"+key, http.StatusSeeOther) +} + +// CurlCreate Create plak with minimum param, ideal for curl. Force 7 day expiration +func (config WebConfig) CurlCreate(w http.ResponseWriter, r *http.Request) { + if r.ContentLength == 0 { + w.WriteHeader(http.StatusBadRequest) + return + } + content, _ := io.ReadAll(r.Body) + err := r.Body.Close() + if err != nil { + log.Println(err) + } + key := utils.GenerateUrl(config.UrlLength) + secret := utils.GenerateSecret() + dbConf := database.DBConfig{ + DB: config.DB, + } + dbConf.InsertPaste(key, string(content), secret, constant.ExpirationCurlCreate) + + var baseURL string + if r.TLS == nil { + baseURL = "http://" + r.Host + "/" + key + } else { + baseURL = "https://" + r.Host + "/" + key + } + + message := baseURL + "\n" + "Delete with : 'curl -X DELETE " + baseURL + "?secret\\=" + secret + "'" + "\n" + + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + _, err = io.WriteString(w, message) + if err != nil { + log.Println(err) + } } // View for plak @@ -130,8 +165,10 @@ func (config WebConfig) Delete(w http.ResponseWriter, r *http.Request) { log.Println(err) } w.WriteHeader(http.StatusNoContent) + return } else { w.WriteHeader(http.StatusForbidden) + return } } w.WriteHeader(http.StatusNotFound) diff --git a/static/index.html b/static/index.html index 5061a27..8459130 100644 --- a/static/index.html +++ b/static/index.html @@ -11,7 +11,7 @@ -
+