web.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. The Kurz Web UI exposes HTTP routes for browsers, route names to access them, and types for the requests.
  3. These routes are exposed by running SetupRoutes(address), which is enough to
  4. configure the Kurz domain API. Be sure to also configure the domain SPI to have
  5. a complete application.
  6. */
  7. package web
  8. import (
  9. "html/template"
  10. "net/http"
  11. "path/filepath"
  12. "github.com/gorilla/mux"
  13. )
  14. // Route names.
  15. const (
  16. RouteGetRoot = "kurz.web.get_root"
  17. RouteGetShort = "kurz.web.get_short"
  18. RoutePostTarget = "kurz.web.post_target"
  19. )
  20. // Content types.
  21. const (
  22. // HtmlType is the MIME HTML type.
  23. HtmlType = "text/html"
  24. // HtmlTypeRegex is a regex matching the MIME HTML type anywhere
  25. HtmlTypeRegex = HtmlType
  26. )
  27. var tmpl *template.Template
  28. type Globals struct {
  29. AssetsVersion int
  30. FullyQualifiedAssetsBaseURL string
  31. FullyQualifiedSiteBaseURL string
  32. RefreshDelay int
  33. SiteName string
  34. }
  35. var globals Globals
  36. // SetupRoutes() configures Web UI routes on the passed mux.Router.
  37. func SetupRoutes(router *mux.Router, configAssetsPath string) {
  38. const assetsPrefix = "/public"
  39. absAssetsDir, err := filepath.Abs(configAssetsPath)
  40. if err != nil {
  41. panic(err)
  42. }
  43. fs := http.FileServer(http.Dir(absAssetsDir))
  44. router.PathPrefix(assetsPrefix).Handler(http.StripPrefix(assetsPrefix, fs))
  45. router.Handle("/favicon.ico", fs)
  46. // BUG(fgm): improve Accept header matchers once https://github.com/golang/go/issues/19307 is completed.
  47. router.HandleFunc("/{short}", handleGetShort).
  48. Methods("GET", "HEAD").
  49. Name(RouteGetShort)
  50. router.HandleFunc("/", handlePostTarget).
  51. HeadersRegexp("Accept", HtmlTypeRegex).
  52. Headers("Content-Type", HtmlType).
  53. Methods("POST").
  54. Name(RoutePostTarget)
  55. router.HandleFunc("/", handleGetRoot).
  56. Methods("GET", "HEAD").
  57. Name(RouteGetRoot)
  58. base, _ := filepath.Abs(configAssetsPath + "/../templates/")
  59. layout := base + "/layout"
  60. tmpl = template.Must(template.ParseFiles(
  61. base+"/201.gohtml",
  62. base+"/404.gohtml",
  63. base+"/home.gohtml",
  64. layout+"/analytics.gohtml",
  65. layout+"/footer.gohtml",
  66. layout+"/inlinecss.gohtml",
  67. ))
  68. }
  69. func BuildGlobals(c map[string]interface{}) {
  70. // Note: keys in viper are lower-cased.
  71. globals = Globals{
  72. AssetsVersion: c["assetsversion"].(int),
  73. FullyQualifiedAssetsBaseURL: c["fullyqualifiedassetsbaseurl"].(string),
  74. FullyQualifiedSiteBaseURL: c["fullyqualifiedsitebaseurl"].(string),
  75. RefreshDelay: c["refreshdelay"].(int),
  76. SiteName: c["sitename"].(string),
  77. }
  78. }