ing
This commit is contained in:
parent
b2800a336b
commit
93e784914b
|
@ -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))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{}) {
|
||||||
|
@ -72,8 +78,7 @@ 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,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,6 @@ func init() {
|
||||||
type RequestMappingAnnotation struct {
|
type RequestMappingAnnotation struct {
|
||||||
annotation.TypeAnnotation `@annotation:"@web:RequestMapping"`
|
annotation.TypeAnnotation `@annotation:"@web:RequestMapping"`
|
||||||
|
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
Entry string `json:"entry"`
|
Entry string `json:"entry"`
|
||||||
Params []string `json:"params"`
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user