diff --git a/cmd/server/main.go b/cmd/server/main.go index 0897f39..c142e98 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,19 +1,11 @@ package main import ( - "log" - - "github.com/buaazp/fasthttprouter" - "github.com/valyala/fasthttp" - app "git.loafle.net/totopia/server/pkg/loafer/app" "git.loafle.net/totopia/server/pkg/server" ) func main() { - router := fasthttprouter.New() app.Run(server.ServerType) - - log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler)) } diff --git a/pkg/loafer/app/app.go b/pkg/loafer/app/app.go index 3abc26c..ca38a1c 100644 --- a/pkg/loafer/app/app.go +++ b/pkg/loafer/app/app.go @@ -6,9 +6,10 @@ import ( "reflect" "git.loafle.net/loafer/di-go" - appAnnotation "git.loafle.net/totopia/server/pkg/loafer/app/annotation" webAnnotation "git.loafle.net/totopia/server/pkg/loafer/web/annotation" + "github.com/buaazp/fasthttprouter" + "github.com/valyala/fasthttp" ) type MethodMapping struct { @@ -16,19 +17,18 @@ type MethodMapping struct { ParamKeys []string } -func Run(t reflect.Type) { +func Run(t reflect.Type) error { taServer := di.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType) if nil == taServer { - log.Printf("[%s] is not Server, use @app:Server", t.Elem().Name()) - return + return fmt.Errorf("[%s] is not Server, use @app:Server", t.Elem().Name()) + } aServer := taServer.(*appAnnotation.ServerAnnotation) log.Printf("%s %d", t.Elem().Name(), aServer.HTTPPort) restHandlers, err := di.GetInstancesByAnnotationType(webAnnotation.RestHandlerAnnotationType) if nil != err { - log.Printf("[%v]", err) - return + return fmt.Errorf("[%v]", err) } for _, restHandler := range restHandlers { @@ -36,6 +36,12 @@ func Run(t reflect.Type) { parseRequestMapping(restHandler) } + router := fasthttprouter.New() + if err := fasthttp.ListenAndServe(fmt.Sprintf(":%d", aServer.HTTPPort), router.Handler); nil != err { + return err + } + + return nil } func parseRestHandler(restHandler interface{}) { @@ -72,8 +78,7 @@ func parseRequestMapping(restHandler interface{}) { } mm[ma.Entry] = &MethodMapping{ - Method: fmt.Sprintf("%s.%s", t.Elem().Name(), methodName), - ParamKeys: ma.Params, + Method: fmt.Sprintf("%s.%s", t.Elem().Name(), methodName), } } diff --git a/pkg/loafer/web/annotation/request-mapping.go b/pkg/loafer/web/annotation/request-mapping.go index e8be645..bc3f10e 100644 --- a/pkg/loafer/web/annotation/request-mapping.go +++ b/pkg/loafer/web/annotation/request-mapping.go @@ -17,7 +17,6 @@ func init() { type RequestMappingAnnotation struct { annotation.TypeAnnotation `@annotation:"@web:RequestMapping"` - Method string `json:"method"` - Entry string `json:"entry"` - Params []string `json:"params"` + Method string `json:"method"` + Entry string `json:"entry"` }