Skip to content

Commit

Permalink
Merge pull request #34 from maxzhang1985/dev
Browse files Browse the repository at this point in the history
set all router and mvc settings for application builder.
  • Loading branch information
yoyofx authored Jan 9, 2020
2 parents 742d9b0 + ab8bee4 commit be18aaa
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
8 changes: 4 additions & 4 deletions Examples/SimpleWeb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}).
Expand Down
41 changes: 33 additions & 8 deletions Framework/ApplicationBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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{
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions Framework/HostBuildContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 8 additions & 12 deletions Framework/HostBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
}

```

0 comments on commit be18aaa

Please sign in to comment.