Skip to content

Commit

Permalink
Version 0.3.6.5
Browse files Browse the repository at this point in the history
* App、Server、Session配置代码梳理优化
* 新增Server接口,新增OfflineServer实现
* 新增Router接口,将原HttpServer路由操作转移至HttpServer.Router()
* 2017-03-24 14:00
  • Loading branch information
devfeel committed Mar 24, 2017
1 parent cce9149 commit 8718777
Show file tree
Hide file tree
Showing 20 changed files with 355 additions and 1,392 deletions.
17 changes: 15 additions & 2 deletions config/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ type (
Session SessionConfig `xml:"session"`
Routers []RouterConfig `xml:"routers>router"`
}

ServerConfig struct {
LogPath string `xml:"logpath,attr"` //文件方式日志目录,如果为空,默认当前目录
EnabledDebug bool `xml:"enableddebug,attr"` //启用Debug模式
Port int `xml:"port,attr"` //端口
Offline bool `xml:"offline,attr"` //是否维护,默认false
OfflineText string `xml:"offlinetext,attr"` //当设置为维护,默认显示内容,如果设置url,优先url
OfflineUrl string `xml:"offlineurl,attr"` //当设置为维护,默认维护页地址,如果设置url,优先url
EnabledDebug bool `xml:"enableddebug,attr"` //启用Debug模式
EnabledGzip bool `xml:"enabledgzip,attr"` //启用gzip
}

Expand All @@ -41,6 +40,20 @@ type (
}
)

func NewAppConfig() *AppConfig {
config := &AppConfig{}
return config
}
func NewServerConfig() *ServerConfig {
config := &ServerConfig{}
return config
}

func NewSessionConfig() *SessionConfig {
config := &SessionConfig{}
return config
}

//初始化配置文件
func InitConfig(configFile string) *AppConfig {
content, err := ioutil.ReadFile(configFile)
Expand Down
19 changes: 11 additions & 8 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"fmt"
"github.com/devfeel/dotweb/router"
"github.com/devfeel/dotweb/routers"
"github.com/devfeel/dotweb/session"
)

Expand All @@ -19,7 +19,7 @@ const (

type HttpContext struct {
Request *http.Request
RouterParams router.Params
RouterParams routers.Params
Response *Response
WebSocket *WebSocket
HijackConn *HijackConn
Expand All @@ -33,7 +33,7 @@ type HttpContext struct {
}

//reset response attr
func (ctx *HttpContext) Reset(res *Response, r *http.Request, server *HttpServer, params router.Params) {
func (ctx *HttpContext) Reset(res *Response, r *http.Request, server *HttpServer, params routers.Params) {
ctx.Request = r
ctx.Response = res
ctx.RouterParams = params
Expand Down Expand Up @@ -77,7 +77,7 @@ func (ctx *HttpContext) Session() (session *session.SessionState) {
//return nil, errors.New("no effective http-server")
panic("no effective http-server")
}
if !ctx.HttpServer.ServerConfig.EnabledSession {
if !ctx.HttpServer.SessionConfig.EnabledSession {
//return nil, errors.New("http-server not enabled session")
panic("http-server not enabled session")
}
Expand Down Expand Up @@ -306,13 +306,16 @@ func (ctx *HttpContext) SetStatusCode(code int) error {
return ctx.Response.WriteHeader(code)
}

// write cookie for domain&name&liveseconds
// write cookie for domain & name & maxAge
//
// default path = "/"
// default domain = current domain
// default seconds = 0
func (ctx *HttpContext) WriteCookie(name, value string, seconds int) {
cookie := http.Cookie{Name: name, Value: value, MaxAge: seconds}
// default maxAge = 0 //seconds
// seconds=0 means no 'Max-Age' attribute specified.
// seconds<0 means delete cookie now, equivalently 'Max-Age: 0'
// seconds>0 means Max-Age attribute present and given in seconds
func (ctx *HttpContext) WriteCookie(name, value string, maxAge int) {
cookie := http.Cookie{Name: name, Value: value, MaxAge: maxAge}
http.SetCookie(ctx.Response.Writer(), &cookie)
}

Expand Down
47 changes: 29 additions & 18 deletions dotweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/devfeel/dotweb/config"
"github.com/devfeel/dotweb/framework/json"
"github.com/devfeel/dotweb/framework/log"
"github.com/devfeel/dotweb/servers"
"github.com/devfeel/dotweb/session"
"net/http"
_ "net/http/pprof"
Expand All @@ -19,8 +20,8 @@ import (
type (
DotWeb struct {
HttpServer *HttpServer
OfflineServer servers.Server
AppConfig *config.AppConfig
SessionConfig *session.StoreConfig
Modules []*HttpModule
logpath string
ExceptionHandler ExceptionHandle
Expand All @@ -44,9 +45,10 @@ const (
*/
func New() *DotWeb {
app := &DotWeb{
HttpServer: NewHttpServer(),
Modules: make([]*HttpModule, 0, 10),
AppContext: NewItemContext(),
HttpServer: NewHttpServer(),
OfflineServer: servers.NewOfflineServer(),
Modules: make([]*HttpModule, 0, 10),
AppContext: NewItemContext(),
}
app.HttpServer.setDotApp(app)
return app
Expand Down Expand Up @@ -130,7 +132,7 @@ func (ds *DotWeb) SetEnabledDebug(isEnabled bool) {
设置是否启用Session,默认为false
*/
func (ds *DotWeb) SetEnabledSession(isEnabled bool) {
ds.HttpServer.ServerConfig.EnabledSession = isEnabled
ds.HttpServer.SessionConfig.EnabledSession = isEnabled
}

/*
Expand All @@ -141,8 +143,12 @@ func (ds *DotWeb) SetEnabledGzip(isEnabled bool) {
}

//set session store config
func (ds *DotWeb) SetSessionConfig(config *session.StoreConfig) {
ds.SessionConfig = config
func (ds *DotWeb) SetSessionConfig(storeConfig *session.StoreConfig) {
ds.HttpServer.SessionConfig.Timeout = storeConfig.Maxlifetime
ds.HttpServer.SessionConfig.SessionMode = storeConfig.StoreName
ds.HttpServer.SessionConfig.ServerIP = storeConfig.ServerIP
ds.HttpServer.SessionConfig.UserName = storeConfig.UserName
ds.HttpServer.SessionConfig.Password = storeConfig.Password
}

/*
Expand Down Expand Up @@ -178,21 +184,22 @@ func (ds *DotWeb) StartServer(httpport int) error {

//添加框架默认路由规则
//默认支持pprof信息查看
ds.HttpServer.GET("/dotweb/debug/pprof/:key", initPProf)
ds.HttpServer.GET("/dotweb/debug/freemem", freeMemory)
ds.HttpServer.GET("/dotweb/state", showServerState)
ds.HttpServer.GET("/dotweb/query/:key", showQuery)
ds.HttpServer.Router().GET("/dotweb/debug/pprof/:key", initPProf)
ds.HttpServer.Router().GET("/dotweb/debug/freemem", freeMemory)
ds.HttpServer.Router().GET("/dotweb/state", showServerState)
ds.HttpServer.Router().GET("/dotweb/query/:key", showQuery)

if ds.ExceptionHandler == nil {
ds.SetExceptionHandle(ds.DefaultHTTPErrorHandler)
}

//init session manager
if ds.HttpServer.ServerConfig.EnabledSession {
if ds.SessionConfig == nil {
panic("no set SessionConfig, but set enabledsession true")
if ds.HttpServer.SessionConfig.EnabledSession {
if ds.HttpServer.sessionManager == nil {
//panic("no set SessionConfig, but set enabledsession true")
logger.Warn("no set SessionConfig, but set enabledsession true, now will use default runtime session", LogTarget_HttpServer)
}
ds.HttpServer.InitSessionManager(ds.SessionConfig)
ds.HttpServer.InitSessionManager(session.NewDefaultRuntimeConfig())
}

port := ":" + strconv.Itoa(httpport)
Expand All @@ -211,17 +218,21 @@ func (ds *DotWeb) StartServerWithConfig(config *config.AppConfig) error {
ds.SetEnabledGzip(config.Server.EnabledGzip)

//设置维护
ds.HttpServer.setOffline(config.Server.Offline, config.Server.OfflineText, config.Server.OfflineUrl)
if config.Server.Offline {
ds.HttpServer.SetOffline(config.Server.Offline, config.Server.OfflineText, config.Server.OfflineUrl)
ds.OfflineServer.SetOffline(config.Server.Offline, config.Server.OfflineText, config.Server.OfflineUrl)
}

//设置session
if config.Session.EnabledSession {
ds.SetEnabledSession(config.Session.EnabledSession)
ds.SetSessionConfig(session.NewStoreConfig(config.Session.SessionMode, config.Session.Timeout, config.Session.ServerIP, config.Session.UserName, config.Session.Password))
}

//load router and register
for _, v := range config.Routers {
if h, isok := ds.HttpServer.GetHandler(v.HandlerName); isok && v.IsUse {
ds.HttpServer.RegisterRoute(strings.ToUpper(v.Method), v.Path, h)
if h, isok := ds.HttpServer.Router().GetHandler(v.HandlerName); isok && v.IsUse {
ds.HttpServer.Router().RegisterRoute(strings.ToUpper(v.Method), v.Path, h)
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/appcontext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func Index(ctx *dotweb.HttpContext) {
}

func InitRoute(server *dotweb.HttpServer) {
server.GET("/", Index)
server.Router().GET("/", Index)
}
2 changes: 1 addition & 1 deletion example/bind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ func TestBind(ctx *dotweb.HttpContext) {
}

func InitRoute(server *dotweb.HttpServer) {
server.POST("/", TestBind)
server.Router().POST("/", TestBind)
}
6 changes: 3 additions & 3 deletions example/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Redirect(ctx *dotweb.HttpContext) {
}

func RegisterHandler(server *dotweb.HttpServer) {
server.RegisterHandler("Index", Index)
server.RegisterHandler("DefaultError", DefaultError)
server.RegisterHandler("Redirect", Redirect)
server.Router().RegisterHandler("Index", Index)
server.Router().RegisterHandler("DefaultError", DefaultError)
server.Router().RegisterHandler("Redirect", Redirect)
}
2 changes: 1 addition & 1 deletion example/httpmodule/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Index(ctx *dotweb.HttpContext) {
}

func InitRoute(server *dotweb.HttpServer) {
server.GET("/", Index)
server.Router().GET("/", Index)
}

func InitModule(dotserver *dotweb.DotWeb) {
Expand Down
12 changes: 6 additions & 6 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func Redirect(ctx *dotweb.HttpContext) {
}

func InitRoute(server *dotweb.HttpServer) {
server.GET("/", Index)
server.POST("/keypost", KeyPost)
server.POST("/jsonpost", JsonPost)
server.GET("/error", DefaultError)
server.GET("/redirect", Redirect)
server.RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
server.Router().GET("/", Index)
server.Router().POST("/keypost", KeyPost)
server.Router().POST("/jsonpost", JsonPost)
server.Router().GET("/error", DefaultError)
server.Router().GET("/redirect", Redirect)
server.Router().RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
}

func InitModule(dotserver *dotweb.DotWeb) {
Expand Down
2 changes: 1 addition & 1 deletion example/session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ func TestSession(ctx *dotweb.HttpContext) {
}

func InitRoute(server *dotweb.HttpServer) {
server.GET("/", TestSession)
server.Router().GET("/", TestSession)
}
2 changes: 1 addition & 1 deletion example/uploadfile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
}

func InitRoute(server *dotweb.HttpServer) {
server.POST("/file", FileUpload)
server.Router().POST("/file", FileUpload)
}

func FileUpload(ctx *dotweb.HttpContext) {
Expand Down
Loading

0 comments on commit 8718777

Please sign in to comment.