优化初步使用
This commit is contained in:
+26
-21
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user