2018-11-17 20:28:02 -09:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2019-12-26 07:21:48 -09:00
|
|
|
"git.binarythought.com/cdramey/qurl/static"
|
|
|
|
"git.binarythought.com/cdramey/qurl/storage"
|
|
|
|
"git.binarythought.com/cdramey/qurl/obj"
|
2018-11-17 20:28:02 -09:00
|
|
|
)
|
|
|
|
|
|
|
|
type RootHandler struct {
|
|
|
|
Storage storage.Storage
|
|
|
|
|
2019-12-26 07:21:48 -09:00
|
|
|
index *StaticContent
|
|
|
|
css *StaticContent
|
|
|
|
favi *StaticContent
|
|
|
|
usage *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:
|
2019-12-26 07:21:48 -09:00
|
|
|
id, err := obj.FromString(fname[1:len(fname)])
|
2018-11-23 19:07:39 -09:00
|
|
|
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
|
2019-12-26 07:21:48 -09:00
|
|
|
ctx.index = &StaticContent{Type: "text/html", Content: static.Index_html}
|
|
|
|
ctx.index.Init()
|
2018-11-17 20:28:02 -09:00
|
|
|
|
|
|
|
// Initialize the static content object for the css
|
2019-12-26 07:21:48 -09:00
|
|
|
ctx.css = &StaticContent{Type: "text/css", Content: static.Qurl_css}
|
|
|
|
ctx.css.Init()
|
2018-11-17 20:28:02 -09:00
|
|
|
|
2018-11-23 19:18:26 -09:00
|
|
|
// Initialize the static content object favicon
|
2019-12-26 07:21:48 -09:00
|
|
|
ctx.favi = &StaticContent{Type: "image/x-icon", Content: static.Favicon_ico}
|
|
|
|
ctx.favi.Init()
|
2018-11-17 20:28:02 -09:00
|
|
|
|
2018-11-23 19:18:26 -09:00
|
|
|
// Initialize the api usage instructions
|
2019-12-26 07:21:48 -09:00
|
|
|
ctx.usage = &StaticContent{Type: "text/html", Content: static.Usage_html}
|
|
|
|
ctx.usage.Init()
|
2018-11-23 19:18:26 -09:00
|
|
|
|
2018-11-17 20:28:02 -09:00
|
|
|
// Initialize submit page template
|
|
|
|
ctx.submit = template.New("submit.html")
|
2019-12-26 07:21:48 -09:00
|
|
|
_, err := ctx.submit.Parse(string(static.Submit_html))
|
2018-11-17 20:28:02 -09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|