Browse Source

Changed hard-coded values to config options

master
Gisle Aune 7 years ago
parent
commit
a83c372c9f
  1. 7
      aitestory.json
  2. BIN
      debug
  3. 2
      main.go
  4. 7
      server/config.go
  5. 23
      view/renderer.go

7
aitestory.json

@ -12,7 +12,7 @@
"server": { "server": {
"host": "0.0.0.0", "host": "0.0.0.0",
"port": 8000, "port": 8000,
"ui": "./ui/"
"debug": true
}, },
"wiki": { "wiki": {
@ -21,6 +21,11 @@
"password": "" "password": ""
}, },
"directories": {
"ui": "./ui/",
"templates": "./view/templates"
},
"test": { "test": {
"enabled": true, "enabled": true,
"username": "AiteBot@AiteStory", "username": "AiteBot@AiteStory",

BIN
debug

2
main.go

@ -11,7 +11,7 @@ func main() {
router.Mount("/user", &controllers.UserController) router.Mount("/user", &controllers.UserController)
router.Mount("/", &controllers.ListController) router.Mount("/", &controllers.ListController)
router.Static("/ui/", server.Main.Config.Server.UI)
router.Static("/ui/", server.Main.Config.Directories.UI)
server.Main.Start() server.Main.Start()
} }

7
server/config.go

@ -22,9 +22,14 @@ type Config struct {
Server struct { Server struct {
Host string `json:"host"` Host string `json:"host"`
Port int `json:"port"` Port int `json:"port"`
UI string `json:"ui"`
Debug bool `json:"debug"`
} `json:"server"` } `json:"server"`
Directories struct {
UI string `json:"ui"`
Templates string `json:"templates"`
} `json:"directories"`
Wiki struct { Wiki struct {
URL string `json:"url"` URL string `json:"url"`
Username string `json:"username"` Username string `json:"username"`

23
view/renderer.go

@ -9,17 +9,19 @@ import (
"os" "os"
"path" "path"
"git.aiterp.net/AiteRP/aitestory/server"
"git.aiterp.net/gisle/wrouter/response" "git.aiterp.net/gisle/wrouter/response"
) )
var wd, _ = os.Getwd() var wd, _ = os.Getwd()
var rootPath = path.Join(wd, "./view/templates/")
var cache = make(map[string]*template.Template) var cache = make(map[string]*template.Template)
var argsCache = make(map[string][]string) var argsCache = make(map[string][]string)
// Register registers a template and compiles it for rendering. This should be done // Register registers a template and compiles it for rendering. This should be done
// in the beginning since an error will terminate the server // in the beginning since an error will terminate the server
func Register(name string, base string, fragments ...string) { func Register(name string, base string, fragments ...string) {
rootPath := server.Main.Config.Directories.Templates
for i, fragment := range fragments { for i, fragment := range fragments {
fragments[i] = path.Join(rootPath, fragment+".tmpl") fragments[i] = path.Join(rootPath, fragment+".tmpl")
} }
@ -38,17 +40,24 @@ func Register(name string, base string, fragments ...string) {
// view model. The view model is expected to be the correct model from the viewmodel // view model. The view model is expected to be the correct model from the viewmodel
// package // package
func Render(w http.ResponseWriter, name string, status int, viewModel interface{}) { func Render(w http.ResponseWriter, name string, status int, viewModel interface{}) {
tmpl, ok := cache[name]
if !ok {
response.Text(w, 500, "Template not found: "+name)
return
}
var tmpl *template.Template
var err error
tmpl, err := template.New(name).ParseFiles(argsCache[name]...)
if server.Main.Config.Server.Debug {
tmpl, err = template.New(name).ParseFiles(argsCache[name]...)
if err != nil { if err != nil {
response.Text(w, 500, "Failed to run template "+name+": "+err.Error()) response.Text(w, 500, "Failed to run template "+name+": "+err.Error())
return return
} }
} else {
var ok bool
tmpl, ok = cache[name]
if !ok {
response.Text(w, 500, "Template not found: "+name)
return
}
}
w.WriteHeader(status) w.WriteHeader(status)
err = tmpl.ExecuteTemplate(w, "base", viewModel) err = tmpl.ExecuteTemplate(w, "base", viewModel)

Loading…
Cancel
Save