优化初步使用

This commit is contained in:
hoteas 2020-02-21 21:44:53 +08:00
parent 992876fa10
commit ab294ca6f0
3 changed files with 90 additions and 21 deletions

View File

@ -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 {

View File

@ -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"
}

View File

@ -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": "如果需要使用自动数据库配置请设置此项手动配置数据库不需要目前支持mysqlsqlite",
"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服务时配置默认服务ip127.0.0.1",
"redisPort": "如果需要使用redis服务时配置默认服务端口6379",
"redisPwd": "如果需要使用redis服务时配置默认服务密码123456",
"sessionName": "设置session的cookie名默认HOTIME",
"tlsCert": "https证书",
"tlsKey": "https密钥",
"tlsPort": "web服务https端口0为不启用https服务",
"tpt": "静态文件目录默认为程序目录下tpt目录"
}