Skip to content

Commit

Permalink
Merge pull request #19 from maxzhang1985/dev
Browse files Browse the repository at this point in the history
v1.3.0
  • Loading branch information
yoyofx authored Dec 2, 2019
2 parents affe5ac + 0114b86 commit 8aa61d8
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 72 deletions.
19 changes: 11 additions & 8 deletions Context/HttpContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"github.com/maxzhang1985/yoyogo/DependencyInjection"
"github.com/maxzhang1985/yoyogo/ResponseRender"
"github.com/maxzhang1985/yoyogo/Standard"
"github.com/maxzhang1985/yoyogo/Utils"
Expand All @@ -28,24 +29,26 @@ const (
)

type HttpContext struct {
Req *http.Request
Resp *responseWriter
RouterData url.Values
store map[string]interface{}
storeMutex *sync.RWMutex
Req *http.Request
Resp *responseWriter
RouterData url.Values
RequiredServices DependencyInjection.IServiceProvider
store map[string]interface{}
storeMutex *sync.RWMutex
}

func NewContext(w http.ResponseWriter, r *http.Request) *HttpContext {
func NewContext(w http.ResponseWriter, r *http.Request, sp DependencyInjection.IServiceProvider) *HttpContext {
ctx := &HttpContext{}
ctx.init(w, r)
ctx.init(w, r, sp)
return ctx
}

func (ctx *HttpContext) init(w http.ResponseWriter, r *http.Request) {
func (ctx *HttpContext) init(w http.ResponseWriter, r *http.Request, sp DependencyInjection.IServiceProvider) {
ctx.storeMutex = new(sync.RWMutex)
ctx.Resp = &responseWriter{w, 0, 0, nil}
ctx.Req = r
ctx.RouterData = url.Values{}
ctx.RequiredServices = sp
ctx.storeMutex.Lock()
ctx.store = nil
ctx.storeMutex.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/DefaultServiceProviderFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func (sc ServiceCollection) Build() IServiceProvider {
return nil
}

return DefaultServiceProvider{container}
return &DefaultServiceProvider{container}
}
4 changes: 2 additions & 2 deletions DependencyInjection/ServiceCollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (sc *ServiceCollection) AddSingletonByName(name string, provider interface{
sc.AddServiceDescriptor(sd)
}

func (sc *ServiceCollection) AddSingletonByImplements(provider interface{}, implements ...interface{}) {
func (sc *ServiceCollection) AddSingletonByImplements(provider interface{}, implements interface{}) {
sd := NewServiceDescriptorByImplements(provider, implements, Singleton)
sc.AddServiceDescriptor(sd)
}
Expand All @@ -40,7 +40,7 @@ func (sc *ServiceCollection) AddTransientByName(name string, provider interface{
sc.AddServiceDescriptor(sd)
}

func (sc *ServiceCollection) AddTransientByImplements(provider interface{}, implements ...interface{}) {
func (sc *ServiceCollection) AddTransientByImplements(provider interface{}, implements interface{}) {
sd := NewServiceDescriptorByImplements(provider, implements, Transient)
sc.AddServiceDescriptor(sd)
}
6 changes: 3 additions & 3 deletions DependencyInjection/ServiceDescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package DependencyInjection
type ServiceDescriptor struct {
Name string
Provider interface{}
Implements []interface{}
Implements interface{}
Lifetime ServiceLifetime
}

func NewServiceDescriptor(name string, provider interface{}, implements []interface{}, lifetime ServiceLifetime) ServiceDescriptor {
func NewServiceDescriptor(name string, provider interface{}, implements interface{}, lifetime ServiceLifetime) ServiceDescriptor {
return ServiceDescriptor{Name: name, Provider: provider, Implements: implements, Lifetime: lifetime}
}

Expand All @@ -19,6 +19,6 @@ func NewServiceDescriptorByName(name string, provider interface{}, lifetime Serv
return ServiceDescriptor{Name: name, Provider: provider, Lifetime: lifetime}
}

func NewServiceDescriptorByImplements(provider interface{}, implements []interface{}, lifetime ServiceLifetime) ServiceDescriptor {
func NewServiceDescriptorByImplements(provider interface{}, implements interface{}, lifetime ServiceLifetime) ServiceDescriptor {
return ServiceDescriptor{Provider: provider, Implements: implements, Lifetime: lifetime}
}
27 changes: 0 additions & 27 deletions Examples/SimpleWeb/Certificate/Certificate.go

This file was deleted.

14 changes: 13 additions & 1 deletion Examples/SimpleWeb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"github.com/maxzhang1985/yoyogo/Context"
"github.com/maxzhang1985/yoyogo/DependencyInjection"
"github.com/maxzhang1985/yoyogo/Examples/SimpleWeb/models"
"github.com/maxzhang1985/yoyogo/Framework"
"github.com/maxzhang1985/yoyogo/Router"
"github.com/maxzhang1985/yoyogo/Standard"
Expand All @@ -23,7 +25,10 @@ func CreateCustomWebHostBuilder(args []string) *YoyoGo.HostBuilder {
Configure(func(app *YoyoGo.ApplicationBuilder) {
app.UseStatic("Static")
}).
UseRouter(RouterConfigFunc)
UseRouter(RouterConfigFunc).
ConfigureServices(func(serviceCollection *DependencyInjection.ServiceCollection) {
serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction))
})
}

//*/
Expand All @@ -41,6 +46,7 @@ func RouterConfigFunc(router Router.IRouterBuilder) {
})

router.GET("/info", GetInfo)
router.GET("/ioc", GetInfoByIOC)
}

//endregion
Expand All @@ -59,6 +65,12 @@ func GetInfo(ctx *Context.HttpContext) {
ctx.JSON(200, Std.M{"info": "ok"})
}

func GetInfoByIOC(ctx *Context.HttpContext) {
var userAction models.IUserAction
_ = ctx.RequiredServices.GetService(&userAction)
ctx.JSON(200, Std.M{"info": "ok " + userAction.Login("zhang")})
}

//HttpPost request: /info/:id ?q1=abc&username=123
func PostInfo(ctx *Context.HttpContext) {
qs_q1 := ctx.Query("q1")
Expand Down
25 changes: 25 additions & 0 deletions Examples/SimpleWeb/models/UserAction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package models

import (
"math/rand"
"strconv"
"time"
)

type IUserAction interface {
Login(name string) string
}

type UserAction struct {
index int
}

func NewUserAction() *UserAction {
rand.Seed(time.Now().Unix())
rnd := rand.Intn(100)
return &UserAction{index: rnd}
}

func (u UserAction) Login(name string) string {
return "hello " + name + strconv.Itoa(u.index)
}
26 changes: 13 additions & 13 deletions Framework/ApplicationBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ func New(handlers ...Handler) *ApplicationBuilder {
}

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

n.handlers = append(n.handlers, handler)
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 (n *ApplicationBuilder) Build() IRequestDelegate {
n.middleware = build(n.handlers)
return n
func (app *ApplicationBuilder) Build() IRequestDelegate {
app.middleware = build(app.handlers)
return app
}

// apply static middleware in builder
Expand All @@ -88,23 +88,23 @@ func (app *ApplicationBuilder) UseStatic(path string) {
}

// apply handler middleware in builder
func (n *ApplicationBuilder) UseHandler(handler http.Handler) {
n.UseMiddleware(wrap(handler))
func (app *ApplicationBuilder) UseHandler(handler http.Handler) {
app.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))
func (app *ApplicationBuilder) UseHandlerFunc(handlerFunc func(rw http.ResponseWriter, r *http.Request)) {
app.UseMiddleware(wrapFunc(handlerFunc))
}

// apply handler func middleware in builder
func (n *ApplicationBuilder) UseFunc(handlerFunc HandlerFunc) {
n.UseMiddleware(handlerFunc)
func (app *ApplicationBuilder) UseFunc(handlerFunc HandlerFunc) {
app.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))
func (app *ApplicationBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
app.middleware.Invoke(Context.NewContext(w, r, app.hostContext.applicationServices))
}
5 changes: 4 additions & 1 deletion Framework/HostBuildContext.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package YoyoGo

import "github.com/maxzhang1985/yoyogo/DependencyInjection"

type HostBuildContext struct {
hostingEnvironment *HostEnv
hostingEnvironment *HostEnv
applicationServices DependencyInjection.IServiceProvider
}
28 changes: 23 additions & 5 deletions Framework/HostBuilder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package YoyoGo

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

type HostEnv struct {
ApplicationName string
Expand All @@ -14,10 +17,11 @@ type HostEnv struct {
}

type HostBuilder struct {
server IServer
context *HostBuildContext
configures []func(*ApplicationBuilder)
routeconfigures []func(Router.IRouterBuilder)
server IServer
context *HostBuildContext
configures []func(*ApplicationBuilder)
routeconfigures []func(Router.IRouterBuilder)
servicesconfigures []func(*DependencyInjection.ServiceCollection)
}

func (self *HostBuilder) Configure(configure func(*ApplicationBuilder)) *HostBuilder {
Expand All @@ -30,6 +34,11 @@ func (self *HostBuilder) UseRouter(configure func(Router.IRouterBuilder)) *HostB
return self
}

func (self *HostBuilder) ConfigureServices(configure func(*DependencyInjection.ServiceCollection)) *HostBuilder {
self.servicesconfigures = append(self.servicesconfigures, configure)
return self
}

func (self *HostBuilder) UseServer(server IServer) *HostBuilder {
self.server = server
return self
Expand All @@ -49,6 +58,8 @@ func (self *HostBuilder) Build() WebHost {
self.context.hostingEnvironment.AppMode = "Dev"
self.context.hostingEnvironment.DefaultAddress = ":8080"

services := DependencyInjection.NewServiceCollection()

builder := NewApplicationBuilder(self.context)

for _, configure := range self.configures {
Expand All @@ -59,6 +70,13 @@ func (self *HostBuilder) Build() WebHost {
configure(builder)
}

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

serviceProvider := services.Build()
self.context.applicationServices = serviceProvider

return NewWebHost(self.server, builder.Build(), self.context)

}
Expand Down
Loading

0 comments on commit 8aa61d8

Please sign in to comment.