-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
68 lines (61 loc) · 1.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package hime
import (
"os"
"gopkg.in/yaml.v3"
)
// AppConfig is hime app's config
type AppConfig struct {
Globals Globals `yaml:"globals" json:"globals"`
Routes Routes `yaml:"routes" json:"routes"`
Templates []TemplateConfig `yaml:"templates" json:"templates"`
}
// Config merges config into app's config
//
// Example:
//
// globals:
//
// data1: test
//
// routes:
//
// index: /
// about: /about
//
// templates:
// - dir: view
// root: layout
// delims: ["{{", "}}"]
// minify: true
// preload:
// - comp/comp1.tmpl
// - comp/comp2.tmpl
// list:
// main.tmpl:
// - main.tmpl
// - _layout.tmpl
// about.tmpl: [about.tmpl, _layout.tmpl]
func (app *App) Config(config AppConfig) {
app.Globals(config.Globals)
app.Routes(config.Routes)
for _, cfg := range config.Templates {
app.Template().Config(cfg)
}
}
// ParseConfig parses config data
func (app *App) ParseConfig(data []byte) {
var config AppConfig
err := yaml.Unmarshal(data, &config)
if err != nil {
panicf("can not parse config; %v", err)
}
app.Config(config)
}
// ParseConfigFile parses config from file
func (app *App) ParseConfigFile(filename string) {
data, err := os.ReadFile(filename)
if err != nil {
panicf("can not read config from file; %v", err)
}
app.ParseConfig(data)
}