Migrated all http mechanics to a central handler, changed static
content to all be front-loaded and thread safe
This commit is contained in:
75
pages/root.go
Normal file
75
pages/root.go
Normal file
@ -0,0 +1,75 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"qurl/static"
|
||||
"qurl/storage"
|
||||
)
|
||||
|
||||
type RootHandler struct {
|
||||
Storage storage.Storage
|
||||
|
||||
index *static.StaticContent
|
||||
css *static.StaticContent
|
||||
favi *static.StaticContent
|
||||
submit *template.Template
|
||||
}
|
||||
|
||||
func (ctx *RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fname := r.URL.Path
|
||||
|
||||
switch fname {
|
||||
case "/index.html":
|
||||
fallthrough
|
||||
case "/":
|
||||
ctx.index.ServeHTTP(w, r)
|
||||
|
||||
case "/submit.html":
|
||||
ctx.ServeSubmit(w, r)
|
||||
|
||||
case "/qurl.css":
|
||||
ctx.css.ServeHTTP(w, r)
|
||||
|
||||
case "/favicon.ico":
|
||||
ctx.favi.ServeHTTP(w, r)
|
||||
|
||||
default:
|
||||
fmt.Printf("Path: %s\n", fname)
|
||||
}
|
||||
}
|
||||
|
||||
func (ctx *RootHandler) Init() error {
|
||||
// Initialize the static content object for the index page
|
||||
index := &static.StaticContent{Content: "index.html"}
|
||||
err := index.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.index = index
|
||||
|
||||
// Initialize the static content object for the css
|
||||
css := &static.StaticContent{Content: "qurl.css"}
|
||||
err = css.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.css = css
|
||||
|
||||
// Initialize the static content object for the css
|
||||
favi := &static.StaticContent{Content: "favicon.ico"}
|
||||
err = favi.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.favi = favi
|
||||
|
||||
// Initialize submit page template
|
||||
ctx.submit = template.New("submit.html")
|
||||
_, err = ctx.submit.Parse(string(static.Assets["submit.html"]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -3,36 +3,18 @@ package pages
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net"
|
||||
"net/http"
|
||||
"qurl/qurl"
|
||||
"qurl/static"
|
||||
"qurl/storage"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SubmitHandler struct {
|
||||
Storage storage.Storage
|
||||
template *template.Template
|
||||
}
|
||||
|
||||
type submitPage struct {
|
||||
Message string
|
||||
URL string
|
||||
}
|
||||
|
||||
func (ctx *SubmitHandler) Init() error {
|
||||
ctx.template = template.New("submit.html")
|
||||
|
||||
_, err := ctx.template.Parse(string(static.Assets["submit.html"]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ctx *SubmitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *RootHandler) ServeSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
var pg submitPage
|
||||
|
||||
u := r.FormValue("url")
|
||||
@ -79,7 +61,7 @@ func (ctx *SubmitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err := ctx.template.Execute(&buf, pg)
|
||||
err := ctx.submit.Execute(&buf, pg)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Template execute error: %s", err.Error()),
|
||||
http.StatusInternalServerError)
|
||||
|
Reference in New Issue
Block a user