-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from maxzhang1985/dev
v1.4.9.5
- Loading branch information
Showing
79 changed files
with
727 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,7 @@ | |
|
||
.idea | ||
|
||
.vscode | ||
|
||
go.sum | ||
__debug_bin |
2 changes: 1 addition & 1 deletion
2
Framework/ApplicationEventPublisher.go → Abstractions/ApplicationEventPublisher.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package YoyoGo | ||
package Abstractions | ||
|
||
import "sync" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Framework/ConfigurationBuilder.go → Abstractions/ConfigurationBuilder.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package YoyoGo | ||
package Abstractions | ||
|
||
type ConfigurationContext struct { | ||
enableFlag bool | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package Abstractions | ||
|
||
type IApplicationBuilder interface { | ||
Build() interface{} | ||
SetHostBuildContext(*HostBuildContext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package YoyoGo | ||
package Abstractions | ||
|
||
type IConfiguration interface { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
2 changes: 1 addition & 1 deletion
2
Framework/IRequestDelegate.go → Abstractions/IRequestDelegate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package YoyoGo | ||
package Abstractions | ||
|
||
import "net/http" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package YoyoGo | ||
|
||
func main() { | ||
println("hello Yoyo") | ||
} |
Oops, something went wrong.