Skip to content

Commit

Permalink
框架集成Config
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Aug 12, 2020
1 parent 069b4b0 commit 5bca353
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 16 deletions.
8 changes: 8 additions & 0 deletions Abstractions/Configuration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Abstractions

import (
"fmt"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -38,3 +39,10 @@ func (c *Configuration) GetSection(name string) IConfiguration {
}
return nil
}

func (c *Configuration) Unmarshal(obj interface{}) {
err := c.config.Unmarshal(obj)
if err != nil {
fmt.Println("unmarshal config is failed, err:", err)
}
}
4 changes: 3 additions & 1 deletion Abstractions/HostBuildContext.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Abstractions

import (
"github.com/yoyofx/yoyogo/Abstractions/configs"
"github.com/yoyofx/yoyogo/DependencyInjection"
"github.com/yoyofx/yoyogo/WebFramework/Context"
)
Expand All @@ -9,7 +10,8 @@ type HostBuildContext struct {
RequestDelegate interface{}
ApplicationCycle *ApplicationLife
HostingEnvironment *Context.HostEnvironment
HostConfiguration IConfiguration
Configuration IConfiguration
HostConfiguration *configs.HostConfig
ApplicationServicesDef *DependencyInjection.ServiceCollection
ApplicationServices DependencyInjection.IServiceProvider
HostServices DependencyInjection.IServiceProvider
Expand Down
28 changes: 22 additions & 6 deletions Abstractions/HostBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package Abstractions

import (
"fmt"
"github.com/yoyofx/yoyogo"
YoyoGo "github.com/yoyofx/yoyogo"
"github.com/yoyofx/yoyogo/Abstractions/configs"
"github.com/yoyofx/yoyogo/DependencyInjection"
"github.com/yoyofx/yoyogo/WebFramework/Context"
"net"
Expand Down Expand Up @@ -33,7 +34,13 @@ func (host *HostBuilder) Configure(configure interface{}) *HostBuilder {
}

func (host *HostBuilder) UseConfiguration(configuration IConfiguration) *HostBuilder {
host.Context.HostConfiguration = configuration
host.Context.Configuration = configuration
section := host.Context.Configuration.GetSection("application")
if section != nil {
config := &configs.HostConfig{}
section.Unmarshal(config)
host.Context.HostConfiguration = config
}
return host
}

Expand Down Expand Up @@ -81,24 +88,33 @@ func RunningHostEnvironmentSetting(hostEnv *Context.HostEnvironment) {
}

//buildingHostEnvironmentSetting build each configuration by init , such as file or env or args ...
func buildingHostEnvironmentSetting(hostEnv *Context.HostEnvironment) {
hostEnv.ApplicationName = "app"
func buildingHostEnvironmentSetting(context *HostBuildContext) {
hostEnv := context.HostingEnvironment
hostEnv.Version = YoyoGo.Version
hostEnv.Addr = DetectAddress("")
config := context.HostConfiguration
hostEnv.ApplicationName = config.Name
if config.Server.Address != "" {
hostEnv.Addr = config.Server.Address
} else {
hostEnv.Addr = DetectAddress("")
}
hostEnv.Port = strings.Replace(hostEnv.Addr, ":", "", -1)
hostEnv.Args = os.Args

if hostEnv.Profile == "" {
hostEnv.Profile = Context.Dev
}
if config.Profile != "" {
hostEnv.Profile = config.Profile
}

}

// Build host
func (host *HostBuilder) Build() IServiceHost {
services := DependencyInjection.NewServiceCollection()

buildingHostEnvironmentSetting(host.Context.HostingEnvironment)
buildingHostEnvironmentSetting(host.Context)
host.Context.ApplicationCycle = NewApplicationLife()

innerConfigures(host.Context, services)
Expand Down
1 change: 1 addition & 0 deletions Abstractions/IConfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package Abstractions
type IConfiguration interface {
Get(name string) interface{}
GetSection(name string) IConfiguration
Unmarshal(interface{})
}
8 changes: 8 additions & 0 deletions Abstractions/configs/HostConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package configs

type HostConfig struct {
Name string `mapstructure:"name"`
Metadata string `mapstructure:"metadata"`
Profile string `mapstructure:"profile"`
Server ServerConfig `mapstructure:"server"`
}
13 changes: 13 additions & 0 deletions Abstractions/configs/ServerConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package configs

type ServerConfig struct {
ServerType string `mapstructure:"type"`
Address string `mapstructure:"address"`
MaxRequestSize string `mapstructure:"max_request_size"`
Static StaticConfig `mapstructure:"static"`
}

type StaticConfig struct {
Patten string `mapstructure:"patten"`
WebRoot string `mapstructure:"webroot"`
}
5 changes: 2 additions & 3 deletions Examples/SimpleWeb/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ application:
metadata: "hello world"
profile: "Dev"
server:
web:
type: "fasthttp"
address: ":8081"
maxBody: 2096157
address: ":8080"
max_request_size: 2096157
static:
patten: "/"
webroot: "./Static"
3 changes: 0 additions & 3 deletions Examples/SimpleWeb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ func CreateCustomBuilder() *Abstractions.HostBuilder {
configuration := Abstractions.NewConfigurationBuilder().AddYamlFile("config").Build()

return YoyoGo.NewWebHostBuilder().
SetEnvironment(Context.Prod).
//UseFastHttp().
UseConfiguration(configuration).
Configure(func(app *YoyoGo.WebApplicationBuilder) {
//app.UseStatic("/","./Static")
app.UseStaticAssets()
app.UseEndpoints(registerEndpointRouterConfig)
app.UseMvc(func(builder *Mvc.ControllerBuilder) {
Expand Down
2 changes: 1 addition & 1 deletion WebFramework/Middleware/StaticMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewStatic(patten string, path string) *Static {
}

func NewStaticWithConfig(configuration Abstractions.IConfiguration) *Static {
config := configuration.GetSection("application.server.web.static")
config := configuration.GetSection("application.server.static")
patten := config.Get("patten").(string)
path := config.Get("webroot").(string)
return NewStatic(patten, path)
Expand Down
2 changes: 1 addition & 1 deletion WebFramework/WebApplicationBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (app *WebApplicationBuilder) UseStatic(patten string, path string) {
}

func (app *WebApplicationBuilder) UseStaticAssets() {
app.UseMiddleware(Middleware.NewStaticWithConfig(app.hostContext.HostConfiguration))
app.UseMiddleware(Middleware.NewStaticWithConfig(app.hostContext.Configuration))
}

// apply handler middleware in builder
Expand Down
2 changes: 1 addition & 1 deletion WebFramework/WebHostBuilderDecorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (decorator WebHostBuilderDecorator) OverrideNewApplicationBuilder(context *
// OverrideNewHost Create WebHost.
func (decorator WebHostBuilderDecorator) OverrideNewHost(server Abstractions.IServer, context *Abstractions.HostBuildContext) Abstractions.IServiceHost {
if server == nil && context.HostConfiguration != nil {
section := context.HostConfiguration.GetSection("application.server.web")
section := context.Configuration.GetSection("application.server")
if section != nil {
serverType := section.Get("type").(string)
address := section.Get("address").(string)
Expand Down

0 comments on commit 5bca353

Please sign in to comment.