-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
214 lines (178 loc) · 5.46 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package uos
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"os"
"path/filepath"
)
// AppConfiguration specifies application/framework configuration.
// Is read from a JSON configuration file in ComponentSetup.
type AppConfiguration struct {
// application context information, available as template context
AppInfo map[string]interface{} `json:"app"`
// web application port
Port int `json:"port"`
// base deployment directory - only files "below" this directory are used.
// If empty or not defined, the directory of the executable is set.
BaseDir string `json:"base_dir"`
Logging LogConfiguration `json:"logging"`
Monitoring MonitoringConfiguration `json:"monitoring"`
Database DBConfiguration `json:"database"`
Assets AssetConfiguration `json:"assets"`
I18N I18NConfiguration `json:"i18n"`
Auth AuthenticationConfiguration `json:"auth"`
// page configuration integrated into HTML pages. To define common settings
// the page "_default" can be specified.
Pages map[string]PageConfiguration `json:"pages"`
Tuning TuningConfiguration `json:"tuning"`
Features FeatureConfiguration `json:"-"`
}
// LogConfiguration specifies logging behaviour.
type LogConfiguration struct {
// log level (panic, fatal, error, warn, info, debug, trace)
Level string `json:"level"`
// write logmessages as colored output to stderr - otherwise log as JSON
UseConsole bool `json:"use_console"`
}
// MonitoringConfiguration specifies ports for application monitoring.
type MonitoringConfiguration struct {
// port for pprof web interface
PortPPROF int `json:"pprof"`
// port for application metrics (Prometheus)
PortMetrics int `json:"metrics"`
}
// DBConfiguration specifies the database.
type DBConfiguration struct {
// SQLite database file
File string `json:"file"`
}
// AssetConfiguration specifies directories containing different types of static data.
type AssetConfiguration struct {
// directory containing "dynamic" assets (= assets that are not included in the executable)
Dynamic string `json:"dynamic"`
// directory containing template files for pages, forms, dialogs, fragements.
Templates string `json:"templates"`
// directory containing markdown documents
Markdown string `json:"markdown"`
}
type I18NConfiguration struct {
// directory containing translations (PO files)
Locale string `json:"locale"`
// list of supported languages (first entry is primary language)
Languages []string `json:"languages"`
}
// AuthenticationConfiguration specifies required keys for cookie handling.
// If a propertie is changed, existing cookies are invalidated.
type AuthenticationConfiguration struct {
HashKey string `json:"hash"`
BlockKey string `json:"block"`
hash []byte
block []byte
}
type PageConfiguration struct {
Title string `json:"title"`
Description string `json:"description"`
Author string `json:"author"`
URL string `json:"url"`
StaticBaseURL string `json:"static_base_url"`
FavIcon string `json:"favicon"`
Styles []string `json:"styles"`
ScriptsHead []string `json:"scripts_head"`
ScriptsBody []string `json:"scripts_body"`
}
func (pc PageConfiguration) clone() PageConfiguration {
var (
buf bytes.Buffer
enc = gob.NewEncoder(&buf)
dec = gob.NewDecoder(&buf)
)
err := enc.Encode(pc)
if err != nil {
Log.ErrorObj("could not clone page configuration (encode)", err)
return pc
}
var cloned PageConfiguration
err = dec.Decode(&cloned)
if err != nil {
Log.ErrorObj("could not clone page configuration (decode)", err)
return pc
}
return cloned
}
type TuningConfiguration struct {
ActivateHTMXPreloading bool `json:"htmx_preload"`
}
type FeatureConfiguration struct {
Dialogs bool `json:"-"`
}
var Config = AppConfiguration{}
func readConfiguration(configFilePath string) error {
configFileContent, err := os.ReadFile(configFilePath)
if err != nil {
return err
}
err = json.Unmarshal(configFileContent, &Config)
if err != nil {
return err
}
// check/create authentification info
if len(Config.Auth.HashKey) == 0 {
Config.Auth.HashKey = randomString(64)
fmt.Printf("generated hash key: %s\n", Config.Auth.HashKey)
}
if len(Config.Auth.BlockKey) == 0 {
Config.Auth.BlockKey = randomString(32)
fmt.Printf("generated block key: %s\n", Config.Auth.BlockKey)
}
Config.Auth.hash = []byte(Config.Auth.HashKey)
Config.Auth.block = []byte(Config.Auth.BlockKey)
// determine base directory
if Config.BaseDir == "" {
exePath, err := os.Executable()
if err != nil {
return err
}
Config.BaseDir, err = filepath.Abs(exePath)
if err != nil {
return err
}
}
return nil
}
func (c AppConfiguration) getPageConfig(pageName string) PageConfiguration {
var (
result = c.Pages["_default"].clone()
pageConfig = c.Pages[pageName]
)
// integrate specific page configuration into result
if pageConfig.Title != "" {
result.Title = pageConfig.Title
}
if pageConfig.Description != "" {
result.Description = pageConfig.Description
}
if pageConfig.Author != "" {
result.Author = pageConfig.Author
}
if pageConfig.URL != "" {
result.URL = pageConfig.URL
}
if pageConfig.StaticBaseURL != "" {
result.StaticBaseURL = pageConfig.StaticBaseURL
}
if pageConfig.FavIcon != "" {
result.FavIcon = pageConfig.FavIcon
}
if len(pageConfig.Styles) != 0 {
result.Styles = pageConfig.Styles
}
if len(pageConfig.ScriptsHead) != 0 {
result.Styles = pageConfig.ScriptsHead
}
if len(pageConfig.ScriptsBody) != 0 {
result.Styles = pageConfig.ScriptsBody
}
return result
}