/
config.go
141 lines (127 loc) · 4.96 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
// PolarisConfig defined the system configuration and router mapping. It's an interface to the end user to enable them to extend the capability.
//
package polaris
import (
"fmt"
"github.com/Unknwon/goconfig"
log "github.com/robinmin/logo"
"os"
"time"
)
// PolarisConfig is the struct to define configuration items
type PolarisConfig struct {
// DirStatic is the default folder name where the public resouces(e.g, image, css, js and etc) are putted into
DirStatic string
// DirTemplate is the folder name where the template files located in
DirTemplate string
// DirLog is the folder name where the log files located in
DirLog string
// TempExtension is the extension name of template files
TempExtension string
// TempEncoding is the encoding name of the templates files
TempEncoding string
// SessionStore is the store type of the session. it can be cookie or redis so far
SessionStore string
// SessionName is the default session name for cookie to store session id
SessionName string
// SessionMask is a mask code to the cookie of session id
SessionMask string
// Redis is the option set for redis connection
Redis *RedisConfig
// DBType is the datbase type. So far, it can be mysql/gomysql/postgres/sqlite/adodb
DBType string
// Database is the configuration items for database
Database DBConfig
// URL is a URL collection
url map[string]string
// Port is the port number current server listening on
Port int
// CfgFile is a reference pointer to the config file
CfgFile string
// CfgHandle is a reference pointer to the config file
CfgHandle *goconfig.ConfigFile
// LogFile is the file name of current log
LogFile string
// LogHandle is the log handl
LogHandle *os.File
}
// RedisConfig is the struct to define configuration items for redis
type RedisConfig struct {
// Size is the maximum number of idle connections
Size int
// Network is the communication protocal of redis
Network string
// Address is the acctual adress of the redis server hostname + port
Address string
// Password is the default password of the connection
Password string
// DB is the default database in redis
DB string
}
// Config is the interface to cental config object
type Config interface {
LoadConfig() bool
GetBasicConfig() *PolarisConfig
RoterMap(app *PolarisApplication) bool
}
// LoadConfig creates a config object by specified config file.
func (cfg *PolarisConfig) LoadConfig() bool {
cfgFile, err := goconfig.LoadConfigFile(cfg.CfgFile)
if err != nil {
log.Error("Failed to load config file:", cfg.CfgFile, ":", err)
return false
}
cfg.DirStatic = cfgFile.MustValue("system", "dir_public", "public")
cfg.DirTemplate = cfgFile.MustValue("system", "dir_template", "view")
cfg.DirLog = cfgFile.MustValue("system", "dir_log", ".")
cfg.TempExtension = cfgFile.MustValue("system", "tpl_extension", ".tpl")
cfg.TempEncoding = cfgFile.MustValue("system", "tpl_encoding", "UTF-8")
cfg.SessionStore = cfgFile.MustValue("session", "session_store", "redis")
cfg.SessionName = cfgFile.MustValue("session", "session_name", "my_session")
cfg.SessionMask = cfgFile.MustValue("session", "session_mask", "")
cfg.url = map[string]string{
"RedirectUrl": cfgFile.MustValue("system", "RedirectUrl", "/new-login"),
"RedirectParam": cfgFile.MustValue("system", "RedirectParam", "new-next"),
}
cfg.Port = cfgFile.MustInt("system", "port", 3000)
cfg.Redis = &RedisConfig{
Size: cfgFile.MustInt("redis", "redis_maxidel", 10),
Network: cfgFile.MustValue("redis", "redis_network", "tcp"),
Address: cfgFile.MustValue("redis", "redis_address", "localhost:6379"),
Password: cfgFile.MustValue("redis", "redis_password", ""),
DB: cfgFile.MustValue("redis", "redis_DB", "0"),
}
cfg.Database = DBConfig{
Type: cfgFile.MustValue("database", "db_type", "mssql"),
Driver: "",
Host: cfgFile.MustValue("database", "db_server", "localhost"),
Port: cfgFile.MustValue("database", "db_port", "1433"),
Database: cfgFile.MustValue("database", "db_database", "temdb"),
User: cfgFile.MustValue("database", "db_user", "sa"),
Password: cfgFile.MustValue("database", "db_password", ""),
Verbose: cfgFile.MustBool("database", "db_verbose", true),
LogFile: cfgFile.MustValue("database", "db_log", ""),
}
cfg.DBType = cfg.Database.Driver
cfg.CfgHandle = cfgFile
// Add file log
if len(cfg.DirLog) > 0 {
year, month, day := time.Now().Date()
cfg.LogFile = cfg.DirLog + "/" + fmt.Sprintf("app_%04d%02d%02d.log", year, month, day)
cfg.LogHandle, _ = os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if cfg.LogHandle != nil {
log.AddLogger("file", cfg.LogHandle, log.ALL)
}
}
return true
}
func (cfg *PolarisConfig) GetBasicConfig() *PolarisConfig {
return cfg
}
// RoterMap provides a interface to the end user to empower them to customize their own router
func (config *PolarisConfig) RoterMap(app *PolarisApplication) bool {
app.Get("/", func() string {
return "Hello, you should define your own configuration as the first step!"
})
return true
}