Skip to content

Commit

Permalink
Merge pull request #38 from maxzhang1985/dev
Browse files Browse the repository at this point in the history
 v1.4.8.2
  • Loading branch information
yoyofx authored Apr 14, 2020
2 parents cb70cc0 + 8ec5717 commit 1c5bd5c
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 25 deletions.
1 change: 1 addition & 0 deletions Context/HostEnvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type HostEnvironment struct {
Args []string
Addr string
Port string
Host string
PID int
}

Expand Down
5 changes: 5 additions & 0 deletions Controller/ActionMethodExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Controller
import (
"github.com/maxzhang1985/yoyogo/Context"
"github.com/maxzhang1985/yoyogo/Utils"
"net/http"
"reflect"
)

Expand All @@ -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()
}

Expand Down
7 changes: 2 additions & 5 deletions Controller/ControllerActivator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions Examples/SimpleWeb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ 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.UseMvc()
app.UseEndpoints(registerEndpointRouterConfig)

app.UseMvc()
app.ConfigureMvcParts(func(builder *Controller.ControllerBuilder) {
builder.AddController(contollers.NewUserController)
})
Expand Down
28 changes: 26 additions & 2 deletions Framework/HostBuilder.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package YoyoGo

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

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion Framework/Middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {}),
HandlerFunc(func(ctx *Context.HttpContext, next func(ctx *Context.HttpContext)) {
if ctx.Response.Status() < 200 {
ctx.Response.WriteHeader(400)
}
}),
&middleware{},
)
}
Expand Down
2 changes: 1 addition & 1 deletion Framework/Version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package YoyoGo

const (
Version = "v1.4.8"
Version = "v1.4.8.2"
)
28 changes: 15 additions & 13 deletions Middleware/RecoveryMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<html>
<head><title>PANIC: {{.RecoveredPanic}}</title></head>
Expand Down Expand Up @@ -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") == "" {
Expand All @@ -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() {
Expand Down
7 changes: 6 additions & 1 deletion Router/MvcRouterHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/maxzhang1985/yoyogo/ActionResult"
"github.com/maxzhang1985/yoyogo/Context"
"github.com/maxzhang1985/yoyogo/Controller"
"net/http"
"strings"
)

Expand All @@ -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,
Expand Down

0 comments on commit 1c5bd5c

Please sign in to comment.