Skip to content

Commit

Permalink
Merge pull request #35 from maxzhang1985/dev
Browse files Browse the repository at this point in the history
v2 ,set all router and mvc settings for application builder.
  • Loading branch information
yoyofx authored Jan 10, 2020
2 parents be18aaa + f830f77 commit cb70cc0
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Examples/SimpleWeb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func CreateCustomBuilder() *YoyoGo.HostBuilder {
//app.SetEnvironment(Context.Prod)
app.UseStatic("Static")
app.UseMvc()
app.UseEndpoints(registerEndpointRouterConfig)
app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) {
builder.AddController(contollers.NewUserController)
})
}).
UseEndpoints(registerEndpointRouterConfig).
ConfigureServices(func(serviceCollection *DependencyInjection.ServiceCollection) {
serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction))
}).
Expand Down
65 changes: 39 additions & 26 deletions Framework/ApplicationBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ const (

//application builder struct
type ApplicationBuilder struct {
hostContext *HostBuildContext
routerBuilder Router.IRouterBuilder
middleware middleware
handlers []Handler
Profile string
mvcConfigures []func(builder *Controller.ControllerBuilder)
hostContext *HostBuildContext
routerBuilder Router.IRouterBuilder
middleware middleware
handlers []Handler
Profile string
routeConfigures []func(Router.IRouterBuilder)
mvcConfigures []func(builder *Controller.ControllerBuilder)
}

// create classic application builder
Expand All @@ -36,8 +37,16 @@ func CreateDefaultBuilder(routerConfig func(router Router.IRouterBuilder)) *Host
UseServer(DefaultHttpServer(DefaultAddress)).
Configure(func(app *ApplicationBuilder) {
app.UseStatic("Static")
}).
UseEndpoints(routerConfig)
app.UseEndpoints(routerConfig)
})
}

// create application builder when combo all handlers to middleware
func New(handlers ...Handler) *ApplicationBuilder {
return &ApplicationBuilder{
handlers: handlers,
middleware: build(handlers),
}
}

// create new application builder
Expand All @@ -57,15 +66,22 @@ func (self *ApplicationBuilder) UseMvc() *ApplicationBuilder {
return self
}

func (this *ApplicationBuilder) SetHostBuildContext(context *HostBuildContext) {
this.hostContext = context
func (self *ApplicationBuilder) UseEndpoints(configure func(Router.IRouterBuilder)) *ApplicationBuilder {
self.routeConfigures = append(self.routeConfigures, configure)
return self
}

func (this *ApplicationBuilder) ConfigureMvcParts(configure func(builder *Controller.ControllerBuilder)) *ApplicationBuilder {
this.mvcConfigures = append(this.mvcConfigures, configure)
return this
}

func (this *ApplicationBuilder) buildEndPoints() {
for _, configure := range this.routeConfigures {
configure(this.routerBuilder)
}
}

func (this *ApplicationBuilder) buildMvc(services *DependencyInjection.ServiceCollection) {
if this.routerBuilder.IsMvc() {
controllerBuilder := Controller.NewControllerBuilder(services)
Expand All @@ -75,22 +91,6 @@ func (this *ApplicationBuilder) buildMvc(services *DependencyInjection.ServiceCo
}
}

// create application builder when combo all handlers to middleware
func New(handlers ...Handler) *ApplicationBuilder {
return &ApplicationBuilder{
handlers: handlers,
middleware: build(handlers),
}
}

// apply middleware in builder
func (app *ApplicationBuilder) UseMiddleware(handler Handler) {
if handler == nil {
panic("handler cannot be nil")
}
app.handlers = append(app.handlers, handler)
}

// build and combo all middleware to request delegate (ServeHTTP(w http.ResponseWriter, r *http.Request))
func (this *ApplicationBuilder) Build() IRequestDelegate {
if this.hostContext == nil {
Expand All @@ -99,14 +99,27 @@ func (this *ApplicationBuilder) Build() IRequestDelegate {

this.hostContext.hostingEnvironment.Profile = this.Profile
this.middleware = build(this.handlers)
this.buildEndPoints()
this.buildMvc(this.hostContext.applicationServicesDef)
return this
}

func (this *ApplicationBuilder) SetHostBuildContext(context *HostBuildContext) {
this.hostContext = context
}

func (app *ApplicationBuilder) SetEnvironment(mode string) {
app.Profile = mode
}

// apply middleware in builder
func (app *ApplicationBuilder) UseMiddleware(handler Handler) {
if handler == nil {
panic("handler cannot be nil")
}
app.handlers = append(app.handlers, handler)
}

// apply static middleware in builder
func (app *ApplicationBuilder) UseStatic(path string) {
app.UseMiddleware(Middleware.NewStatic("Static"))
Expand Down
44 changes: 3 additions & 41 deletions Framework/HostBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ package YoyoGo

import (
"github.com/maxzhang1985/yoyogo/Context"
"github.com/maxzhang1985/yoyogo/Controller"
"github.com/maxzhang1985/yoyogo/DependencyInjection"
"github.com/maxzhang1985/yoyogo/Router"
"os"
)

type HostBuilder struct {
server IServer
context *HostBuildContext
configures []func(*ApplicationBuilder)
routeconfigures []func(Router.IRouterBuilder)
servicesconfigures []func(*DependencyInjection.ServiceCollection)
mvcconfigures []func(builder *Controller.ControllerBuilder)
servicesConfigures []func(*DependencyInjection.ServiceCollection)
lifeConfigure func(*ApplicationLife)
}

Expand All @@ -23,21 +19,11 @@ func (self *HostBuilder) Configure(configure func(*ApplicationBuilder)) *HostBui
return self
}

func (self *HostBuilder) UseEndpoints(configure func(Router.IRouterBuilder)) *HostBuilder {
self.routeconfigures = append(self.routeconfigures, configure)
return self
}

func (self *HostBuilder) ConfigureServices(configure func(*DependencyInjection.ServiceCollection)) *HostBuilder {
self.servicesconfigures = append(self.servicesconfigures, configure)
self.servicesConfigures = append(self.servicesConfigures, 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
return self
Expand All @@ -48,26 +34,6 @@ func (self *HostBuilder) UseServer(server IServer) *HostBuilder {
return self
}

func (self *HostBuilder) UseFastHttpByAddr(addr string) *HostBuilder {
self.server = NewFastHttp(addr)
return self
}

func (self *HostBuilder) UseFastHttp() *HostBuilder {
self.server = NewFastHttp("")
return self
}

func (self *HostBuilder) UseHttpByAddr(addr string) *HostBuilder {
self.server = DefaultHttpServer(addr)
return self
}

func (self *HostBuilder) UseHttp() *HostBuilder {
self.server = DefaultHttpServer("")
return self
}

func runningHostEnvironmentSetting(hostEnv *Context.HostEnvironment) {
hostEnv.Port = detectAddress(hostEnv.Addr)
hostEnv.PID = os.Getpid()
Expand All @@ -91,7 +57,7 @@ func (self *HostBuilder) Build() WebHost {
self.context.ApplicationCycle = NewApplicationLife()

configures(self.context, services)
for _, configure := range self.servicesconfigures {
for _, configure := range self.servicesConfigures {
configure(services)
}

Expand All @@ -101,10 +67,6 @@ func (self *HostBuilder) Build() WebHost {
configure(applicationBuilder)
}

for _, configure := range self.routeconfigures {
configure(applicationBuilder.routerBuilder)
}

self.context.applicationServicesDef = services
applicationBuilder.SetHostBuildContext(self.context)
self.context.RequestDelegate = applicationBuilder.Build() // ServeHTTP(w http.ResponseWriter, r *http.Request)
Expand Down
21 changes: 21 additions & 0 deletions Framework/WebHostBuilderEx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package YoyoGo

func (self *HostBuilder) UseFastHttpByAddr(addr string) *HostBuilder {
self.server = NewFastHttp(addr)
return self
}

func (self *HostBuilder) UseFastHttp() *HostBuilder {
self.server = NewFastHttp("")
return self
}

func (self *HostBuilder) UseHttpByAddr(addr string) *HostBuilder {
self.server = DefaultHttpServer(addr)
return self
}

func (self *HostBuilder) UseHttp() *HostBuilder {
self.server = DefaultHttpServer("")
return self
}
41 changes: 25 additions & 16 deletions Middleware/LoggerMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/maxzhang1985/yoyogo/Context"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -73,6 +74,7 @@ type LoggerInfo struct {
HostName string
Method string
Path string
Body string
Request *http.Request
}

Expand Down Expand Up @@ -110,30 +112,37 @@ func (l *Logger) Inovke(ctx *Context.HttpContext, next func(ctx *Context.HttpCon
next(ctx)
res := ctx.Response

loginfo := LoggerInfo{
strBody := ""
bodyFormat := "%s"
if ctx.Request.Method == "POST" {
body, _ := ioutil.ReadAll(ctx.Request.Body)
strBody = string(body[:])
bodyFormat = "\n%s"
}

logInfo := LoggerInfo{
StartTime: start.Format(l.dateFormat),
Status: res.Status(),
Duration: time.Since(start).String(),
HostName: ctx.Request.Host,
Method: ctx.Request.Method,
Path: ctx.Request.URL.Path,
Request: ctx.Request,
Path: ctx.Request.URL.RequestURI(),
Body: strBody,
}

//buff := &bytes.Buffer{}
//_ = l.template.Execute(buff, log)
statusColor := loginfo.StatusCodeColor()
methodColor := loginfo.MethodColor()
resetColor := loginfo.ResetColor()
outlog := fmt.Sprintf("[yoyogo] %v |%s %3d %s| %10v | %15s |%s %5s %s %s",
loginfo.StartTime,
statusColor, loginfo.Status, resetColor,
loginfo.Duration,
loginfo.HostName,
methodColor, loginfo.Method, resetColor,
loginfo.Path,
statusColor := logInfo.StatusCodeColor()
methodColor := logInfo.MethodColor()
resetColor := logInfo.ResetColor()
outLog := fmt.Sprintf("[yoyogo] %v |%s %3d %s| %10v | %15s |%s %5s %s %s "+bodyFormat,
logInfo.StartTime,
statusColor, logInfo.Status, resetColor,
logInfo.Duration,
logInfo.HostName,
methodColor, logInfo.Method, resetColor,
logInfo.Path,
logInfo.Body,
)

l.ALogger.Println(outlog)
l.ALogger.Println(outLog)

}

0 comments on commit cb70cc0

Please sign in to comment.