Changed hard-coded values to config options
This commit is contained in:
+6
-1
@@ -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",
|
||||||
|
|||||||
@@ -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()
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-3
@@ -20,11 +20,16 @@ type Config struct {
|
|||||||
} `json:"db"`
|
} `json:"db"`
|
||||||
|
|
||||||
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"`
|
||||||
|
|||||||
+19
-10
@@ -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,16 +40,23 @@ 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]
|
var tmpl *template.Template
|
||||||
if !ok {
|
var err error
|
||||||
response.Text(w, 500, "Template not found: "+name)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.New(name).ParseFiles(argsCache[name]...)
|
if server.Main.Config.Server.Debug {
|
||||||
if err != nil {
|
tmpl, err = template.New(name).ParseFiles(argsCache[name]...)
|
||||||
response.Text(w, 500, "Failed to run template "+name+": "+err.Error())
|
if err != nil {
|
||||||
return
|
response.Text(w, 500, "Failed to run template "+name+": "+err.Error())
|
||||||
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user