diff --git a/application.go b/application.go index 6a1b246..e66e43d 100644 --- a/application.go +++ b/application.go @@ -41,6 +41,25 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, req *http.Request) { //启动实例 func (this *Application) Run(router Router) { + //如果没有设置配置自动生成配置 + if this.configPath == "" || len(this.Config) == 0 { + this.SetConfig() + } + + //防止手动设置缓存误伤 + if this.CacheIns == nil { + this.SetCache(CacheIns(&CacheMemory{})) + } + + //防止手动设置session误伤 + if this.sessionShort == nil && this.sessionLong == nil { + if this.connectDbFunc == nil { + this.SetSession(CacheIns(&CacheMemory{}), nil) + } else { + this.SetSession(CacheIns(&CacheMemory{}), CacheIns(&CacheDb{Db: &this.Db, Time: this.Config.GetInt64("cacheLongTime")})) + } + + } this.Router = router //重新设置MethodRouter//直达路由 @@ -170,20 +189,6 @@ func (this *Application) SetDefault(connect func(err ...*Error) *sql.DB) { this.Db.SetConnect(this.connectDbFunc) } - //防止手动设置缓存误伤 - if this.CacheIns == nil { - this.SetCache(CacheIns(&CacheMemory{})) - } - //防止手动设置session误伤 - if this.sessionShort == nil && this.sessionLong == nil { - if this.connectDbFunc == nil { - this.SetSession(CacheIns(&CacheMemory{}), nil) - } else { - this.SetSession(CacheIns(&CacheMemory{}), CacheIns(&CacheDb{Db: &this.Db, Time: this.Config.GetInt64("cacheLongTime")})) - } - - } - } //设置配置文件路径全路径或者相对路径 @@ -330,7 +335,7 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) { context.HandlerStr, context.RouterString = this.urlSer(context.HandlerStr) //跨域设置 - CrossDomain(&context) + this.crossDomain(&context) //是否展示日志 if this.Config.GetInt("connectLogShow") != 0 { log.Println(context.HandlerStr + time.Now().Format(" 2006-01-02 15:04 ") + Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":"))) @@ -417,31 +422,31 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) { } -func CrossDomain(this *Context) { +func (this *Application) crossDomain(context *Context) { //没有跨域设置 - if this.Config.GetString("crossDomain") == "" { + if context.Config.GetString("crossDomain") == "" { return } - header := this.Resp.Header() + header := context.Resp.Header() header.Set("Access-Control-Allow-Origin", "*") header.Set("Access-Control-Allow-Methods", "*") header.Set("Access-Control-Allow-Credentials", "true") header.Set("Access-Control-Expose-Headers", "*") header.Set("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Access-Token") - if this.Config.GetString("crossDomain") != "*" { + if context.Config.GetString("crossDomain") != "*" { header.Set("Access-Control-Allow-Origin", this.Config.GetString("crossDomain")) return } - origin := this.Req.Header.Get("Origin") + origin := context.Req.Header.Get("Origin") if origin != "" { header.Set("Access-Control-Allow-Origin", origin) return } - refer := this.Req.Header.Get("Referer") + refer := context.Req.Header.Get("Referer") if refer != "" { tempInt := 0 lastInt := strings.IndexFunc(refer, func(r rune) bool { diff --git a/example/config/config.json b/example/config/config.json new file mode 100644 index 0000000..8eb4714 --- /dev/null +++ b/example/config/config.json @@ -0,0 +1,30 @@ +{ + "cacheLongTime": 2592000, + "cacheShortTime": 7200, + "dbCached": 0, + "dbHost": "127.0.0.1", + "dbName": "test", + "dbPort": "3306", + "dbPwd": "root", + "dbUser": "root", + "debug": 1, + "defFile": [ + "index.html", + "index.htm" + ], + "error": { + "1": "内部系统异常", + "2": "访问权限异常", + "3": "请求参数异常", + "4": "数据处理异常", + "5": "数据结果异常" + }, + "logLevel": 1, + "modeRouterStrict": false, + "port": "0", + "sessionName": "HOTIME", + "tlsCert": "", + "tlsKey": "", + "tlsPort": "0", + "tpt": "tpt" +} \ No newline at end of file diff --git a/example/config/confignote.json b/example/config/confignote.json new file mode 100644 index 0000000..baa64db --- /dev/null +++ b/example/config/confignote.json @@ -0,0 +1,34 @@ +{ + "cacheLongTime": "两级缓存,长缓存存储时间60 * 60 * 24 * 30,一般为数据库或者redis缓存", + "cacheShortTime": "两级缓存,短缓存存储时间60 * 60 * 2,一般为内存缓存", + "connectLogShow": "如果需要web访问链接、访问ip、访问时间打印,0为关闭其他数字开启此功能", + "crossDomain": "如果需要跨域设置,空字符串为不开启,*为开启所有网站允许跨域,http://www.baidu.com为指定域允许跨域", + "dbCached": "是否开启数据库缓存0为关闭,其他开启", + "dbHost": "数据库ip地址默认127.0.0.1", + "dbName": "数据库名称,sqlite为文件路径比如a/x.db", + "dbPort": "数据库端口", + "dbPwd": "数据库密码", + "dbType": "如果需要使用自动数据库配置,请设置此项,手动配置数据库不需要,目前支持mysql,sqlite", + "dbUser": "数据库用户名", + "debug": "是否开启debug模式,0关闭,其他开启,debug模式下日志展示更全", + "defFile": "默认访问文件,默认访问index.html或者index.htm文件", + "error": { + "1": "内部系统异常,在环境配置,文件访问权限等基础运行环境条件不足造成严重错误时使用", + "2": "访问权限异常,没有登录或者登录异常等时候使用", + "3": "请求参数异常,request参数不满足要求,比如参数不足,参数类型错误,参数不满足要求等时候使用", + "4": "数据处理异常,数据库操作或者三方请求返回的结果非正常结果,比如数据库突然中断等时候使用", + "5": "数据结果异常,一般用于无法给出response要求的格式要求下使用,比如response需要的是string格式但你只能提供int数据时", + "注释": "web服务内置错误提示,自定义异常建议10开始" + }, + "logLevel": "日志等级,0打印,1关闭,2,记录到文件", + "modeRouterStrict": "路由严格模式false,为大小写忽略必须匹配,true必须大小写匹配", + "port": "web服务开启Http端口,0为不启用http服务", + "redisHost": "如果需要使用redis服务时配置,默认服务ip:127.0.0.1", + "redisPort": "如果需要使用redis服务时配置,默认服务端口:6379", + "redisPwd": "如果需要使用redis服务时配置,默认服务密码:123456", + "sessionName": "设置session的cookie名默认HOTIME", + "tlsCert": "https证书", + "tlsKey": "https密钥", + "tlsPort": "web服务https端口,0为不启用https服务", + "tpt": "静态文件目录,默认为程序目录下tpt目录" +} \ No newline at end of file