|
@@ -21,6 +21,7 @@ import (
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/gorilla/mux"
|
|
"github.com/gorilla/sessions"
|
|
"github.com/gorilla/sessions"
|
|
|
|
+ "github.com/spf13/cast"
|
|
)
|
|
)
|
|
|
|
|
|
// Route names.
|
|
// Route names.
|
|
@@ -53,6 +54,7 @@ type Globals struct {
|
|
}
|
|
}
|
|
|
|
|
|
var globals Globals
|
|
var globals Globals
|
|
|
|
+var router mux.Router
|
|
var store *sessions.CookieStore
|
|
var store *sessions.CookieStore
|
|
var tmpl *template.Template
|
|
var tmpl *template.Template
|
|
|
|
|
|
@@ -77,21 +79,22 @@ func setupAssetRoutes(configAssetsPath string, router *mux.Router) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func setupControllerRoutes(router *mux.Router) {
|
|
|
|
|
|
+func setupControllerRoutes(gmux *mux.Router) {
|
|
|
|
+ router = *gmux
|
|
// 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}", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
- handleGetShort(w, r, router)
|
|
|
|
|
|
+ gmux.HandleFunc("/{short}", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ handleGetShort(w, r, gmux)
|
|
}).
|
|
}).
|
|
Methods("GET", "HEAD").
|
|
Methods("GET", "HEAD").
|
|
Name(RouteGetShort)
|
|
Name(RouteGetShort)
|
|
- router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
- handlePostTarget(w, r, router)
|
|
|
|
|
|
+ gmux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ handlePostTarget(w, r, gmux)
|
|
}).
|
|
}).
|
|
HeadersRegexp("Accept", HtmlTypeRegex).
|
|
HeadersRegexp("Accept", HtmlTypeRegex).
|
|
Headers("Content-Type", HtmlFormType).
|
|
Headers("Content-Type", HtmlFormType).
|
|
Methods("POST").
|
|
Methods("POST").
|
|
Name(RoutePostTarget)
|
|
Name(RoutePostTarget)
|
|
- router.HandleFunc("/", handleGetRoot).
|
|
|
|
|
|
+ gmux.HandleFunc("/", handleGetRoot).
|
|
Methods("GET", "HEAD").
|
|
Methods("GET", "HEAD").
|
|
Name(RouteGetRoot)
|
|
Name(RouteGetRoot)
|
|
}
|
|
}
|
|
@@ -101,6 +104,7 @@ func setupTemplates(configAssetsPath string) {
|
|
layout := base + "/layout"
|
|
layout := base + "/layout"
|
|
funcMap := template.FuncMap{
|
|
funcMap := template.FuncMap{
|
|
"asset": URLForAsset,
|
|
"asset": URLForAsset,
|
|
|
|
+ "path": urlFromRouteVariadic,
|
|
}
|
|
}
|
|
tmpl = template.Must(template.New("kurz").
|
|
tmpl = template.Must(template.New("kurz").
|
|
Funcs(funcMap).
|
|
Funcs(funcMap).
|
|
@@ -158,8 +162,27 @@ func URLFromRoute(router *mux.Router, name string, params map[string]string) (st
|
|
return fqsu, nil
|
|
return fqsu, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func urlFromRouteVariadic(name string, paramPairs ...interface{}) (string, error) {
|
|
|
|
+ if len(paramPairs)%2 != 0 {
|
|
|
|
+ return "", errors.New("needs an even number of arguments")
|
|
|
|
+ }
|
|
|
|
+ var params = make(map[string]string, len(paramPairs)/2)
|
|
|
|
+ var k string
|
|
|
|
+ for i, r := range paramPairs {
|
|
|
|
+ if i%2 == 0 {
|
|
|
|
+ // If i is even, this is the key for the next value.
|
|
|
|
+ k = r.(string)
|
|
|
|
+ } else {
|
|
|
|
+ // Else it is a value, so store it with the key we just got previously.
|
|
|
|
+ params[k] = cast.ToString(r)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return URLFromRoute(&router, name, params)
|
|
|
|
+}
|
|
|
|
+
|
|
/*
|
|
/*
|
|
-URLFromRoute generates absolute URLs for assets.
|
|
|
|
|
|
+URLForAsset generates absolute URLs for assets.
|
|
|
|
|
|
To build URLs for routes, use URLFromRoute().
|
|
To build URLs for routes, use URLFromRoute().
|
|
|
|
|