/* 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 ( "errors" "fmt" "github.com/gorilla/mux" "html/template" "net/http" "net/url" "path" "path/filepath" ) // 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" funcMap := template.FuncMap{ "asset": URLForAsset, } tmpl = template.Must(template.New("kurz"). Funcs(funcMap). 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". "" is only expected to be used for "favicon.ico". - path: the asset path relative to the project root */ func URLForAsset(ns string, assetPath string) (string, error) { if ns != "" && ns != "css" && ns != "js" && ns != "images" { return "", errors.New("invalid asset namespace: " + ns) } version := globals.AssetsVersion base, err := url.Parse(globals.AssetsBaseURL) if err != nil { panic(err) } // Handles "" cleanly, and doesn't use a "\" on windows, unlike filepath.Join. base.Path = path.Join(ns, assetPath) // No need to url.QueryEscape() since this format is query-clean by construction. base.RawQuery = fmt.Sprintf("v=%d", version) res := base.String() return res, nil }