From 421d8e89d9a0873a90c681a08ec3be9480eaccd0 Mon Sep 17 00:00:00 2001 From: maxzhang Date: Thu, 9 Jan 2020 15:26:52 +0800 Subject: [PATCH 1/2] set all router and mvc settings for application builder. --- Examples/SimpleWeb/main.go | 8 +++---- Framework/ApplicationBuilder.go | 41 ++++++++++++++++++++++++++------- Framework/HostBuildContext.go | 9 ++++---- Framework/HostBuilder.go | 20 +++++++--------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/Examples/SimpleWeb/main.go b/Examples/SimpleWeb/main.go index 994d4390..19451014 100644 --- a/Examples/SimpleWeb/main.go +++ b/Examples/SimpleWeb/main.go @@ -25,13 +25,13 @@ func CreateCustomBuilder() *YoyoGo.HostBuilder { //UseServer(YoyoGo.DefaultHttps(":8080", "./Certificate/server.pem", "./Certificate/server.key")). Configure(func(app *YoyoGo.ApplicationBuilder) { //app.SetEnvironment(Context.Prod) - app.UseMvc() app.UseStatic("Static") + app.UseMvc() + app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) { + builder.AddController(contollers.NewUserController) + }) }). UseEndpoints(registerEndpointRouterConfig). - ConfigureMvcParts(func(builder *Controller.ControllerBuilder) { - builder.AddController(contollers.NewUserController) - }). ConfigureServices(func(serviceCollection *DependencyInjection.ServiceCollection) { serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction)) }). diff --git a/Framework/ApplicationBuilder.go b/Framework/ApplicationBuilder.go index e6faf449..e109f10d 100644 --- a/Framework/ApplicationBuilder.go +++ b/Framework/ApplicationBuilder.go @@ -2,6 +2,8 @@ package YoyoGo import ( "github.com/maxzhang1985/yoyogo/Context" + "github.com/maxzhang1985/yoyogo/Controller" + "github.com/maxzhang1985/yoyogo/DependencyInjection" "github.com/maxzhang1985/yoyogo/Middleware" "github.com/maxzhang1985/yoyogo/Router" "net/http" @@ -19,6 +21,8 @@ type ApplicationBuilder struct { routerBuilder Router.IRouterBuilder middleware middleware handlers []Handler + Profile string + mvcConfigures []func(builder *Controller.ControllerBuilder) } // create classic application builder @@ -37,14 +41,13 @@ func CreateDefaultBuilder(routerConfig func(router Router.IRouterBuilder)) *Host } // create new application builder -func NewApplicationBuilder(context *HostBuildContext) *ApplicationBuilder { +func NewApplicationBuilder() *ApplicationBuilder { routerBuilder := Router.NewRouterBuilder() recovery := Middleware.NewRecovery() logger := Middleware.NewLogger() router := Middleware.NewRouter(routerBuilder) self := New(logger, recovery, router) self.routerBuilder = routerBuilder - self.hostContext = context return self } @@ -54,6 +57,24 @@ func (self *ApplicationBuilder) UseMvc() *ApplicationBuilder { return self } +func (this *ApplicationBuilder) SetHostBuildContext(context *HostBuildContext) { + this.hostContext = context +} + +func (this *ApplicationBuilder) ConfigureMvcParts(configure func(builder *Controller.ControllerBuilder)) *ApplicationBuilder { + this.mvcConfigures = append(this.mvcConfigures, configure) + return this +} + +func (this *ApplicationBuilder) buildMvc(services *DependencyInjection.ServiceCollection) { + if this.routerBuilder.IsMvc() { + controllerBuilder := Controller.NewControllerBuilder(services) + for _, configure := range this.mvcConfigures { + configure(controllerBuilder) + } + } +} + // create application builder when combo all handlers to middleware func New(handlers ...Handler) *ApplicationBuilder { return &ApplicationBuilder{ @@ -67,19 +88,23 @@ func (app *ApplicationBuilder) UseMiddleware(handler Handler) { if handler == nil { panic("handler cannot be nil") } - app.handlers = append(app.handlers, handler) - //n.middleware = build(n.handlers) } // build and combo all middleware to request delegate (ServeHTTP(w http.ResponseWriter, r *http.Request)) -func (app *ApplicationBuilder) Build() IRequestDelegate { - app.middleware = build(app.handlers) - return app +func (this *ApplicationBuilder) Build() IRequestDelegate { + if this.hostContext == nil { + panic("hostContext is nil! please set.") + } + + this.hostContext.hostingEnvironment.Profile = this.Profile + this.middleware = build(this.handlers) + this.buildMvc(this.hostContext.applicationServicesDef) + return this } func (app *ApplicationBuilder) SetEnvironment(mode string) { - app.hostContext.hostingEnvironment.Profile = mode + app.Profile = mode } // apply static middleware in builder diff --git a/Framework/HostBuildContext.go b/Framework/HostBuildContext.go index 472ca3a1..1ffe8b68 100644 --- a/Framework/HostBuildContext.go +++ b/Framework/HostBuildContext.go @@ -6,8 +6,9 @@ import ( ) type HostBuildContext struct { - RequestDelegate IRequestDelegate - ApplicationCycle *ApplicationLife - hostingEnvironment *Context.HostEnvironment - applicationServices DependencyInjection.IServiceProvider + RequestDelegate IRequestDelegate + ApplicationCycle *ApplicationLife + hostingEnvironment *Context.HostEnvironment + applicationServicesDef *DependencyInjection.ServiceCollection + applicationServices DependencyInjection.IServiceProvider } diff --git a/Framework/HostBuilder.go b/Framework/HostBuilder.go index 4bbccdd3..2b397110 100644 --- a/Framework/HostBuilder.go +++ b/Framework/HostBuilder.go @@ -33,10 +33,10 @@ func (self *HostBuilder) ConfigureServices(configure func(*DependencyInjection.S return self } -func (self *HostBuilder) ConfigureMvcParts(configure func(builder *Controller.ControllerBuilder)) *HostBuilder { - self.mvcconfigures = append(self.mvcconfigures, configure) - return self -} +//func (self *HostBuilder) ConfigureMvcParts(configure func(builder *Controller.ControllerBuilder)) *HostBuilder { +// self.mvcconfigures = append(self.mvcconfigures, configure) +// return self +//} func (self *HostBuilder) OnApplicationLifeEvent(lifeConfigure func(*ApplicationLife)) *HostBuilder { self.lifeConfigure = lifeConfigure @@ -95,14 +95,7 @@ func (self *HostBuilder) Build() WebHost { configure(services) } - controllerBuilder := Controller.NewControllerBuilder(services) - for _, configure := range self.mvcconfigures { - configure(controllerBuilder) - } - - self.context.applicationServices = services.Build() //serviceProvider - - applicationBuilder := NewApplicationBuilder(self.context) + applicationBuilder := NewApplicationBuilder() for _, configure := range self.configures { configure(applicationBuilder) @@ -112,7 +105,10 @@ func (self *HostBuilder) Build() WebHost { configure(applicationBuilder.routerBuilder) } + self.context.applicationServicesDef = services + applicationBuilder.SetHostBuildContext(self.context) self.context.RequestDelegate = applicationBuilder.Build() // ServeHTTP(w http.ResponseWriter, r *http.Request) + self.context.applicationServices = services.Build() //serviceProvider go self.lifeConfigure(self.context.ApplicationCycle) return NewWebHost(self.server, self.context) From ab8bee4e7ceab32851beebadc9fd24a82d39e408 Mon Sep 17 00:00:00 2001 From: maxzhang Date: Thu, 9 Jan 2020 15:35:03 +0800 Subject: [PATCH 2/2] readme --- README.md | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3f1edbd4..0174d374 100644 --- a/README.md +++ b/README.md @@ -126,15 +126,16 @@ func main() { func CreateCustomBuilder() *YoyoGo.HostBuilder { return YoyoGo.NewWebHostBuilder(). UseFastHttp(). - //UseServer(YoyoGo.DefaultHttps(":8080", "./Certificate/server.pem", "./Certificate/server.key")). Configure(func(app *YoyoGo.ApplicationBuilder) { - // app.SetEnvironment(Context.Prod) - app.UseMvc() + //app.SetEnvironment(Context.Prod) app.UseStatic("Static") + app.UseMvc() + app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) { + builder.AddController(contollers.NewUserController) + }) }). - UseEndpoints(registerEndpoints). + UseEndpoints(registerEndpointRouterConfig). ConfigureServices(func(serviceCollection *DependencyInjection.ServiceCollection) { - serviceCollection.AddSingletonByNameAndImplements("usercontroller", contollers.NewUserController, new(Controller.IController)) serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction)) }). OnApplicationLifeEvent(getApplicationLifeEvent) @@ -208,27 +209,30 @@ func fireApplicationLifeEvent(life *YoyoGo.ApplicationLife) { // Mvc type UserController struct { *Controller.ApiController - Name string - // + userAction models.IUserAction // IOC } -func NewUserController() *UserController { - return &UserController{Name: "www"} +// ctor for ioc +func NewUserController(userAction models.IUserAction) *UserController { + return &UserController{userAction: userAction} } +// reuqest param binder type RegiserRequest struct { Controller.RequestParam UserName string `param:"username"` Password string `param:"password"` } -func (p *UserController) Register(ctx *Context.HttpContext, request *RegiserRequest) Controller.ApiResult { +// auto bind action param by ioc +func (this *UserController) Register(ctx *Context.HttpContext, request *RegiserRequest) ActionResult.IActionResult { result := Controller.ApiResult{Success: true, Message: "ok", Data: request} - return result + return ActionResult.Json{Data: result} } -func (p *UserController) GetInfo() Controller.ApiResult { - return Controller.ApiResult{Success: true, Message: "ok"} +// use userAction interface by ioc +func (this *UserController) GetInfo() Controller.ApiResult { + return this.OK(this.userAction.Login("zhang")) } ``` \ No newline at end of file