-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.go
More file actions
173 lines (151 loc) · 4.02 KB
/
init.go
File metadata and controls
173 lines (151 loc) · 4.02 KB
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
package server
import (
"flag"
"fmt"
"os"
"path"
"github.com/achun/tom-toml"
"github.com/typepress/core"
)
const usageMessage = `` +
`Usage of TypePress:
Usage:
typepress [-Falgs [Value]]
Flags:
domain "" hosting domain.
laddr ":80" HTTP listen local network address.
static "static/" path to static files.
content "content/" path to content files.
template "template/" path to template files.
config "" path to config file.
varsion prints Server module version.
Config file fist.
If config file is empty, try to load "conf/default.toml".
`
var (
domain, laddr, contentPath, templatePath,
staticPath, configFile string
printsVarsion bool
)
func init() {
flag.StringVar(&domain, "domain", "", "hosting domain")
flag.StringVar(&laddr, "laddr", ":80", "HTTP listen local network address")
flag.StringVar(&contentPath, "content", "content/", "path to content files")
flag.StringVar(&templatePath, "template", "template/", "path to template files")
flag.StringVar(&staticPath, "static", "static/", "path to static files")
flag.StringVar(&configFile, "config", "", "path to config file")
flag.BoolVar(&printsVarsion, "version", false, "prints current version")
}
func usage() {
fmt.Fprintln(os.Stderr, usageMessage)
os.Exit(2)
}
// +dl en
/*
LoadConfig to load base configure from:
- Command-line flags.
- the environment variable, to sets such as: os.Setenv(core.SessionName+"_laddr").
- TOML format config file.
*/
// +dl
/*
LoadConfig 调入基本配置从:
- 命令行参数.
- 环境变量, 通过 os.Setenv(core.SessionName+"_laddr") 形式进行设置.
- TOML 格式配置文件.
*/
func LoadConfig() {
const try = "conf/default.toml"
var err error
flag.Usage = usage
flag.Parse()
if printsVarsion {
fmt.Println(Version)
os.Exit(0)
}
getEnv()
config := try
if len(configFile) != 0 {
config = configFile
}
if !path.IsAbs(config) {
config = path.Join(core.PWD, config)
}
core.Conf, err = toml.LoadFile(config)
if err != nil && len(configFile) != 0 {
fmt.Println("init site config:", err)
os.Exit(1)
}
if err != nil {
config = ""
core.Conf = toml.Toml{}
}
conf := core.Conf
if err != nil || nil == conf["site"] {
conf["site"] = toml.NewItem(toml.Table)
}
if conf["template"] == nil {
conf["template"] = toml.NewItem(toml.Table)
}
if conf["site.domain"] == nil {
conf["site.domain"], _ = newItem(toml.String, domain)
}
if conf["site.laddr"] == nil {
conf["site.laddr"], _ = newItem(toml.String, laddr)
}
if conf["site.content"] == nil {
conf["site.content"], _ = newItem(toml.String, contentPath)
}
if conf["site.static"] == nil {
conf["site.static"], _ = newItem(toml.String, staticPath)
}
if conf["template.path"] == nil {
conf["template.path"], _ = newItem(toml.String, templatePath)
}
domain = conf["site.domain"].String()
laddr = conf["site.laddr"].String()
contentPath = conf["site.content"].String()
staticPath = conf["site.static"].String()
templatePath = conf["template.path"].String()
fmt.Printf(`TypePress %s starting... server module %s
listen : %s
domain : %s
static : %s
content : %s
template : %s
config : %s
`,
AppVersion, Version, laddr, domain, staticPath, contentPath, templatePath, config)
}
func newItem(kind toml.Kind, x interface{}) (*toml.Item, error) {
it := toml.NewItem(kind)
return it, it.Set(x)
}
// Get flags from the environment variable
func getEnv() {
var tmp string
tmp = os.Getenv(core.SessionName + "_domain")
if len(tmp) != 0 {
domain = tmp
}
tmp = os.Getenv(core.SessionName + "_laddr")
if len(tmp) != 0 {
laddr = tmp
}
tmp = os.Getenv(core.SessionName + "_config")
if len(tmp) != 0 {
configFile = tmp
}
tmp = os.Getenv(core.SessionName + "_static")
if len(tmp) != 0 {
staticPath = tmp
}
tmp = os.Getenv(core.SessionName + "_content")
if len(tmp) != 0 {
contentPath = tmp
}
tmp = os.Getenv(core.SessionName + "_template")
if len(tmp) != 0 {
templatePath = tmp
}
}