123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*
- 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(listenAddress), 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"
- "net/url"
- "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
- )
- type Globals struct {
- AssetsBaseURL string
- AssetsVersion int
- AssetsPath string
- SiteBaseURL string
- RefreshDelay int
- SiteName string
- }
- var globals Globals
- var tmpl *template.Template
- // SetupRoutes() configures Web UI routes on the passed mux.Router.
- func SetupRoutes(router *mux.Router, configSiteBaseURL, configAssetsBaseURL, 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}", func(w http.ResponseWriter, r *http.Request) {
- handleGetShort(w, r, router)
- }).
- 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{
- AssetsBaseURL: c["assetsbaseurl"].(string),
- AssetsPath: c["assetspath"].(string),
- AssetsVersion: c["assetsversion"].(int),
- RefreshDelay: c["refreshdelay"].(int),
- SiteBaseURL: c["sitebaseurl"].(string),
- SiteName: c["sitename"].(string),
- }
- }
- /*
- URLFromRoute generates absolute URLs for named routes.
- To build URLs for assets, use URLForAsset().
- - ns: the assets namespace. One of "js", "css', "images".
- - path: the asset path relative to the project root
- */
- func URLFromRoute(router mux.Router, name string, params map[string]string) string {
- return ""
- }
- /*
- URLFromRoute generates absolute URLs for assets.
- To build URLs for routes, use URLFromRoute().
- - ns: the assets namespace. One of "js", "css', "images".
- - path: the asset path relative to the project root
- */
- func URLForAsset(ns string, path string) string {
- base, err := url.Parse(globals.AssetsBaseURL)
- if err != nil {
- panic(err)
- }
- base.Path = path
- res := base.String()
- return res
- }
|