Merge pull request '✨ Implement basic POST request for curl' (#30) from ada/curl-upload into main
Reviewed-on: #30
This commit is contained in:
commit
7c6e18538b
4 changed files with 44 additions and 5 deletions
|
@ -4,4 +4,5 @@ import "time"
|
|||
|
||||
const (
|
||||
HTTPTimeout = 3 * time.Second
|
||||
ExpirationCurlCreate = 604800 * time.Second // Second in one week
|
||||
)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
<link href="/static/style.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<form method="post">
|
||||
<form action="/create/" method="post">
|
||||
<div>
|
||||
<label for="lines"></label>
|
||||
<textarea id="lines" readonly wrap="hard">1</textarea>
|
||||
|
|
Loading…
Reference in a new issue