Commit fe22f79a authored by Artemis's avatar Artemis

Moved routes into its own struct, into another file

parent 0d07a2d5
......@@ -3,3 +3,5 @@
build/
*.min.css
node_modules/
# Configuration files, binaries etc.
paste*
......@@ -4,6 +4,8 @@ import (
"github.com/spf13/viper"
)
var NotFoundErr = "This document could not be found. Maybe it expired? Remember that documents have a 1-hour expiry time."
var ConfigName = "paste"
func SetupConfig() error {
......
package main
import (
"fmt"
"github.com/go-redis/redis"
"github.com/husobee/vestigo"
"net/http"
"time"
)
type Handlers struct {
Backend *redis.Client
Templates *Templates
}
func GetRouter(handlers *Handlers) *vestigo.Router {
r := vestigo.NewRouter()
r.Get("/", handlers.HandleHome)
r.Post("/", handlers.HandleNewPaste)
r.Get("/s/:key", handlers.HandleShowPaste)
r.Get("/r/:key", handlers.HandleShowRaw)
r.Get("/privacy.html", handlers.HandlePrivacyPolicy)
return r
}
func (h Handlers) HandleHome(w http.ResponseWriter, req *http.Request) {
err := h.Templates.homepage.Execute(w, nil)
if err != nil {
fmt.Println(err)
}
}
func (h Handlers) HandleNewPaste(w http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
w.WriteHeader(400)
fmt.Println(err)
_ = h.Templates.homepage.Execute(w, &Content{Error: "Form submit error"})
return
}
if len(req.Form["paste"]) == 0 {
// Required value
w.WriteHeader(400)
_ = h.Templates.homepage.Execute(w, &Content{Error: "You must provide a value"})
return
}
pasteValue := req.Form["paste"][0]
if pasteValue == "" {
// Required value
w.WriteHeader(400)
_ = h.Templates.homepage.Execute(w, &Content{Error: "You must provide a value", Value: pasteValue})
return
}
err, key := GetNewKey()
if err != nil {
w.WriteHeader(500)
fmt.Println(err)
return
}
res := h.Backend.Set(key, pasteValue, time.Hour)
if err := res.Err(); err != nil {
w.WriteHeader(500)
fmt.Println(err)
_ = h.Templates.homepage.Execute(w, &Content{Error: "Failed to store the value", Value: pasteValue})
return
}
http.Redirect(w, req, "/s/"+key, http.StatusFound)
_, _ = fmt.Fprint(w, key)
}
func (h Handlers) HandleShowPaste(w http.ResponseWriter, req *http.Request) {
key := vestigo.Param(req, "key")
res := h.Backend.Get(key)
if err := res.Err(); err != nil {
if err == redis.Nil {
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, NotFoundErr)
} else {
w.WriteHeader(500)
fmt.Println(err)
}
return
}
err := h.Templates.show.Execute(w, &Content{
Value: res.Val(),
Raw: "/r/" + key,
})
if err != nil {
fmt.Println(err)
}
}
func (h Handlers) HandleShowRaw(w http.ResponseWriter, req *http.Request) {
key := vestigo.Param(req, "key")
res := h.Backend.Get(key)
if err := res.Err(); err != nil {
if err == redis.Nil {
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, NotFoundErr)
} else {
w.WriteHeader(500)
fmt.Println(err)
}
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf8")
_, _ = fmt.Fprint(w, res.Val())
}
func (h Handlers) HandlePrivacyPolicy(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "privacy.html")
}
......@@ -4,12 +4,10 @@ import (
"fmt"
"github.com/elgs/gostrgen"
"github.com/go-redis/redis"
"github.com/husobee/vestigo"
"github.com/spf13/viper"
"html/template"
"log"
"net/http"
"time"
)
func GetNewKey() (error, string) {
......@@ -38,8 +36,6 @@ type Content struct {
Raw string
}
var NotFoundErr = "This document could not be found. Maybe it expired? Remember that documents have a 1-hour expiry time."
func main() {
err := SetupConfig()
if err != nil {
......@@ -56,108 +52,15 @@ func main() {
panic(err)
}
r := vestigo.NewRouter()
t := Templates{}
t := &Templates{}
t.init()
r.Get("/", func(w http.ResponseWriter, req *http.Request) {
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
}
pasteValue := req.Form["paste"][0]
if pasteValue == "" {
// Required value
w.WriteHeader(400)
_ = t.homepage.Execute(w, &Content{Error: "You must provide a value", Value: pasteValue})
return
}
err, key := GetNewKey()
if err != nil {
w.WriteHeader(500)
fmt.Println(err)
return
}
res := backend.Set(key, pasteValue, time.Hour)
if err := res.Err(); err != nil {
w.WriteHeader(500)
fmt.Println(err)
_ = t.homepage.Execute(w, &Content{Error: "Failed to store the value", Value: pasteValue})
return
}
http.Redirect(w, req, "/s/"+key, http.StatusFound)
_, _ = fmt.Fprint(w, key)
})
r.Get("/s/:key", func(w http.ResponseWriter, req *http.Request) {
key := vestigo.Param(req, "key")
res := backend.Get(key)
if err := res.Err(); err != nil {
if err == redis.Nil {
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, NotFoundErr)
} else {
w.WriteHeader(500)
fmt.Println(err)
}
return
}
err := t.show.Execute(w, &Content{
Value: res.Val(),
Raw: "/r/" + key,
})
if err != nil {
fmt.Println(err)
}
})
r.Get("/r/:key", func(w http.ResponseWriter, req *http.Request) {
key := vestigo.Param(req, "key")
res := backend.Get(key)
if err := res.Err(); err != nil {
if err == redis.Nil {
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, NotFoundErr)
} else {
w.WriteHeader(500)
fmt.Println(err)
}
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf8")
_, _ = fmt.Fprint(w, res.Val())
})
handlers := &Handlers{
Templates: t,
Backend: backend,
}
r.Get("/privacy.html", func(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "privacy.html")
})
r := GetRouter(handlers)
host := viper.GetString("HttpHost")
fmt.Printf("HTTP server about to start listening on %s\n", host)
......
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