Commit 2c9ecded authored by Artemis's avatar Artemis

Moved different codes into their own files, fixed template initializer to use a cleaner solution

parent 331539c7
package main
import (
"fmt"
"github.com/elgs/gostrgen"
)
func GetNewKey() (error, string) {
key, err := gostrgen.RandGen(6, gostrgen.LowerUpper, "", "")
if err != nil {
fmt.Print(err)
return err, ""
}
return nil, key
}
......@@ -13,14 +13,20 @@ type Handlers struct {
Templates *Templates
}
func GetRouter(handlers *Handlers) *vestigo.Router {
type Content struct {
Error string
Value string
Raw string
}
func (h Handlers) GetRouter() *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)
r.Get("/", h.HandleHome)
r.Post("/", h.HandleNewPaste)
r.Get("/s/:key", h.HandleShowPaste)
r.Get("/r/:key", h.HandleShowRaw)
r.Get("/privacy.html", h.HandlePrivacyPolicy)
return r
}
......
......@@ -2,40 +2,12 @@ package main
import (
"fmt"
"github.com/elgs/gostrgen"
"github.com/go-redis/redis"
"github.com/spf13/viper"
"html/template"
"log"
"net/http"
)
func GetNewKey() (error, string) {
key, err := gostrgen.RandGen(6, gostrgen.LowerUpper, "", "")
if err != nil {
fmt.Print(err)
return err, ""
}
return nil, key
}
type Templates struct {
homepage *template.Template
show *template.Template
}
func (t *Templates) init() {
t.homepage = template.Must(template.ParseFiles("templates/homepage.html"))
t.show = template.Must(template.ParseFiles("templates/show.html"))
}
type Content struct {
Error string
Value string
Raw string
}
func main() {
err := SetupConfig()
if err != nil {
......@@ -52,17 +24,12 @@ func main() {
panic(err)
}
t := &Templates{}
t.init()
handlers := &Handlers{
Templates: t,
Templates: InitTemplates(),
Backend: backend,
}
r := GetRouter(handlers)
host := viper.GetString("HttpHost")
fmt.Printf("HTTP server about to start listening on %s\n", host)
log.Fatal(http.ListenAndServe(host, r))
log.Fatal(http.ListenAndServe(host, handlers.GetRouter()))
}
package main
import "html/template"
type Templates struct {
homepage *template.Template
show *template.Template
}
func InitTemplates() *Templates {
return &Templates{
homepage: template.Must(template.ParseFiles("templates/homepage.html")),
show: template.Must(template.ParseFiles("templates/show.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