This commit is contained in:
hoteas 2019-05-27 05:46:03 +00:00
parent a3d3155e9b
commit 374e0ab2ac
2 changed files with 42 additions and 6 deletions

View File

@ -20,6 +20,7 @@ type Application struct {
Router Router
contextBase contextBase
Port string //端口号 Port string //端口号
TLSPort string //ssl访问端口号
connectListener []func(this *Context) bool //所有的访问监听,true按原计划继续使用false表示有监听器处理 connectListener []func(this *Context) bool //所有的访问监听,true按原计划继续使用false表示有监听器处理
connectDbFunc func(err ...*Error) *sql.DB connectDbFunc func(err ...*Error) *sql.DB
configPath string configPath string
@ -71,6 +72,7 @@ func (this *Application) Run(router Router) {
//this.Port = port //this.Port = port
this.Port = this.Config.GetString("port") this.Port = this.Config.GetString("port")
this.TLSPort = this.Config.GetString("tlsPort")
if this.connectDbFunc != nil && (this.Db.DB == nil || this.Db.DB.Ping() != nil) { if this.connectDbFunc != nil && (this.Db.DB == nil || this.Db.DB.Ping() != nil) {
this.Db.SetConnect(this.connectDbFunc) this.Db.SetConnect(this.connectDbFunc)
@ -93,11 +95,39 @@ func (this *Application) Run(router Router) {
if !IsRun { if !IsRun {
IsRun = true IsRun = true
} }
App[this.Port] = this
this.Server.Handler = this ch := make(chan int)
//启动服务 go func() {
this.Server.Addr = ":" + this.Port
this.Server.ListenAndServe() if ObjToCeilInt(this.Port)!=0{
App[this.Port] = this
this.Server.Handler = this
//启动服务
this.Server.Addr = ":" + this.Port
err:=this.Server.ListenAndServe()
fmt.Println(err)
ch <-1
}
}()
go func() {
if ObjToCeilInt(this.TLSPort)!=0{
App[this.TLSPort] = this
this.Server.Handler = this
//启动服务
this.Server.Addr = ":" + this.TLSPort
err:=this.Server.ListenAndServeTLS(this.Config.GetString("tlsCert"),this.Config.GetString("tlsKey"))
fmt.Println(err)
ch <-2
}
}()
value := <- ch
fmt.Println("启动服务失败 : ", value)
} }
@ -167,6 +197,9 @@ func (this *Application) SetConfig(configPath ...string) {
this.Config.Put(k, v) //程序配置 this.Config.Put(k, v) //程序配置
Config.Put(k, v) //系统配置 Config.Put(k, v) //系统配置
} }
}else {
fmt.Println("配置文件不存在,或者配置出错,使用缺省默认配置")
} }
//else { //else {
// //文件不存在则写入文件,存在不做处理 // //文件不存在则写入文件,存在不做处理

5
var.go
View File

@ -16,11 +16,14 @@ var Config = Map{
"dbUser": "root", "dbUser": "root",
"dbPwd": "root", "dbPwd": "root",
"dbPort": "3306", "dbPort": "3306",
"port": "80", "port": "0",
"cacheShortTime": 60 * 60 * 2, "cacheShortTime": 60 * 60 * 2,
"cacheLongTime": 60 * 60 * 24 * 30, "cacheLongTime": 60 * 60 * 24 * 30,
"sessionName": "HOTIME", "sessionName": "HOTIME",
"error": Map{}, "error": Map{},
"debug":1,//debug 0关闭1开启 "debug":1,//debug 0关闭1开启
"modeRouterStrict":false,//路由严格模式/a/b/c "modeRouterStrict":false,//路由严格模式/a/b/c
"tlsPort":"0",
"tlsKey":"",
"tlsCert":"",
} }