Skip to content

Commit

Permalink
func commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxzhang committed Dec 2, 2019
1 parent d389e8e commit b1e4db1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Framework/ApplicationBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (
)

// HTTP methods

const (
// DefaultAddress is used if no other is specified.
DefaultAddress = ":8080"
)

//application builder struct
type ApplicationBuilder struct {
hostContext *HostBuildContext
routerHandler Router.IRouterHandler
middleware middleware
handlers []Handler
}

// create classic application builder
func UseClassic() *ApplicationBuilder {
return &ApplicationBuilder{}
}
Expand All @@ -35,6 +36,7 @@ func CreateDefaultWebHostBuilder(args []string, routerConfig func(router Router.
UseRouter(routerConfig)
}

// create new application builder
func NewApplicationBuilder(context *HostBuildContext) *ApplicationBuilder {
routerHandler := Router.NewRouterHandler()
recovery := Middleware.NewRecovery()
Expand All @@ -46,6 +48,7 @@ func NewApplicationBuilder(context *HostBuildContext) *ApplicationBuilder {
return self
}

// after create builder , apply router and logger and recovery middleware
func (self *ApplicationBuilder) UseMvc() *ApplicationBuilder {
self.routerHandler = Router.NewRouterHandler()
self.UseMiddleware(Middleware.NewLogger())
Expand All @@ -55,13 +58,15 @@ func (self *ApplicationBuilder) UseMvc() *ApplicationBuilder {
return self
}

// 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 (n *ApplicationBuilder) UseMiddleware(handler Handler) {
if handler == nil {
panic("handler cannot be nil")
Expand All @@ -71,31 +76,35 @@ func (n *ApplicationBuilder) UseMiddleware(handler Handler) {
//n.middleware = build(n.handlers)
}

// build and combo all middleware to request delegate (ServeHTTP(w http.ResponseWriter, r *http.Request))
func (n *ApplicationBuilder) Build() IRequestDelegate {
n.middleware = build(n.handlers)
return n
}

// apply static middleware in builder
func (app *ApplicationBuilder) UseStatic(path string) {
app.UseMiddleware(Middleware.NewStatic("Static"))
}

// apply handler middleware in builder
func (n *ApplicationBuilder) UseHandler(handler http.Handler) {
n.UseMiddleware(wrap(handler))
}

// apply handler func middleware in builder
func (n *ApplicationBuilder) UseHandlerFunc(handlerFunc func(rw http.ResponseWriter, r *http.Request)) {
n.UseMiddleware(wrapFunc(handlerFunc))
}

// apply handler func middleware in builder
func (n *ApplicationBuilder) UseFunc(handlerFunc HandlerFunc) {
n.UseMiddleware(handlerFunc)
}

/*
Middleware of Server Handler , request port.
*/

func (yoyo *ApplicationBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
yoyo.middleware.Invoke(Context.NewContext(w, r))
}

0 comments on commit b1e4db1

Please sign in to comment.