server/pkg/loafer/app/app.go

82 lines
2.0 KiB
Go
Raw Normal View History

2019-11-11 15:00:04 +00:00
package app
import (
2019-11-13 14:21:05 +00:00
"fmt"
2019-11-11 15:00:04 +00:00
"log"
"reflect"
2019-11-13 14:21:05 +00:00
"git.loafle.net/loafer/di-go"
2019-11-11 15:00:04 +00:00
2019-11-11 15:13:14 +00:00
appAnnotation "git.loafle.net/totopia/server/pkg/loafer/app/annotation"
webAnnotation "git.loafle.net/totopia/server/pkg/loafer/web/annotation"
2019-11-11 15:00:04 +00:00
)
2019-11-13 14:21:05 +00:00
type MethodMapping struct {
Method string
ParamKeys []string
}
2019-11-11 15:00:04 +00:00
func Run(t reflect.Type) {
2019-11-13 14:21:05 +00:00
taServer := di.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType)
2019-11-11 15:13:14 +00:00
if nil == taServer {
2019-11-11 15:00:04 +00:00
log.Printf("[%s] is not Server, use @app:Server", t.Elem().Name())
return
}
2019-11-11 15:13:14 +00:00
aServer := taServer.(*appAnnotation.ServerAnnotation)
log.Printf("%s %d", t.Elem().Name(), aServer.HTTPPort)
2019-11-13 14:21:05 +00:00
restHandlers, err := di.GetInstancesByAnnotationType(webAnnotation.RestHandlerAnnotationType)
2019-11-11 15:13:14 +00:00
if nil != err {
log.Printf("[%v]", err)
return
}
for _, restHandler := range restHandlers {
2019-11-13 14:21:05 +00:00
parseRestHandler(restHandler)
parseRequestMapping(restHandler)
}
}
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)
2019-11-11 15:13:14 +00:00
continue
}
2019-11-13 14:21:05 +00:00
mm[ma.Entry] = &MethodMapping{
Method: fmt.Sprintf("%s.%s", t.Elem().Name(), methodName),
ParamKeys: ma.Params,
}
2019-11-11 15:13:14 +00:00
}
2019-11-11 15:00:04 +00:00
2019-11-13 14:21:05 +00:00
log.Printf("%s %v", t.Elem().Name(), methodMapping)
2019-11-11 15:00:04 +00:00
}