-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
165 lines (140 loc) · 4.31 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
// Copyright 2013 slowfei And The Contributors All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Create on 2013-11-06
// Update on 2014-07-15
// Email slowfei#foxmail.com
// Home http://www.slowfei.com
//
// leafveingo config handle
//
package leafveingo
import (
"encoding/json"
"errors"
"github.com/slowfei/gosfcore/utils/filemanager"
"io/ioutil"
"path/filepath"
"time"
)
const (
// default web root dir name
DEFAULT_WEBROOT_DIR_NAME = "webRoot"
// default template dir name
DEFAULT_TEMPLATE_DIR_NAME = "template"
// file size default upload 32M
DEFAULT_FILE_UPLOAD_SIZE int64 = 32 << 20
// server time out default 0
DEFAULT_SERVER_TIMEOUT = 0
// default template suffix
DEFAULT_TEMPLATE_SUFFIX = ".tpl"
// default html charset
DEFAULT_HTML_CHARSET = "utf-8"
// default response write compress
DEFAULT_RESPONSE_WRITE_COMPRESS = true
// default session max life time 1800 seconds
DEFAULT_SESSION_MAX_LIFE_TIME = 1800
// default log channel size
DEFAULT_LOG_CHANNEL_SIZE = 5000
// default KeepAlivePeriod
DEFAULT_KEEP_ALIVE_PERIOD = 3 * time.Minute
)
var (
_defaultConfigJson = `
{
"AppVersion" :"1.0",
"FileUploadSize" :33554432,
"Charset" :"utf-8",
"StaticFileSuffixes" :[".js", ".css", ".png", ".jpg", ".gif", ".ico", ".html"],
"ServerTimeout" :0,
"SessionMaxlifeTime" :1800,
"IPHeaderKey" :"",
"IsReqPathIgnoreCase" :true,
"MultiProjectHosts" :[],
"TemplateSuffix" :".tpl",
"IsCompactHTML" :true,
"LogConfigPath" :"config/log.conf",
"LogGroup" :"",
"TLSCertPath" :"",
"TLSKeyPath" :"",
"TLSPort" :0,
"TLSAloneRun" :false,
"IsRespWriteCompress" :true,
"UserData" :{}
}`
)
/**
* leafveingo config
* default see _defaultConfigJson
*/
type Config struct {
AppVersion string // app version.
FileUploadSize int64 // file size upload
Charset string // html encode type
StaticFileSuffixes []string // supported static file suffixes
ServerTimeout int64 // server time out, default 0
SessionMaxlifeTime int32 // http session maxlife time, unit second. use session set
IPHeaderKey string // proxy to http headers set ip key, default ""
IsReqPathIgnoreCase bool // request url path ignore case
MultiProjectHosts []string // setting integrated multi-project hosts,default nil
TemplateSuffix string // template suffix
IsCompactHTML bool // is Compact HTML, default true
LogConfigPath string // log config path, relative or absolute path, relative path from execute file root directory
LogGroup string // log group name
TLSCertPath string // tls cert.pem, relative or absolute path, relative path from execute file root directory
TLSKeyPath string // tls key.pem
TLSPort int // tls run prot, default server port+1
TLSAloneRun bool // are leafveingo alone run tls server, default false
// is ResponseWriter writer compress gizp...
// According Accept-Encoding select compress type
// default true
IsRespWriteCompress bool
UserData map[string]string // user custom config other info
}
/**
* conifg load
*
* @param jsonData
* @return load error info
*/
func configLoadByJson(jsonData []byte, c *Config) error {
e2 := json.Unmarshal(jsonData, c)
if nil != e2 {
return e2
}
return nil
}
/**
* conifg load
*
* @param configPath
* @return error info
*/
func configLoadByFilepath(configPath string, c *Config) error {
var path string
if filepath.IsAbs(configPath) {
path = configPath
} else {
path = filepath.Join(SFFileManager.GetExecDir(), configPath)
}
isExists, isDir, _ := SFFileManager.Exists(path)
if !isExists || isDir {
return errors.New("failed to load configuration file:" + configPath)
}
jsonData, e1 := ioutil.ReadFile(path)
if nil != e1 {
return e1
}
return configLoadByJson(jsonData, c)
}