Commit 12a445bc authored by Artemis's avatar Artemis 🐱

Added base template structure and post logic

parent 5234d209
......@@ -5,3 +5,10 @@ paste
:target: https://www.buymeacoffee.com/Artemis
Small temp redis-based pastebin server.
Environment variables
=====================
- ``HTTP_HOST`` (default ``:1234``): HTTP listening interface host
- ``REDIS_ADDR`` (required): Redis URL
- ``REDIS_PASS`` (optional, default ""): Redis password
......@@ -3,11 +3,31 @@ package main
import (
"fmt"
"github.com/elgs/gostrgen"
"github.com/go-redis/redis"
"github.com/husobee/vestigo"
"html/template"
"log"
"net/http"
"os"
)
func getEnv(key string) string {
val := os.Getenv(key)
if len(val) == 0 {
panic(key + " is a required environment variable key")
}
return val
}
func getEnvDefault(key string, defaultVal string) string {
val := os.Getenv(key)
if len(val) == 0 {
val = defaultVal
}
return val
}
func GetNewKey() (error, string) {
key, err := gostrgen.RandGen(6, gostrgen.LowerUpper, "", "")
if err != nil {
......@@ -28,19 +48,59 @@ func GetNewKey() (error, string) {
// return nil, key
//}
type Templates struct {
homepage *template.Template
}
type Content struct {
Error string
Value string
}
func (t *Templates) init() {
t.homepage = template.Must(template.ParseFiles("templates/homepage.html"))
}
func main() {
//backend := redis.NewClient(&redis.Options{})
//res := backend.Ping()
//if err := res.Err(); err != nil { panic(err) }
backend := redis.NewClient(&redis.Options{
Addr: getEnv("REDIS_ADDR"),
Password: getEnvDefault("REDIS_PASS", ""),
DB: 0,
})
res := backend.Ping()
if err := res.Err(); err != nil {
panic(err)
}
r := vestigo.NewRouter()
t := Templates{}
t.init()
r.Get("/", func(w http.ResponseWriter, req *http.Request) {
_, _ = fmt.Fprint(w, "TODO homepage")
err := t.homepage.Execute(w, nil)
if err != nil {
fmt.Println(err)
}
})
r.Post("/", func(w http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
w.WriteHeader(400)
fmt.Println(err)
_ = t.homepage.Execute(w, &Content{Error: "Form submit error"})
return
}
if len(req.Form["paste"]) == 0 {
// Required value
w.WriteHeader(400)
_ = t.homepage.Execute(w, &Content{Error: "You must provide a value"})
return
}
err, key := GetNewKey()
value := req.Form["paste"][0]
if err != nil {
w.WriteHeader(500)
......@@ -48,7 +108,7 @@ func main() {
return
}
_, _ = fmt.Fprintln(w, key)
_, _ = fmt.Fprintln(w, key+value)
})
r.Get("/s/:key", func(w http.ResponseWriter, req *http.Request) {
......@@ -63,5 +123,7 @@ func main() {
_, _ = fmt.Fprintf(w, "TODO direct raw retrieval, got key %s", key)
})
log.Fatal(http.ListenAndServe(":1234", r))
host := getEnvDefault("HTTP_HOST", ":1234")
fmt.Printf("HTTP server about to start listening on %s\n", host)
log.Fatal(http.ListenAndServe(host, r))
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Paste</title>
<link rel="stylesheet" href="https://res.axdn.fr/folio/style.min.css" />
</head>
<body>
<h1>Paste</h1>
{{if .Error}}
<section>
<p><b>{{.Error}}</b></p>
</section>
{{end}}
<section>
<form method="post">
<fieldset>
<label for="paste">Your document</label>
<textarea name="paste" id="paste" rows="15" required autofocus>{{.Value}}</textarea>
</fieldset>
<fieldset>
<button type="submit">Store</button>
</fieldset>
</form>
</section>
<hr>
<footer>
<a href="https://gitlab.com/Artemix/paste" target="_blank">Source code</a>
</footer>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment