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
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))
}

View File

@ -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),
}
}

View File

@ -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"`
}