|
@@ -8,12 +8,14 @@ a complete application.
|
|
package web
|
|
package web
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "errors"
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/gorilla/mux"
|
|
"html/template"
|
|
"html/template"
|
|
"net/http"
|
|
"net/http"
|
|
"net/url"
|
|
"net/url"
|
|
|
|
+ "path"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
-
|
|
|
|
- "github.com/gorilla/mux"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
// Route names.
|
|
// Route names.
|
|
@@ -72,14 +74,19 @@ func SetupRoutes(router *mux.Router, configSiteBaseURL, configAssetsBaseURL, con
|
|
|
|
|
|
base, _ := filepath.Abs(configAssetsPath + "/../templates/")
|
|
base, _ := filepath.Abs(configAssetsPath + "/../templates/")
|
|
layout := base + "/layout"
|
|
layout := base + "/layout"
|
|
- tmpl = template.Must(template.ParseFiles(
|
|
|
|
- base+"/201.gohtml",
|
|
|
|
- base+"/404.gohtml",
|
|
|
|
- base+"/home.gohtml",
|
|
|
|
- layout+"/analytics.gohtml",
|
|
|
|
- layout+"/footer.gohtml",
|
|
|
|
- layout+"/inlinecss.gohtml",
|
|
|
|
- ))
|
|
|
|
|
|
+ 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{}) {
|
|
func BuildGlobals(c map[string]interface{}) {
|
|
@@ -101,7 +108,7 @@ To build URLs for assets, use URLForAsset().
|
|
|
|
|
|
- ns: the assets namespace. One of "js", "css', "images".
|
|
- ns: the assets namespace. One of "js", "css', "images".
|
|
- path: the asset path relative to the project root
|
|
- path: the asset path relative to the project root
|
|
- */
|
|
|
|
|
|
+*/
|
|
func URLFromRoute(router mux.Router, name string, params map[string]string) string {
|
|
func URLFromRoute(router mux.Router, name string, params map[string]string) string {
|
|
return ""
|
|
return ""
|
|
}
|
|
}
|
|
@@ -111,15 +118,25 @@ URLFromRoute generates absolute URLs for assets.
|
|
|
|
|
|
To build URLs for routes, use URLFromRoute().
|
|
To build URLs for routes, use URLFromRoute().
|
|
|
|
|
|
- - ns: the assets namespace. One of "js", "css', "images".
|
|
|
|
|
|
+ - 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
|
|
- path: the asset path relative to the project root
|
|
- */
|
|
|
|
-func URLForAsset(ns string, path string) string {
|
|
|
|
|
|
+*/
|
|
|
|
+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)
|
|
base, err := url.Parse(globals.AssetsBaseURL)
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(err)
|
|
panic(err)
|
|
}
|
|
}
|
|
- base.Path = path
|
|
|
|
|
|
+ // 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()
|
|
res := base.String()
|
|
- return res
|
|
|
|
|
|
+ return res, nil
|
|
}
|
|
}
|