From 3fb3b68cc584058e767a1fb65258e4b382ccbdce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=99=E7=89=9B=E6=97=A0=E4=BA=BA=E9=9B=B6=E5=94=AE?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE?= Date: Sun, 19 Jan 2020 12:04:11 +0800 Subject: [PATCH 1/4] get local ip to env.host --- Context/HostEnvironment.go | 1 + Framework/HostBuilder.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Context/HostEnvironment.go b/Context/HostEnvironment.go index 7dd8394d..845f3297 100644 --- a/Context/HostEnvironment.go +++ b/Context/HostEnvironment.go @@ -13,6 +13,7 @@ type HostEnvironment struct { Args []string Addr string Port string + Host string PID int } diff --git a/Framework/HostBuilder.go b/Framework/HostBuilder.go index 97dd94d9..7af254d5 100644 --- a/Framework/HostBuilder.go +++ b/Framework/HostBuilder.go @@ -1,8 +1,10 @@ package YoyoGo import ( + "fmt" "github.com/maxzhang1985/yoyogo/Context" "github.com/maxzhang1985/yoyogo/DependencyInjection" + "net" "os" ) @@ -34,20 +36,42 @@ func (self *HostBuilder) UseServer(server IServer) *HostBuilder { return self } +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 +} + func runningHostEnvironmentSetting(hostEnv *Context.HostEnvironment) { + hostEnv.Host = getLocalIP() hostEnv.Port = detectAddress(hostEnv.Addr) hostEnv.PID = os.Getpid() } func buildingHostEnvironmentSetting(hostEnv *Context.HostEnvironment) { // build each configuration by init , such as file or env or args ... - hostEnv.Args = os.Args hostEnv.ApplicationName = "app" hostEnv.Version = Version + hostEnv.Addr = ":8080" + + hostEnv.Args = os.Args + if hostEnv.Profile == "" { hostEnv.Profile = Context.Dev } - hostEnv.Addr = ":8080" + } func (self *HostBuilder) Build() WebHost { From 7db1a9c403e3d2a5e1a32c198f0b7fe9b3c5336b Mon Sep 17 00:00:00 2001 From: maxzhang1985 Date: Tue, 21 Jan 2020 10:59:28 +0800 Subject: [PATCH 2/4] if not handler processing this request was not found (HTTP 404 NotFound),that return 400 for void middleware. --- Examples/SimpleWeb/main.go | 10 ++++------ Framework/Middleware.go | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Examples/SimpleWeb/main.go b/Examples/SimpleWeb/main.go index e03fc33c..7ae55bdd 100644 --- a/Examples/SimpleWeb/main.go +++ b/Examples/SimpleWeb/main.go @@ -3,9 +3,7 @@ package main import ( "fmt" "github.com/maxzhang1985/yoyogo/Context" - "github.com/maxzhang1985/yoyogo/Controller" "github.com/maxzhang1985/yoyogo/DependencyInjection" - "github.com/maxzhang1985/yoyogo/Examples/SimpleWeb/contollers" "github.com/maxzhang1985/yoyogo/Examples/SimpleWeb/models" "github.com/maxzhang1985/yoyogo/Framework" "github.com/maxzhang1985/yoyogo/Router" @@ -26,11 +24,11 @@ func CreateCustomBuilder() *YoyoGo.HostBuilder { Configure(func(app *YoyoGo.ApplicationBuilder) { //app.SetEnvironment(Context.Prod) app.UseStatic("Static") - app.UseMvc() + //app.UseMvc() app.UseEndpoints(registerEndpointRouterConfig) - app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) { - builder.AddController(contollers.NewUserController) - }) + //app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) { + // builder.AddController(contollers.NewUserController) + //}) }). ConfigureServices(func(serviceCollection *DependencyInjection.ServiceCollection) { serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction)) diff --git a/Framework/Middleware.go b/Framework/Middleware.go index 1aebf313..6eb530f8 100644 --- a/Framework/Middleware.go +++ b/Framework/Middleware.go @@ -51,7 +51,7 @@ func wrapFunc(handlerFunc http.HandlerFunc) Handler { func voidMiddleware() middleware { return newMiddleware( - HandlerFunc(func(ctx *Context.HttpContext, next func(ctx *Context.HttpContext)) {}), + HandlerFunc(func(ctx *Context.HttpContext, next func(ctx *Context.HttpContext)) { ctx.Response.WriteHeader(400) }), &middleware{}, ) } From 052fdedeedfc7a33a490af99a7f85e42fc29e9c9 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Fri, 7 Feb 2020 16:29:21 +0800 Subject: [PATCH 3/4] mvc demo --- Examples/SimpleWeb/main.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Examples/SimpleWeb/main.go b/Examples/SimpleWeb/main.go index 7ae55bdd..429c2168 100644 --- a/Examples/SimpleWeb/main.go +++ b/Examples/SimpleWeb/main.go @@ -3,7 +3,9 @@ package main import ( "fmt" "github.com/maxzhang1985/yoyogo/Context" + "github.com/maxzhang1985/yoyogo/Controller" "github.com/maxzhang1985/yoyogo/DependencyInjection" + "github.com/maxzhang1985/yoyogo/Examples/SimpleWeb/contollers" "github.com/maxzhang1985/yoyogo/Examples/SimpleWeb/models" "github.com/maxzhang1985/yoyogo/Framework" "github.com/maxzhang1985/yoyogo/Router" @@ -24,11 +26,12 @@ func CreateCustomBuilder() *YoyoGo.HostBuilder { Configure(func(app *YoyoGo.ApplicationBuilder) { //app.SetEnvironment(Context.Prod) app.UseStatic("Static") - //app.UseMvc() app.UseEndpoints(registerEndpointRouterConfig) - //app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) { - // builder.AddController(contollers.NewUserController) - //}) + + app.UseMvc() + app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) { + builder.AddController(contollers.NewUserController) + }) }). ConfigureServices(func(serviceCollection *DependencyInjection.ServiceCollection) { serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction)) From 8ec5717bd71fa0ded518f29f11164efeaa026269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=99=E7=89=9B=E6=97=A0=E4=BA=BA=E9=9B=B6=E5=94=AE?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE?= Date: Tue, 14 Apr 2020 14:46:15 +0800 Subject: [PATCH 4/4] v1.4.8.2 Optimize error prompts for controllers and actions --- Controller/ActionMethodExecutor.go | 5 +++++ Controller/ControllerActivator.go | 7 ++----- Examples/SimpleWeb/main.go | 2 +- Framework/Middleware.go | 6 +++++- Framework/Version.go | 2 +- Middleware/RecoveryMiddleware.go | 28 +++++++++++++++------------- Router/MvcRouterHandler.go | 7 ++++++- 7 files changed, 35 insertions(+), 22 deletions(-) diff --git a/Controller/ActionMethodExecutor.go b/Controller/ActionMethodExecutor.go index beb4f76a..9b38cdf4 100644 --- a/Controller/ActionMethodExecutor.go +++ b/Controller/ActionMethodExecutor.go @@ -3,6 +3,7 @@ package Controller import ( "github.com/maxzhang1985/yoyogo/Context" "github.com/maxzhang1985/yoyogo/Utils" + "net/http" "reflect" ) @@ -17,6 +18,10 @@ func (actionExecutor ActionMethodExecutor) Execute(ctx *ActionExecutorContext) i if ctx.Controller != nil { if ctx.In.MethodInovker == nil { ctx.In.MethodInovker = Utils.NewMethodCaller(ctx.Controller, ctx.ActionName) + if ctx.In.MethodInovker == nil { + ctx.Context.Response.WriteHeader(http.StatusNotFound) + panic(ctx.ActionName + " action is not found! at " + ctx.ControllerName) + } ctx.In.ActionParamTypes = ctx.In.MethodInovker.GetParamTypes() } diff --git a/Controller/ControllerActivator.go b/Controller/ControllerActivator.go index 9e8f1ad2..b0e57544 100644 --- a/Controller/ControllerActivator.go +++ b/Controller/ControllerActivator.go @@ -2,11 +2,8 @@ package Controller import "github.com/maxzhang1985/yoyogo/DependencyInjection" -func ActivateController(serviceProvider DependencyInjection.IServiceProvider, controllerName string) IController { +func ActivateController(serviceProvider DependencyInjection.IServiceProvider, controllerName string) (IController, error) { var controller IController err := serviceProvider.GetServiceByName(&controller, controllerName) - if err != nil { - panic("Controller not found! " + err.Error()) - } - return controller + return controller, err } diff --git a/Examples/SimpleWeb/main.go b/Examples/SimpleWeb/main.go index 429c2168..f0690e9c 100644 --- a/Examples/SimpleWeb/main.go +++ b/Examples/SimpleWeb/main.go @@ -24,7 +24,7 @@ func CreateCustomBuilder() *YoyoGo.HostBuilder { UseFastHttp(). //UseServer(YoyoGo.DefaultHttps(":8080", "./Certificate/server.pem", "./Certificate/server.key")). Configure(func(app *YoyoGo.ApplicationBuilder) { - //app.SetEnvironment(Context.Prod) + app.SetEnvironment(Context.Dev) app.UseStatic("Static") app.UseEndpoints(registerEndpointRouterConfig) diff --git a/Framework/Middleware.go b/Framework/Middleware.go index 6eb530f8..749d4032 100644 --- a/Framework/Middleware.go +++ b/Framework/Middleware.go @@ -51,7 +51,11 @@ func wrapFunc(handlerFunc http.HandlerFunc) Handler { func voidMiddleware() middleware { return newMiddleware( - HandlerFunc(func(ctx *Context.HttpContext, next func(ctx *Context.HttpContext)) { ctx.Response.WriteHeader(400) }), + HandlerFunc(func(ctx *Context.HttpContext, next func(ctx *Context.HttpContext)) { + if ctx.Response.Status() < 200 { + ctx.Response.WriteHeader(400) + } + }), &middleware{}, ) } diff --git a/Framework/Version.go b/Framework/Version.go index 80604f01..fe52dc22 100644 --- a/Framework/Version.go +++ b/Framework/Version.go @@ -1,5 +1,5 @@ package YoyoGo const ( - Version = "v1.4.8" + Version = "v1.4.8.2" ) diff --git a/Middleware/RecoveryMiddleware.go b/Middleware/RecoveryMiddleware.go index efd110dc..462ede6b 100644 --- a/Middleware/RecoveryMiddleware.go +++ b/Middleware/RecoveryMiddleware.go @@ -15,11 +15,7 @@ const ( // NoPrintStackBodyString is the body content returned when HTTP stack printing is suppressed NoPrintStackBodyString = "500 Internal Server Error" - panicText = `{ - Result:0, - Data: 'error', - Message:'%s' -}` + panicText = `{ "Result": -1 , "Data": "error", "Message": "%s" }` panicHTML = ` PANIC: {{.RecoveredPanic}} @@ -159,25 +155,35 @@ func NewRecovery() *Recovery { } func (rec *Recovery) Inovke(ctx *Context.HttpContext, next func(ctx *Context.HttpContext)) { + defer func() { if err := recover(); err != nil { var hostEnv *Context.HostEnvironment envErr := ctx.RequiredServices.GetService(&hostEnv) - ctx.Response.WriteHeader(http.StatusInternalServerError) + if envErr == nil && hostEnv.IsDevelopment() { rec.PrintStack = true rec.LogStack = true rec.StackAll = true rec.Formatter = &HTMLPanicFormatter{} } + if ctx.Response.Status() != http.StatusNotFound { + ctx.Response.WriteHeader(http.StatusInternalServerError) + } stack := make([]byte, rec.StackSize) - stack = stack[:runtime.Stack(stack, rec.StackAll)] + if rec.StackAll { + stack = stack[:runtime.Stack(stack, rec.StackAll)] + } infos := &PanicInformation{RecoveredPanic: err, Request: ctx.Request} // PrintStack will write stack trace info to the ResponseWriter if set to true! - // If set to false it will respond with the standard response documented here https://httpstat.us/500 - if rec.PrintStack { + if rec.LogStack { infos.Stack = stack + //print console stack errors + rec.Logger.Printf(panicText, err, string(stack)) + } + + if rec.PrintStack { rec.Formatter.FormatPanicError(ctx.Response, ctx.Request, infos) } else { if ctx.Response.Header().Get("Content-Type") == "" { @@ -186,10 +192,6 @@ func (rec *Recovery) Inovke(ctx *Context.HttpContext, next func(ctx *Context.Htt _, _ = fmt.Fprint(ctx.Response, NoPrintStackBodyString) } - if rec.LogStack { - rec.Logger.Printf(panicText, err, string(stack)) - } - if rec.PanicHandlerFunc != nil { func() { defer func() { diff --git a/Router/MvcRouterHandler.go b/Router/MvcRouterHandler.go index 6bc9ea13..6f31c762 100644 --- a/Router/MvcRouterHandler.go +++ b/Router/MvcRouterHandler.go @@ -4,6 +4,7 @@ import ( "github.com/maxzhang1985/yoyogo/ActionResult" "github.com/maxzhang1985/yoyogo/Context" "github.com/maxzhang1985/yoyogo/Controller" + "net/http" "strings" ) @@ -21,7 +22,11 @@ func (handler *MvcRouterHandler) Invoke(ctx *Context.HttpContext, pathComponents } actionName := pathComponents[1] - controller := Controller.ActivateController(ctx.RequiredServices, controllerName) + controller, err := Controller.ActivateController(ctx.RequiredServices, controllerName) + if err != nil { + ctx.Response.WriteHeader(http.StatusNotFound) + panic(controllerName + " controller is not found! " + err.Error()) + } executorContext := &Controller.ActionExecutorContext{ ControllerName: controllerName,