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
This commit is contained in:
Ada 2024-02-24 01:26:27 +01:00
parent d93c024a0c
commit e99c71cc20
Signed by: ada
GPG key ID: 6A7F898157C6DE6E
4 changed files with 44 additions and 5 deletions

View file

@ -3,5 +3,6 @@ package constant
import "time" import "time"
const ( const (
HTTPTimeout = 3 * time.Second HTTPTimeout = 3 * time.Second
ExpirationCurlCreate = 604800 * time.Second // Second in one week
) )

View file

@ -18,7 +18,7 @@ type ServerConfig struct {
Templates embed.FS 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") index, err := config.Static.ReadFile("static/index.html")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -41,7 +41,8 @@ func (config ServerConfig) router() {
http.HandleFunc("GET /{$}", config.home) http.HandleFunc("GET /{$}", config.home)
http.Handle("GET /static/{file}", http.FileServer(staticFiles)) http.Handle("GET /static/{file}", http.FileServer(staticFiles))
http.HandleFunc("GET /{key}/{settings...}", WebConfig.View) 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) http.HandleFunc("DELETE /{key}", WebConfig.Delete)
} }

View file

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"time" "time"
"git.gnous.eu/gnouseu/plakken/internal/constant"
"git.gnous.eu/gnouseu/plakken/internal/database" "git.gnous.eu/gnouseu/plakken/internal/database"
"git.gnous.eu/gnouseu/plakken/internal/utils" "git.gnous.eu/gnouseu/plakken/internal/utils"
"github.com/redis/go-redis/v9" "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))) 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 // View for plak
@ -130,8 +165,10 @@ func (config WebConfig) Delete(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
return
} else { } else {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
return
} }
} }
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)

View file

@ -11,7 +11,7 @@
<link href="/static/style.css" rel="stylesheet"> <link href="/static/style.css" rel="stylesheet">
</head> </head>
<body> <body>
<form method="post"> <form action="/create/" method="post">
<div> <div>
<label for="lines"></label> <label for="lines"></label>
<textarea id="lines" readonly wrap="hard">1</textarea> <textarea id="lines" readonly wrap="hard">1</textarea>