55 "github.com/devfeel/dotweb/config"
66 "github.com/devfeel/dotweb/framework/json"
77 "github.com/devfeel/dotweb/framework/log"
8+ "github.com/devfeel/dotweb/servers"
89 "github.com/devfeel/dotweb/session"
910 "net/http"
1011 _ "net/http/pprof"
@@ -19,8 +20,8 @@ import (
1920type (
2021 DotWeb struct {
2122 HttpServer * HttpServer
23+ OfflineServer servers.Server
2224 AppConfig * config.AppConfig
23- SessionConfig * session.StoreConfig
2425 Modules []* HttpModule
2526 logpath string
2627 ExceptionHandler ExceptionHandle
@@ -44,9 +45,10 @@ const (
4445 */
4546func New () * DotWeb {
4647 app := & DotWeb {
47- HttpServer : NewHttpServer (),
48- Modules : make ([]* HttpModule , 0 , 10 ),
49- AppContext : NewItemContext (),
48+ HttpServer : NewHttpServer (),
49+ OfflineServer : servers .NewOfflineServer (),
50+ Modules : make ([]* HttpModule , 0 , 10 ),
51+ AppContext : NewItemContext (),
5052 }
5153 app .HttpServer .setDotApp (app )
5254 return app
@@ -130,7 +132,7 @@ func (ds *DotWeb) SetEnabledDebug(isEnabled bool) {
130132设置是否启用Session,默认为false
131133*/
132134func (ds * DotWeb ) SetEnabledSession (isEnabled bool ) {
133- ds .HttpServer .ServerConfig .EnabledSession = isEnabled
135+ ds .HttpServer .SessionConfig .EnabledSession = isEnabled
134136}
135137
136138/*
@@ -141,8 +143,12 @@ func (ds *DotWeb) SetEnabledGzip(isEnabled bool) {
141143}
142144
143145//set session store config
144- func (ds * DotWeb ) SetSessionConfig (config * session.StoreConfig ) {
145- ds .SessionConfig = config
146+ func (ds * DotWeb ) SetSessionConfig (storeConfig * session.StoreConfig ) {
147+ ds .HttpServer .SessionConfig .Timeout = storeConfig .Maxlifetime
148+ ds .HttpServer .SessionConfig .SessionMode = storeConfig .StoreName
149+ ds .HttpServer .SessionConfig .ServerIP = storeConfig .ServerIP
150+ ds .HttpServer .SessionConfig .UserName = storeConfig .UserName
151+ ds .HttpServer .SessionConfig .Password = storeConfig .Password
146152}
147153
148154/*
@@ -178,21 +184,22 @@ func (ds *DotWeb) StartServer(httpport int) error {
178184
179185 //添加框架默认路由规则
180186 //默认支持pprof信息查看
181- ds .HttpServer .GET ("/dotweb/debug/pprof/:key" , initPProf )
182- ds .HttpServer .GET ("/dotweb/debug/freemem" , freeMemory )
183- ds .HttpServer .GET ("/dotweb/state" , showServerState )
184- ds .HttpServer .GET ("/dotweb/query/:key" , showQuery )
187+ ds .HttpServer .Router (). GET ("/dotweb/debug/pprof/:key" , initPProf )
188+ ds .HttpServer .Router (). GET ("/dotweb/debug/freemem" , freeMemory )
189+ ds .HttpServer .Router (). GET ("/dotweb/state" , showServerState )
190+ ds .HttpServer .Router (). GET ("/dotweb/query/:key" , showQuery )
185191
186192 if ds .ExceptionHandler == nil {
187193 ds .SetExceptionHandle (ds .DefaultHTTPErrorHandler )
188194 }
189195
190196 //init session manager
191- if ds .HttpServer .ServerConfig .EnabledSession {
192- if ds .SessionConfig == nil {
193- panic ("no set SessionConfig, but set enabledsession true" )
197+ if ds .HttpServer .SessionConfig .EnabledSession {
198+ if ds .HttpServer .sessionManager == nil {
199+ //panic("no set SessionConfig, but set enabledsession true")
200+ logger .Warn ("no set SessionConfig, but set enabledsession true, now will use default runtime session" , LogTarget_HttpServer )
194201 }
195- ds .HttpServer .InitSessionManager (ds . SessionConfig )
202+ ds .HttpServer .InitSessionManager (session . NewDefaultRuntimeConfig () )
196203 }
197204
198205 port := ":" + strconv .Itoa (httpport )
@@ -211,17 +218,21 @@ func (ds *DotWeb) StartServerWithConfig(config *config.AppConfig) error {
211218 ds .SetEnabledGzip (config .Server .EnabledGzip )
212219
213220 //设置维护
214- ds .HttpServer .setOffline (config .Server .Offline , config .Server .OfflineText , config .Server .OfflineUrl )
221+ if config .Server .Offline {
222+ ds .HttpServer .SetOffline (config .Server .Offline , config .Server .OfflineText , config .Server .OfflineUrl )
223+ ds .OfflineServer .SetOffline (config .Server .Offline , config .Server .OfflineText , config .Server .OfflineUrl )
224+ }
215225
226+ //设置session
216227 if config .Session .EnabledSession {
217228 ds .SetEnabledSession (config .Session .EnabledSession )
218229 ds .SetSessionConfig (session .NewStoreConfig (config .Session .SessionMode , config .Session .Timeout , config .Session .ServerIP , config .Session .UserName , config .Session .Password ))
219230 }
220231
221232 //load router and register
222233 for _ , v := range config .Routers {
223- if h , isok := ds .HttpServer .GetHandler (v .HandlerName ); isok && v .IsUse {
224- ds .HttpServer .RegisterRoute (strings .ToUpper (v .Method ), v .Path , h )
234+ if h , isok := ds .HttpServer .Router (). GetHandler (v .HandlerName ); isok && v .IsUse {
235+ ds .HttpServer .Router (). RegisterRoute (strings .ToUpper (v .Method ), v .Path , h )
225236 }
226237 }
227238
0 commit comments