Compare commits

..

No commits in common. "20d24e24466805a2a213244b077c6bb6984abfc0" and "55fcd8283485f1e0a245b95912960b56ec928ea4" have entirely different histories.

4 changed files with 5 additions and 44 deletions

View file

@ -4,5 +4,4 @@ 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, _ *http.Request) { func (config ServerConfig) home(w http.ResponseWriter, r *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,8 +41,7 @@ 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.CurlCreate) http.HandleFunc("POST /{$}", WebConfig.Create)
http.HandleFunc("POST /create/{$}", WebConfig.Create)
http.HandleFunc("DELETE /{key}", WebConfig.Delete) http.HandleFunc("DELETE /{key}", WebConfig.Delete)
} }

View file

@ -8,7 +8,6 @@ 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"
@ -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))) 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
@ -165,10 +130,8 @@ 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 action="/create/" method="post"> <form 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>