|
1 | 1 | package app
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "os"
|
| 6 | + "strings" |
5 | 7 | "time"
|
6 | 8 |
|
7 | 9 | "github.com/fragmenta/assets"
|
@@ -106,29 +108,61 @@ func SetupAssets() {
|
106 | 108 | }
|
107 | 109 | }
|
108 | 110 |
|
109 |
| - // Set up helpers which are aware of fingerprinted assets |
110 |
| - // These behave differently depending on the compile flag above |
111 |
| - // when compile is set to no, they use precompiled assets |
112 |
| - // otherwise they serve all files in a group separately |
113 |
| - view.Helpers["style"] = appAssets.StyleLink |
114 |
| - view.Helpers["script"] = appAssets.ScriptLink |
115 |
| - |
116 | 111 | }
|
117 | 112 |
|
118 | 113 | // SetupView sets up the view package by loadind templates.
|
119 | 114 | func SetupView() {
|
120 | 115 | defer log.Time(time.Now(), log.V{"msg": "Finished loading templates"})
|
121 | 116 |
|
122 | 117 | view.Production = config.Production()
|
123 |
| - err := view.LoadTemplates() |
| 118 | + |
| 119 | + // Start with default source path |
| 120 | + paths := []string{"src"} |
| 121 | + |
| 122 | + // Add a theme path if we have one |
| 123 | + theme := config.Get("theme") |
| 124 | + if theme != "" { |
| 125 | + log.Log(log.V{"msg": "loading templates for theme", "theme": theme}) |
| 126 | + themePath := fmt.Sprintf("themes/%s/src", theme) |
| 127 | + paths = append(paths, themePath) |
| 128 | + } |
| 129 | + |
| 130 | + err := view.LoadTemplatesAtPaths(paths, helperFuncs()) |
124 | 131 | if err != nil {
|
125 |
| - // server.Fatalf("Error reading templates %s", err) |
126 | 132 | log.Fatal(log.V{"msg": "unable to read templates", "error": err})
|
127 | 133 | os.Exit(1)
|
128 | 134 | }
|
129 | 135 |
|
130 | 136 | }
|
131 | 137 |
|
| 138 | +// helperFuncs returns a setr of helper functions for view templates |
| 139 | +func helperFuncs() map[string]interface{} { |
| 140 | + |
| 141 | + helpers := view.DefaultHelpers() |
| 142 | + |
| 143 | + // Set up helpers which are aware of fingerprinted assets |
| 144 | + // These behave differently depending on the compile flag above |
| 145 | + // when compile is set to no, they use precompiled assets |
| 146 | + // otherwise they serve all files in a group separately |
| 147 | + helpers["style"] = appAssets.StyleLink |
| 148 | + helpers["script"] = appAssets.ScriptLink |
| 149 | + |
| 150 | + // Get the server config for the root_url |
| 151 | + rootURL := config.Get("root_url") |
| 152 | + |
| 153 | + // If running locally use localhost instead |
| 154 | + host, err := os.Hostname() |
| 155 | + if err == nil && strings.HasSuffix(host, ".local") { |
| 156 | + rootURL = "http://localhost:3000" |
| 157 | + } |
| 158 | + |
| 159 | + helpers["root_url"] = func() string { |
| 160 | + return rootURL |
| 161 | + } |
| 162 | + |
| 163 | + return helpers |
| 164 | +} |
| 165 | + |
132 | 166 | // SetupDatabase sets up the db with query given our server config.
|
133 | 167 | func SetupDatabase() {
|
134 | 168 | defer log.Time(time.Now(), log.V{"msg": "Finished opening database", "db": config.Get("db"), "user": config.Get("db_user")})
|
|
0 commit comments