12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /*
- The Kurz Web UI exposes HTTP routes for browsers, route names to access them, and types for the requests.
- These routes are exposed by running SetupRoutes(address), which is enough to
- configure the Kurz domain API. Be sure to also configure the domain SPI to have
- a complete application.
- */
- package web
- import (
- "html/template"
- "net/http"
- "path/filepath"
- "github.com/gorilla/mux"
- )
- // Route names.
- const (
- RouteGetRoot = "kurz.web.get_root"
- RouteGetShort = "kurz.web.get_short"
- RoutePostTarget = "kurz.web.post_target"
- )
- // Content types.
- const (
- // HtmlType is the MIME HTML type.
- HtmlType = "text/html"
- // HtmlTypeRegex is a regex matching the MIME HTML type anywhere
- HtmlTypeRegex = HtmlType
- )
- var tmpl *template.Template
- type Globals struct {
- AssetsVersion int
- FullyQualifiedAssetsBaseURL string
- FullyQualifiedSiteBaseURL string
- RefreshDelay int
- SiteName string
- }
- var globals Globals
- // SetupRoutes() configures Web UI routes on the passed mux.Router.
- func SetupRoutes(router *mux.Router, configAssetsPath string) {
- const assetsPrefix = "/public"
- absAssetsDir, err := filepath.Abs(configAssetsPath)
- if err != nil {
- panic(err)
- }
- fs := http.FileServer(http.Dir(absAssetsDir))
- router.PathPrefix(assetsPrefix).Handler(http.StripPrefix(assetsPrefix, fs))
- router.Handle("/favicon.ico", fs)
- // BUG(fgm): improve Accept header matchers once https://github.com/golang/go/issues/19307 is completed.
- router.HandleFunc("/{short}", handleGetShort).
- Methods("GET", "HEAD").
- Name(RouteGetShort)
- router.HandleFunc("/", handlePostTarget).
- HeadersRegexp("Accept", HtmlTypeRegex).
- Headers("Content-Type", HtmlType).
- Methods("POST").
- Name(RoutePostTarget)
- router.HandleFunc("/", handleGetRoot).
- Methods("GET", "HEAD").
- Name(RouteGetRoot)
- base, _ := filepath.Abs(configAssetsPath + "/../templates/")
- layout := base + "/layout"
- tmpl = template.Must(template.ParseFiles(
- base+"/201.gohtml",
- base+"/404.gohtml",
- base+"/home.gohtml",
- layout+"/analytics.gohtml",
- layout+"/footer.gohtml",
- layout+"/inlinecss.gohtml",
- ))
- }
- func BuildGlobals(c map[string]interface{}) {
- // Note: keys in viper are lower-cased.
- globals = Globals{
- AssetsVersion: c["assetsversion"].(int),
- FullyQualifiedAssetsBaseURL: c["fullyqualifiedassetsbaseurl"].(string),
- FullyQualifiedSiteBaseURL: c["fullyqualifiedsitebaseurl"].(string),
- RefreshDelay: c["refreshdelay"].(int),
- SiteName: c["sitename"].(string),
- }
- }
|