2018-11-17 20:28:02 -09:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"qurl/static"
|
|
|
|
"qurl/storage"
|
2018-11-23 19:07:39 -09:00
|
|
|
"qurl/qurl"
|
2018-11-17 20:28:02 -09:00
|
|
|
)
|
|
|
|
|
|
|
|
type RootHandler struct {
|
|
|
|
Storage storage.Storage
|
|
|
|
|
|
|
|
index *static.StaticContent
|
|
|
|
css *static.StaticContent
|
|
|
|
favi *static.StaticContent
|
2018-11-23 19:18:26 -09:00
|
|
|
usage *static.StaticContent
|
2018-11-17 20:28:02 -09:00
|
|
|
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)
|
|
|
|
|
2018-11-23 19:18:26 -09:00
|
|
|
case "/api/usage.html":
|
|
|
|
ctx.usage.ServeHTTP(w, r)
|
|
|
|
|
2018-11-19 07:03:33 -09:00
|
|
|
case "/api/url":
|
|
|
|
ctx.ServeAPI(w, r)
|
|
|
|
|
2018-11-17 20:28:02 -09:00
|
|
|
default:
|
2018-11-23 19:07:39 -09:00
|
|
|
id, err := qurl.FromString(fname[1:len(fname)])
|
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2018-11-23 19:22:41 -09:00
|
|
|
return
|
2018-11-23 19:07:39 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
q, err := ctx.Storage.GetQURLByID(id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("Database error: %s", err.Error()),
|
|
|
|
http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-23 19:22:41 -09:00
|
|
|
if q == nil {
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-23 19:07:39 -09:00
|
|
|
http.Redirect(w, r, q.URL, http.StatusPermanentRedirect)
|
2018-11-17 20:28:02 -09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-11-23 19:18:26 -09:00
|
|
|
// Initialize the static content object favicon
|
2018-11-17 20:28:02 -09:00
|
|
|
favi := &static.StaticContent{Content: "favicon.ico"}
|
|
|
|
err = favi.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.favi = favi
|
|
|
|
|
2018-11-23 19:18:26 -09:00
|
|
|
// Initialize the api usage instructions
|
|
|
|
usage := &static.StaticContent{Content: "usage.html"}
|
|
|
|
err = usage.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.usage = usage
|
|
|
|
|
2018-11-17 20:28:02 -09:00
|
|
|
// 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
|
|
|
|
}
|