87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"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 {
|
|
Method string
|
|
ParamKeys []string
|
|
}
|
|
|
|
func Run(t reflect.Type) error {
|
|
taServer := di.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType)
|
|
if nil == taServer {
|
|
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 {
|
|
return fmt.Errorf("[%v]", err)
|
|
}
|
|
|
|
for _, restHandler := range restHandlers {
|
|
parseRestHandler(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{}) {
|
|
t := reflect.TypeOf(restHandler)
|
|
ta := di.GetTypeAnnotation(t, webAnnotation.RestHandlerAnnotationType)
|
|
if nil == ta {
|
|
log.Printf("Service[%s] is not RESTService, use @RESTService", t.Elem().Name())
|
|
return
|
|
}
|
|
log.Printf("%s %v", t.Elem().Name(), ta)
|
|
}
|
|
|
|
func parseRequestMapping(restHandler interface{}) {
|
|
t := reflect.TypeOf(restHandler)
|
|
mas := di.GetMethodAnnotations(t, webAnnotation.RequestMappingAnnotationType)
|
|
if nil == mas || 0 == len(mas) {
|
|
return
|
|
}
|
|
|
|
methodMapping := make(map[string]map[string]*MethodMapping)
|
|
|
|
for methodName, v := range mas {
|
|
ma := v.(*webAnnotation.RequestMappingAnnotation)
|
|
mm, ok := methodMapping[ma.Method]
|
|
if !ok {
|
|
mm = make(map[string]*MethodMapping)
|
|
methodMapping[ma.Method] = mm
|
|
}
|
|
|
|
_, ok = mm[ma.Entry]
|
|
if ok {
|
|
log.Printf("Mapping of method[%s], entry[%s] is exist already", ma.Method, ma.Entry)
|
|
continue
|
|
}
|
|
|
|
mm[ma.Entry] = &MethodMapping{
|
|
Method: fmt.Sprintf("%s.%s", t.Elem().Name(), methodName),
|
|
}
|
|
}
|
|
|
|
log.Printf("%s %v", t.Elem().Name(), methodMapping)
|
|
}
|