Skip to content

Commit

Permalink
Merge pull request #71 from yoyofx/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
yoyofx authored Aug 12, 2020
2 parents 1beab63 + 08732bc commit 6b6bfb4
Show file tree
Hide file tree
Showing 28 changed files with 678 additions and 45 deletions.
46 changes: 43 additions & 3 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 All @@ -10,22 +11,61 @@ type Configuration struct {
}

func NewConfiguration(configContext *ConfigurationContext) *Configuration {
v := viper.New()
defaultConfig := viper.New()
defaultConfig.AddConfigPath(".")
defaultConfig.SetConfigName(configContext.configName)
defaultConfig.SetConfigType(configContext.configType)
if err := defaultConfig.ReadInConfig(); err != nil {
return nil
}

profile := defaultConfig.Get("application.profile")
var profileConfig *viper.Viper
if profile != nil {
profileConfig = viper.New()
profileConfig.AddConfigPath(".")
configContext.profile = profile.(string)
profileConfig.SetConfigName(configContext.configName + "_" + configContext.profile)
profileConfig.SetConfigType(configContext.configType)
configs := defaultConfig.AllSettings()
// 将default中的配置全部以默认配置写入
for k, v := range configs {
profileConfig.Set(k, v)
}

if err := profileConfig.ReadInConfig(); err != nil {
profileConfig = defaultConfig
}
}

return &Configuration{
context: configContext,
config: v,
config: profileConfig,
}
}

func (c *Configuration) Get(name string) interface{} {
return c.config.Get(name)
}

func (c *Configuration) GetSection(name string) *Configuration {
func (c *Configuration) GetSection(name string) IConfiguration {
section := c.config.Sub(name)
section.SetConfigName(c.context.configName + "_" + c.context.profile)
configs := c.config.AllSettings()
// 将default中的配置全部以默认配置写入
for k, v := range configs {
section.Set(k, v)
}

if section != nil {
return &Configuration{config: section}
}
return nil
}

func (c *Configuration) Unmarshal(obj interface{}) {
err := c.config.Unmarshal(obj)
if err != nil {
fmt.Println("unmarshal config is failed, err:", err)
}
}
13 changes: 9 additions & 4 deletions Abstractions/ConfigurationBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type ConfigurationContext struct {
enableEnv bool
configType string
configName string
profile string
}

type ConfigurationBuilder struct {
Expand All @@ -15,26 +16,30 @@ func NewConfigurationBuilder() *ConfigurationBuilder {
return &ConfigurationBuilder{context: &ConfigurationContext{}}
}

func (builder *ConfigurationBuilder) AddFlagArgs() {
func (builder *ConfigurationBuilder) AddFlagArgs() *ConfigurationBuilder {
builder.context.enableFlag = true
return builder
}

func (builder *ConfigurationBuilder) AddEnvironment() {
func (builder *ConfigurationBuilder) AddEnvironment() *ConfigurationBuilder {
builder.context.enableEnv = true
return builder
}

func (builder *ConfigurationBuilder) AddYamlFile(name string) {
func (builder *ConfigurationBuilder) AddYamlFile(name string) *ConfigurationBuilder {
if builder.context.configType == "" {
builder.context.configType = "yaml"
builder.context.configName = name
}
return builder
}

func (builder *ConfigurationBuilder) AddJsonFile(name string) {
func (builder *ConfigurationBuilder) AddJsonFile(name string) *ConfigurationBuilder {
if builder.context.configType == "" {
builder.context.configType = "json"
builder.context.configName = name
}
return builder
}

func (builder *ConfigurationBuilder) Build() *Configuration {
Expand Down
4 changes: 4 additions & 0 deletions 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,6 +10,9 @@ type HostBuildContext struct {
RequestDelegate interface{}
ApplicationCycle *ApplicationLife
HostingEnvironment *Context.HostEnvironment
Configuration IConfiguration
HostConfiguration *configs.HostConfig
ApplicationServicesDef *DependencyInjection.ServiceCollection
ApplicationServices DependencyInjection.IServiceProvider
HostServices DependencyInjection.IServiceProvider
}
34 changes: 29 additions & 5 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 @@ -32,6 +33,17 @@ func (host *HostBuilder) Configure(configure interface{}) *HostBuilder {
return host
}

func (host *HostBuilder) UseConfiguration(configuration IConfiguration) *HostBuilder {
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
}

// ConfigureServices configure function by ServiceCollection of DI.
func (host *HostBuilder) ConfigureServices(configure func(*DependencyInjection.ServiceCollection)) *HostBuilder {
host.servicesConfigures = append(host.servicesConfigures, configure)
Expand Down Expand Up @@ -76,10 +88,21 @@ 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
if config != nil {
hostEnv.ApplicationName = config.Name
if config.Server.Address != "" {
hostEnv.Addr = config.Server.Address
}
if config.Profile != "" {
hostEnv.Profile = config.Profile
}
}

hostEnv.Port = strings.Replace(hostEnv.Addr, ":", "", -1)
hostEnv.Args = os.Args

Expand All @@ -93,15 +116,15 @@ func buildingHostEnvironmentSetting(hostEnv *Context.HostEnvironment) {
func (host *HostBuilder) Build() IServiceHost {
services := DependencyInjection.NewServiceCollection()

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

innerConfigures(host.Context, services)
for _, configure := range host.servicesConfigures {
configure(services)
}

applicationBuilder := host.Decorator.OverrideNewApplicationBuilder()
applicationBuilder := host.Decorator.OverrideNewApplicationBuilder(host.Context)

for _, configure := range host.configures {
//configure(applicationBuilder)
Expand All @@ -110,6 +133,7 @@ func (host *HostBuilder) Build() IServiceHost {

host.Context.ApplicationServicesDef = services
applicationBuilder.SetHostBuildContext(host.Context)
host.Context.HostServices = services.Build() //serviceProvider
host.Context.RequestDelegate = applicationBuilder.Build() // ServeHTTP(w http.ResponseWriter, r *http.Request)
host.Context.ApplicationServices = services.Build() //serviceProvider

Expand Down
3 changes: 3 additions & 0 deletions Abstractions/IConfiguration.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package Abstractions

type IConfiguration interface {
Get(name string) interface{}
GetSection(name string) IConfiguration
Unmarshal(interface{})
}
2 changes: 1 addition & 1 deletion Abstractions/IHostBuilderDecorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type IHostBuilderDecorator interface {
// OverrideConfigure is configure function by application builder.
OverrideConfigure(configureFunc interface{}, builder IApplicationBuilder)
// OverrideNewApplicationBuilder create application builder.
OverrideNewApplicationBuilder() IApplicationBuilder
OverrideNewApplicationBuilder(context *HostBuildContext) IApplicationBuilder
// OverrideNewHost Create IServiceHost.
OverrideNewHost(server IServer, context *HostBuildContext) IServiceHost
}
1 change: 1 addition & 0 deletions Abstractions/IServiceHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func PrintLogo(l *log.Logger, env *Context.HostEnvironment) {
l.Printf("machine host ip : %s", ConsoleColors.Blue(env.Host))
l.Printf("listening on port : %s", ConsoleColors.Blue(env.Port))
l.Printf("application running pid : %s", ConsoleColors.Blue(strconv.Itoa(env.PID)))
l.Printf("application name : %s", ConsoleColors.Blue(env.ApplicationName))
l.Printf("application environment : %s", ConsoleColors.Blue(env.Profile))
l.Printf("application exec path : %s", ConsoleColors.Yellow(Utils.GetCurrentDirectory()))
l.Printf("running in %s mode , change (Dev,Test,Prod) mode by HostBuilder.SetEnvironment .", ConsoleColors.Blue(env.Profile))
Expand Down
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"`
}
11 changes: 11 additions & 0 deletions Examples/SimpleWeb/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
application:
name: demo1
metadata: "hello world"
profile: "dev"
server:
type: "fasthttp"
address: ":8080"
max_request_size: 2096157
static:
patten: "/"
webroot: "./Static"
4 changes: 4 additions & 0 deletions Examples/SimpleWeb/config_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
application:
name: demo_dev
server:
address: ":8080"
4 changes: 4 additions & 0 deletions Examples/SimpleWeb/config_prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
application:
name: demo_prod
server:
address: ":8081"
21 changes: 17 additions & 4 deletions Examples/SimpleWeb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,31 @@ import (
"github.com/yoyofx/yoyogo/WebFramework/Router"
)

func main() {
func SimpleDemo() {
YoyoGo.CreateDefaultBuilder(func(router Router.IRouterBuilder) {
Endpoints.UsePrometheus(router)

router.GET("/info", func(ctx *Context.HttpContext) {
ctx.JSON(200, Context.M{"info": "ok"})
})
}).Build().Run()
}

func main() {
//SimpleDemo()
//webHost := YoyoGo.CreateDefaultBuilder(registerEndpointRouterConfig).Build()
webHost := CreateCustomBuilder().Build()
webHost.Run()
}

//* Create the builder of Web host
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) {
builder.AddController(contollers.NewUserController)
Expand All @@ -45,6 +56,8 @@ func CreateCustomBuilder() *Abstractions.HostBuilder {
func registerEndpointRouterConfig(router Router.IRouterBuilder) {
Endpoints.UseHealth(router)
Endpoints.UseViz(router)
Endpoints.UsePrometheus(router)
//Endpoints.UsePprof(router)

router.GET("/error", func(ctx *Context.HttpContext) {
panic("http get error")
Expand Down
Loading

0 comments on commit 6b6bfb4

Please sign in to comment.