web.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 "github.com/gorilla/mux"
  9. // Route names.
  10. const (
  11. RouteGetRoot = "kurz.web.get_root"
  12. RouteGetShort = "kurz.web.get_short"
  13. RoutePostTarget = "kurz.web.post_target"
  14. )
  15. // Content types.
  16. const (
  17. // HtmlType is the MIME HTML type.
  18. HtmlType = "text/html"
  19. // HtmlTypeRegex is a regex matching the MIME HTML type anywhere
  20. HtmlTypeRegex = HtmlType
  21. )
  22. // SetupRoutes treats s as UTF-8-encoded bytes and returns a copy with all Unicode letters that begin
  23. // words mapped to their title case.
  24. func SetupRoutes(router *mux.Router) {
  25. // BUG(fgm): improve Accept header matchers once https://github.com/golang/go/issues/19307 is completed.
  26. router.HandleFunc("/{short}", handleGetShort).
  27. HeadersRegexp("Accept", HtmlTypeRegex).
  28. Methods("GET", "HEAD").
  29. Name(RouteGetShort)
  30. router.HandleFunc("/", handlePostTarget).
  31. HeadersRegexp("Accept", HtmlTypeRegex).
  32. Headers("Content-Type", HtmlType).
  33. Methods("POST").
  34. Name(RoutePostTarget)
  35. router.HandleFunc("/", handleGetRoot).
  36. HeadersRegexp("Accept", HtmlTypeRegex).
  37. Methods("GET", "HEAD").
  38. Name(RouteGetRoot)
  39. }