This commit is contained in:
병준 박 2019-11-14 00:18:54 +09:00
parent b2800a336b
commit 93e784914b
3 changed files with 15 additions and 19 deletions

View File

@ -1,19 +1,11 @@
package main package main
import ( import (
"log"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
app "git.loafle.net/totopia/server/pkg/loafer/app" app "git.loafle.net/totopia/server/pkg/loafer/app"
"git.loafle.net/totopia/server/pkg/server" "git.loafle.net/totopia/server/pkg/server"
) )
func main() { func main() {
router := fasthttprouter.New()
app.Run(server.ServerType) app.Run(server.ServerType)
log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler))
} }

View File

@ -6,9 +6,10 @@ import (
"reflect" "reflect"
"git.loafle.net/loafer/di-go" "git.loafle.net/loafer/di-go"
appAnnotation "git.loafle.net/totopia/server/pkg/loafer/app/annotation" appAnnotation "git.loafle.net/totopia/server/pkg/loafer/app/annotation"
webAnnotation "git.loafle.net/totopia/server/pkg/loafer/web/annotation" webAnnotation "git.loafle.net/totopia/server/pkg/loafer/web/annotation"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
) )
type MethodMapping struct { type MethodMapping struct {
@ -16,19 +17,18 @@ type MethodMapping struct {
ParamKeys []string ParamKeys []string
} }
func Run(t reflect.Type) { func Run(t reflect.Type) error {
taServer := di.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType) taServer := di.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType)
if nil == taServer { if nil == taServer {
log.Printf("[%s] is not Server, use @app:Server", t.Elem().Name()) return fmt.Errorf("[%s] is not Server, use @app:Server", t.Elem().Name())
return
} }
aServer := taServer.(*appAnnotation.ServerAnnotation) aServer := taServer.(*appAnnotation.ServerAnnotation)
log.Printf("%s %d", t.Elem().Name(), aServer.HTTPPort) log.Printf("%s %d", t.Elem().Name(), aServer.HTTPPort)
restHandlers, err := di.GetInstancesByAnnotationType(webAnnotation.RestHandlerAnnotationType) restHandlers, err := di.GetInstancesByAnnotationType(webAnnotation.RestHandlerAnnotationType)
if nil != err { if nil != err {
log.Printf("[%v]", err) return fmt.Errorf("[%v]", err)
return
} }
for _, restHandler := range restHandlers { for _, restHandler := range restHandlers {
@ -36,6 +36,12 @@ func Run(t reflect.Type) {
parseRequestMapping(restHandler) 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{}) { func parseRestHandler(restHandler interface{}) {
@ -73,7 +79,6 @@ func parseRequestMapping(restHandler interface{}) {
mm[ma.Entry] = &MethodMapping{ mm[ma.Entry] = &MethodMapping{
Method: fmt.Sprintf("%s.%s", t.Elem().Name(), methodName), Method: fmt.Sprintf("%s.%s", t.Elem().Name(), methodName),
ParamKeys: ma.Params,
} }
} }

View File

@ -19,5 +19,4 @@ type RequestMappingAnnotation struct {
Method string `json:"method"` Method string `json:"method"`
Entry string `json:"entry"` Entry string `json:"entry"`
Params []string `json:"params"`
} }