config.go 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
package beego

import (
	"html/template"
	"os"
	"path"
	"runtime"
	"strconv"
	"strings"
P
Pengfei Xue 已提交
10 11 12

	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/session"
13 14 15 16 17 18 19 20 21
)

var (
	BeeApp        *App
	AppName       string
	AppPath       string
	AppConfigPath string
	StaticDir     map[string]string
	TemplateCache map[string]*template.Template
F
Francois 已提交
22
	StaticExtensionsToGzip []string //Files which should also be compressed with gzip (.js, .css, etc)
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
	HttpAddr      string
	HttpPort      int
	HttpTLS       bool
	HttpCertFile  string
	HttpKeyFile   string
	RecoverPanic  bool
	AutoRender    bool
	ViewsPath     string
	RunMode       string //"dev" or "prod"
	AppConfig     config.ConfigContainer
	//related to session
	GlobalSessions        *session.Manager //GlobalSessions
	SessionOn             bool             // whether auto start session,default is false
	SessionProvider       string           // default session provider  memory mysql redis
	SessionName           string           // sessionName cookie's name
	SessionGCMaxLifetime  int64            // session's gc maxlifetime
	SessionSavePath       string           // session savepath if use mysql/redis/file this set to the connectinfo
	SessionHashFunc       string
	SessionHashKey        string
	SessionCookieLifeTime int
	UseFcgi               bool
	MaxMemory             int64
	EnableGzip            bool   // enable gzip
	DirectoryIndex        bool   //enable DirectoryIndex default is false
	EnableHotUpdate       bool   //enable HotUpdate default is false
	HttpServerTimeOut     int64  //set httpserver timeout
	ErrorsShow            bool   //set weather show errors
	XSRFKEY               string //set XSRF
	EnableXSRF            bool
	XSRFExpire            int
	CopyRequestBody       bool //When in raw application, You want to the reqeustbody
	TemplateLeft          string
	TemplateRight         string
	BeegoServerName       string
	EnableAdmin           bool   //enable admin module to log api time
	AdminHttpAddr         string //admin module http addr
	AdminHttpPort         int
)

A
astaxie 已提交
62
func init() {
P
Pengfei Xue 已提交
63
	// create beeapp
A
astaxie 已提交
64
	BeeApp = NewApp()
P
Pengfei Xue 已提交
65 66 67

	// initialize default configurations
	os.Chdir(path.Dir(os.Args[0]))
68
	AppPath = path.Dir(os.Args[0])
P
Pengfei Xue 已提交
69

70
	StaticDir = make(map[string]string)
P
Pengfei Xue 已提交
71
	StaticDir["/static"] = "static"
F
Francois 已提交
72 73
	
	StaticExtensionsToGzip = []string{".css", ".js"}
P
Pengfei Xue 已提交
74

A
typo  
astaxie 已提交
75
	TemplateCache = make(map[string]*template.Template)
P
Pengfei Xue 已提交
76 77

	// set this to 0.0.0.0 to make this app available to externally
A
astaxie 已提交
78
	HttpAddr = ""
79
	HttpPort = 8080
P
Pengfei Xue 已提交
80

81
	AppName = "beego"
P
Pengfei Xue 已提交
82

83
	RunMode = "dev" //default runmod
P
Pengfei Xue 已提交
84

85
	AutoRender = true
P
Pengfei Xue 已提交
86

87
	RecoverPanic = true
P
Pengfei Xue 已提交
88

89
	ViewsPath = "views"
P
Pengfei Xue 已提交
90

91 92 93 94 95 96 97
	SessionOn = false
	SessionProvider = "memory"
	SessionName = "beegosessionID"
	SessionGCMaxLifetime = 3600
	SessionSavePath = ""
	SessionHashFunc = "sha1"
	SessionHashKey = "beegoserversessionkey"
A
astaxie 已提交
98
	SessionCookieLifeTime = 0 //set cookie default is the brower life
P
Pengfei Xue 已提交
99

100
	UseFcgi = false
P
Pengfei Xue 已提交
101

102
	MaxMemory = 1 << 26 //64MB
P
Pengfei Xue 已提交
103

104
	EnableGzip = false
P
Pengfei Xue 已提交
105

A
astaxie 已提交
106
	AppConfigPath = path.Join(AppPath, "conf", "app.conf")
P
Pengfei Xue 已提交
107

108
	HttpServerTimeOut = 0
P
Pengfei Xue 已提交
109

110
	ErrorsShow = true
P
Pengfei Xue 已提交
111

112 113
	XSRFKEY = "beegoxsrf"
	XSRFExpire = 0
P
Pengfei Xue 已提交
114

115 116
	TemplateLeft = "{{"
	TemplateRight = "}}"
P
Pengfei Xue 已提交
117

118
	BeegoServerName = "beegoServer"
P
Pengfei Xue 已提交
119

120
	EnableAdmin = true
P
Pengfei Xue 已提交
121
	AdminHttpAddr = "127.0.0.1"
122
	AdminHttpPort = 8088
P
Pengfei Xue 已提交
123

124
	runtime.GOMAXPROCS(runtime.NumCPU())
P
Pengfei Xue 已提交
125 126 127

	err := ParseConfig()
	if err != nil && !os.IsNotExist(err) {
128 129
		// for init if doesn't have app.conf will not panic
		Info(err)
P
Pengfei Xue 已提交
130
	}
131 132
}

A
astaxie 已提交
133
//parse config now only support ini, next will support json
134 135 136 137 138
func ParseConfig() (err error) {
	AppConfig, err = config.NewConfig("ini", AppConfigPath)
	if err != nil {
		return err
	} else {
139
		HttpAddr = AppConfig.String("HttpAddr")
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

		if v, err := AppConfig.Int("HttpPort"); err == nil {
			HttpPort = v
		}

		if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil {
			MaxMemory = maxmemory
		}

		if appname := AppConfig.String("AppName"); appname != "" {
			AppName = appname
		}

		if runmode := AppConfig.String("RunMode"); runmode != "" {
			RunMode = runmode
		}

		if autorender, err := AppConfig.Bool("AutoRender"); err == nil {
			AutoRender = autorender
		}

		if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil {
			RecoverPanic = autorecover
		}

		if views := AppConfig.String("ViewsPath"); views != "" {
			ViewsPath = views
		}

		if sessionon, err := AppConfig.Bool("SessionOn"); err == nil {
			SessionOn = sessionon
		}

		if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" {
			SessionProvider = sessProvider
		}

		if sessName := AppConfig.String("SessionName"); sessName != "" {
			SessionName = sessName
		}

		if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" {
			SessionSavePath = sesssavepath
		}

		if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" {
			SessionHashFunc = sesshashfunc
		}

		if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" {
			SessionHashKey = sesshashkey
		}

		if sessMaxLifeTime, err := AppConfig.Int("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
			int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64)
			SessionGCMaxLifetime = int64val
		}

		if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
			SessionCookieLifeTime = sesscookielifetime
		}

		if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil {
			UseFcgi = usefcgi
		}

		if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil {
			EnableGzip = enablegzip
		}

		if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
			DirectoryIndex = directoryindex
		}

		if hotupdate, err := AppConfig.Bool("HotUpdate"); err == nil {
			EnableHotUpdate = hotupdate
		}

		if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil {
			HttpServerTimeOut = timeout
		}

		if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil {
			ErrorsShow = errorsshow
		}

		if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil {
			CopyRequestBody = copyrequestbody
		}

		if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" {
			XSRFKEY = xsrfkey
		}

		if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil {
			EnableXSRF = enablexsrf
		}

		if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
			XSRFExpire = expire
		}

		if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" {
			TemplateLeft = tplleft
		}

		if tplright := AppConfig.String("TemplateRight"); tplright != "" {
			TemplateRight = tplright
		}

		if httptls, err := AppConfig.Bool("HttpTLS"); err == nil {
			HttpTLS = httptls
		}

254
		if certfile := AppConfig.String("HttpCertFile"); certfile != "" {
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
			HttpCertFile = certfile
		}

		if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
			HttpKeyFile = keyfile
		}

		if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
			BeegoServerName = serverName
		}

		if sd := AppConfig.String("StaticDir"); sd != "" {
			for k := range StaticDir {
				delete(StaticDir, k)
			}
			sds := strings.Fields(sd)
			for _, v := range sds {
				if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
					StaticDir["/"+url2fsmap[0]] = url2fsmap[1]
				} else {
					StaticDir["/"+url2fsmap[0]] = url2fsmap[0]
				}
			}
		}
F
Francois 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
		
		if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
			extensions := strings.Split(sgz, ",")
			if len(extensions) > 0 {
				StaticExtensionsToGzip = []string{}
				for _, ext := range extensions {
					if len(ext) == 0 {
						continue
					}
					extWithDot := ext
					if extWithDot[:1] != "." {
						extWithDot = "." + extWithDot
					}
					StaticExtensionsToGzip = append(StaticExtensionsToGzip, extWithDot)
				}
			}
		}
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

		if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
			EnableAdmin = enableadmin
		}

		if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" {
			AdminHttpAddr = adminhttpaddr
		}

		if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil {
			AdminHttpPort = adminhttpport
		}
	}
	return nil
}