|
@@ -1,7 +1,7 @@
|
|
/*
|
|
/*
|
|
The Kurz Web UI exposes HTTP routes for browsers, route names to access them, and types for the requests.
|
|
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
|
|
|
|
|
|
+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
|
|
configure the Kurz domain API. Be sure to also configure the domain SPI to have
|
|
a complete application.
|
|
a complete application.
|
|
*/
|
|
*/
|
|
@@ -10,6 +10,7 @@ package web
|
|
import (
|
|
import (
|
|
"html/template"
|
|
"html/template"
|
|
"net/http"
|
|
"net/http"
|
|
|
|
+ "net/url"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/gorilla/mux"
|
|
@@ -31,20 +32,20 @@ const (
|
|
HtmlTypeRegex = HtmlType
|
|
HtmlTypeRegex = HtmlType
|
|
)
|
|
)
|
|
|
|
|
|
-var tmpl *template.Template
|
|
|
|
-
|
|
|
|
type Globals struct {
|
|
type Globals struct {
|
|
- AssetsVersion int
|
|
|
|
- FullyQualifiedAssetsBaseURL string
|
|
|
|
- FullyQualifiedSiteBaseURL string
|
|
|
|
- RefreshDelay int
|
|
|
|
- SiteName string
|
|
|
|
|
|
+ AssetsBaseURL string
|
|
|
|
+ AssetsVersion int
|
|
|
|
+ AssetsPath string
|
|
|
|
+ SiteBaseURL string
|
|
|
|
+ RefreshDelay int
|
|
|
|
+ SiteName string
|
|
}
|
|
}
|
|
|
|
|
|
var globals Globals
|
|
var globals Globals
|
|
|
|
+var tmpl *template.Template
|
|
|
|
|
|
// SetupRoutes() configures Web UI routes on the passed mux.Router.
|
|
// SetupRoutes() configures Web UI routes on the passed mux.Router.
|
|
-func SetupRoutes(router *mux.Router, configAssetsPath string) {
|
|
|
|
|
|
+func SetupRoutes(router *mux.Router, configSiteBaseURL, configAssetsBaseURL, configAssetsPath string) {
|
|
const assetsPrefix = "/public"
|
|
const assetsPrefix = "/public"
|
|
absAssetsDir, err := filepath.Abs(configAssetsPath)
|
|
absAssetsDir, err := filepath.Abs(configAssetsPath)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -55,7 +56,9 @@ func SetupRoutes(router *mux.Router, configAssetsPath string) {
|
|
router.Handle("/favicon.ico", fs)
|
|
router.Handle("/favicon.ico", fs)
|
|
|
|
|
|
// BUG(fgm): improve Accept header matchers once https://github.com/golang/go/issues/19307 is completed.
|
|
// BUG(fgm): improve Accept header matchers once https://github.com/golang/go/issues/19307 is completed.
|
|
- router.HandleFunc("/{short}", handleGetShort).
|
|
|
|
|
|
+ router.HandleFunc("/{short}", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ handleGetShort(w, r, router)
|
|
|
|
+ }).
|
|
Methods("GET", "HEAD").
|
|
Methods("GET", "HEAD").
|
|
Name(RouteGetShort)
|
|
Name(RouteGetShort)
|
|
router.HandleFunc("/", handlePostTarget).
|
|
router.HandleFunc("/", handlePostTarget).
|
|
@@ -82,10 +85,41 @@ func SetupRoutes(router *mux.Router, configAssetsPath string) {
|
|
func BuildGlobals(c map[string]interface{}) {
|
|
func BuildGlobals(c map[string]interface{}) {
|
|
// Note: keys in viper are lower-cased.
|
|
// Note: keys in viper are lower-cased.
|
|
globals = Globals{
|
|
globals = Globals{
|
|
- AssetsVersion: c["assetsversion"].(int),
|
|
|
|
- FullyQualifiedAssetsBaseURL: c["fullyqualifiedassetsbaseurl"].(string),
|
|
|
|
- FullyQualifiedSiteBaseURL: c["fullyqualifiedsitebaseurl"].(string),
|
|
|
|
- RefreshDelay: c["refreshdelay"].(int),
|
|
|
|
- SiteName: c["sitename"].(string),
|
|
|
|
|
|
+ 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
|
|
}
|
|
}
|