Skip to content

Commit

Permalink
Merge pull request #46 from maxzhang1985/dev
Browse files Browse the repository at this point in the history
v1.4.9.5
  • Loading branch information
yoyofx authored Jun 23, 2020
2 parents ff67770 + e67e520 commit d80adde
Show file tree
Hide file tree
Showing 79 changed files with 727 additions and 445 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

.idea

.vscode

go.sum
__debug_bin
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package YoyoGo
package Abstractions

import "sync"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package YoyoGo
package Abstractions

const (
APPLICATION_LIFE_START = "APPLICATION_LIFE_START"
Expand Down
6 changes: 4 additions & 2 deletions Framework/Configuration.go → Abstractions/Configuration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package YoyoGo
package Abstractions

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

type Configuration struct {
context *ConfigurationContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package YoyoGo
package Abstractions

type ConfigurationContext struct {
enableFlag bool
Expand Down
14 changes: 14 additions & 0 deletions Abstractions/HostBuildContext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Abstractions

import (
"github.com/maxzhang1985/yoyogo/DependencyInjection"
"github.com/maxzhang1985/yoyogo/WebFramework/Context"
)

type HostBuildContext struct {
RequestDelegate interface{}
ApplicationCycle *ApplicationLife
HostingEnvironment *Context.HostEnvironment
ApplicationServicesDef *DependencyInjection.ServiceCollection
ApplicationServices DependencyInjection.IServiceProvider
}
118 changes: 118 additions & 0 deletions Abstractions/HostBuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package Abstractions

import (
"fmt"
"github.com/maxzhang1985/yoyogo"
"github.com/maxzhang1985/yoyogo/DependencyInjection"
"github.com/maxzhang1985/yoyogo/WebFramework/Context"
"net"
"os"
)

// host builder
type HostBuilder struct {
Server IServer // Server
Context *HostBuildContext // context of Host builder
Decorator IHostBuilderDecorator // host builder decorator or extension
configures []interface{} // []func(IApplicationBuilder), configure function by application builder.
servicesConfigures []func(*DependencyInjection.ServiceCollection) // configure function by ServiceCollection of DI.
lifeConfigure func(*ApplicationLife) // on application life event
}

// Configure function func(IApplicationBuilder)
func (host *HostBuilder) Configure(configure interface{}) *HostBuilder {
host.configures = append(host.configures, configure)
return host
}

// ConfigureServices configure function by ServiceCollection of DI.
func (host *HostBuilder) ConfigureServices(configure func(*DependencyInjection.ServiceCollection)) *HostBuilder {
host.servicesConfigures = append(host.servicesConfigures, configure)
return host
}

// OnApplicationLifeEvent on application life event
func (host *HostBuilder) OnApplicationLifeEvent(lifeConfigure func(*ApplicationLife)) *HostBuilder {
host.lifeConfigure = lifeConfigure
return host
}

// UseServer set IServer to host builder
func (host *HostBuilder) UseServer(server IServer) *HostBuilder {
host.Server = server
return host
}

// getLocalIP get localhost ip
func getLocalIP() string {
var localIp string
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
localIp = ipnet.IP.String()
break
}
}
}
return localIp
}

// RunningHostEnvironmentSetting ,get running env setting.
func RunningHostEnvironmentSetting(hostEnv *Context.HostEnvironment) {
hostEnv.Host = getLocalIP()
hostEnv.Port = DetectAddress(hostEnv.Addr)
hostEnv.PID = os.Getpid()
}

//buildingHostEnvironmentSetting build each configuration by init , such as file or env or args ...
func buildingHostEnvironmentSetting(hostEnv *Context.HostEnvironment) {
hostEnv.ApplicationName = "app"
hostEnv.Version = YoyoGo.Version
hostEnv.Addr = ":8080"

hostEnv.Args = os.Args

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

}

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

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

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

applicationBuilder := host.Decorator.OverrideNewApplicationBuilder()

for _, configure := range host.configures {
//configure(applicationBuilder)
host.Decorator.OverrideConfigure(configure, applicationBuilder)
}

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

go host.lifeConfigure(host.Context.ApplicationCycle)
return host.Decorator.OverrideNewHost(host.Server, host.Context)
}

// inner configures function for DI.
func innerConfigures(hostContext *HostBuildContext, serviceCollection *DependencyInjection.ServiceCollection) {
serviceCollection.AddSingleton(func() *ApplicationLife { return hostContext.ApplicationCycle })
serviceCollection.AddSingleton(func() *Context.HostEnvironment { return hostContext.HostingEnvironment })
}
6 changes: 6 additions & 0 deletions Abstractions/IApplicationBuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package Abstractions

type IApplicationBuilder interface {
Build() interface{}
SetHostBuildContext(*HostBuildContext)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package YoyoGo
package Abstractions

type IConfiguration interface {
}
12 changes: 12 additions & 0 deletions Abstractions/IHostBuilderDecorator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Abstractions

// IHostBuilderDecorator Host Builder decorator or extension
type IHostBuilderDecorator interface {

// OverrideConfigure is configure function by application builder.
OverrideConfigure(configureFunc interface{}, builder IApplicationBuilder)
// OverrideNewApplicationBuilder create application builder.
OverrideNewApplicationBuilder() IApplicationBuilder
// OverrideNewHost Create IServiceHost.
OverrideNewHost(server IServer, context *HostBuildContext) IServiceHost
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package YoyoGo
package Abstractions

import "net/http"

Expand Down
13 changes: 10 additions & 3 deletions Framework/IServer.go → Abstractions/IServer.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package YoyoGo
package Abstractions

import "os"
import (
"os"
)

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

type IServer interface {
GetAddr() string
Run(context *HostBuildContext) (e error)
Shutdown()
}

func detectAddress(addr ...string) string {
func DetectAddress(addr ...string) string {
if len(addr) > 0 {
return addr[0]
}
Expand Down
8 changes: 8 additions & 0 deletions Abstractions/IServiceHost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Abstractions

type IServiceHost interface {
Run()
Shutdown()
StopApplicationNotify()
SetAppMode(mode string)
}
48 changes: 48 additions & 0 deletions Abstractions/Platform/exitsignal_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package Platform

import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)

func HookSignals(host Abstract.IServiceHost) {
quitSig := make(chan os.Signal)
signal.Notify(
quitSig,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGSTOP,
syscall.SIGUSR1,
syscall.SIGUSR2,
syscall.SIGKILL,
)

go func() {
var sig os.Signal
for {
sig = <-quitSig
fmt.Println()
switch sig {
case syscall.SIGQUIT:
host.StopApplicationNotify()
host.Shutdown()
os.Exit(0)
// graceful stop
case syscall.SIGHUP:
host.StopApplicationNotify()
host.Shutdown()
case syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM:
host.StopApplicationNotify()
host.Shutdown()
os.Exit(0)
// terminate now
}
time.Sleep(time.Second * 3)
}
}()
}
48 changes: 48 additions & 0 deletions Abstractions/Platform/exitsignal_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package Platform

import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)

func HookSignals(host Abstract.IServiceHost) {
quitSig := make(chan os.Signal)
signal.Notify(
quitSig,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGSTOP,
syscall.SIGUSR1,
syscall.SIGUSR2,
syscall.SIGKILL,
)

go func() {
var sig os.Signal
for {
sig = <-quitSig
fmt.Println()
switch sig {
case syscall.SIGQUIT:
host.StopApplicationNotify()
host.Shutdown()
os.Exit(0)
// graceful stop
case syscall.SIGHUP:
host.StopApplicationNotify()
host.Shutdown()
case syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM:
host.StopApplicationNotify()
host.Shutdown()
os.Exit(0)
// terminate now
}
time.Sleep(time.Second * 3)
}
}()
}
46 changes: 46 additions & 0 deletions Abstractions/Platform/exitsignal_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package Platform

import (
"fmt"
"github.com/maxzhang1985/yoyogo/Abstractions"
"os"
"os/signal"
"syscall"
"time"
)

func HookSignals(host Abstractions.IServiceHost) {
quitSig := make(chan os.Signal)
signal.Notify(
quitSig,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGKILL,
)

go func() {
var sig os.Signal
for {
sig = <-quitSig
fmt.Println()
switch sig {
case syscall.SIGQUIT:
host.StopApplicationNotify()
host.Shutdown()
os.Exit(0)
// graceful stop
case syscall.SIGHUP:
host.StopApplicationNotify()
host.Shutdown()
case syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM:
host.StopApplicationNotify()
host.Shutdown()
os.Exit(0)
// terminate now
}
time.Sleep(time.Second * 3)
}
}()
}
5 changes: 5 additions & 0 deletions Cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package YoyoGo

func main() {
println("hello Yoyo")
}
Loading

0 comments on commit d80adde

Please sign in to comment.