Compare commits
No commits in common. "20d24e24466805a2a213244b077c6bb6984abfc0" and "55fcd8283485f1e0a245b95912960b56ec928ea4" have entirely different histories.
20d24e2446
...
55fcd82834
4 changed files with 5 additions and 44 deletions
|
@ -4,5 +4,4 @@ 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, _ *http.Request) {
|
||||
func (config ServerConfig) home(w http.ResponseWriter, r *http.Request) {
|
||||
index, err := config.Static.ReadFile("static/index.html")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
@ -41,8 +41,7 @@ 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.CurlCreate)
|
||||
http.HandleFunc("POST /create/{$}", WebConfig.Create)
|
||||
http.HandleFunc("POST /{$}", WebConfig.Create)
|
||||
http.HandleFunc("DELETE /{key}", WebConfig.Delete)
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ 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"
|
||||
|
@ -73,41 +72,7 @@ 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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
http.Redirect(w, r, key, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// View for plak
|
||||
|
@ -165,10 +130,8 @@ 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 action="/create/" method="post">
|
||||
<form method="post">
|
||||
<div>
|
||||
<label for="lines"></label>
|
||||
<textarea id="lines" readonly wrap="hard">1</textarea>
|
||||
|
|
Loading…
Reference in a new issue