forked from naoina/kocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kocha.go
79 lines (68 loc) · 1.6 KB
/
kocha.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
package kocha
import (
"os"
"github.com/joho/godotenv"
)
const (
DefaultHttpAddr = "127.0.0.1:9100"
DefaultMaxClientBodySize = 1024 * 1024 * 10 // 10MB
StaticDir = "public"
)
// AppConfig represents a application-scope configuration.
type AppConfig struct {
AppPath string
AppName string
DefaultLayout string
TemplateSet TemplateSet
RouteTable RouteTable
Logger *Logger
Middlewares []Middleware
Session *SessionConfig
MaxClientBodySize int64
router *Router
templateMap TemplateMap
}
var (
// Global logger
Log *Logger
// The configuration of application.
appConfig *AppConfig
// Whether the app has been initialized.
initialized bool = false
)
// Init initialize the app.
func Init(config *AppConfig) {
appConfig = config
if appConfig.MaxClientBodySize < 1 {
appConfig.MaxClientBodySize = DefaultMaxClientBodySize
}
if err := appConfig.Session.Validate(); err != nil {
panic(err)
}
templateMap, err := appConfig.TemplateSet.buildTemplateMap()
if err != nil {
panic(err)
}
appConfig.templateMap = templateMap
router, err := appConfig.RouteTable.buildRouter()
if err != nil {
panic(err)
}
appConfig.router = router
Log = initLogger(appConfig.Logger)
initialized = true
}
// SettingEnv is similar to os.Getenv.
// However, SettingEnv returns def value if the variable is not present, and
// sets def to environment variable.
func SettingEnv(key, def string) string {
env := os.Getenv(key)
if env != "" {
return env
}
os.Setenv(key, def)
return def
}
func init() {
_ = godotenv.Load()
}