ing
This commit is contained in:
parent
6da88d5c84
commit
b2800a336b
3
go.mod
3
go.mod
|
@ -3,7 +3,8 @@ module git.loafle.net/totopia/server
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da
|
git.loafle.net/loafer/annotation-go v0.0.0-20191113141909-a254c5695d3b
|
||||||
|
git.loafle.net/loafer/di-go v0.0.0-20191113141126-d08fa9dbe72d
|
||||||
github.com/buaazp/fasthttprouter v0.1.1
|
github.com/buaazp/fasthttprouter v0.1.1
|
||||||
github.com/valyala/fasthttp v1.6.0
|
github.com/valyala/fasthttp v1.6.0
|
||||||
)
|
)
|
||||||
|
|
13
go.sum
13
go.sum
|
@ -1,7 +1,12 @@
|
||||||
git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da h1:g0syubB28a2puIRlb/wbKLYSs+5FwszAe0xbixqn8eY=
|
git.loafle.net/loafer/annotation-go v0.0.0-20191113135342-a3974dc21898/go.mod h1:1yow6wwbB3nWq6Asgt3BAPfXJTjZeqgMYF+VVPlj9Xk=
|
||||||
git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da/go.mod h1:k0EtoXnmtf/Qdh1ZLTsR18Zi5hNiGan9fa4/LiNW5z8=
|
git.loafle.net/loafer/annotation-go v0.0.0-20191113141909-a254c5695d3b h1:pWkpKOtuHNIgUFiEpoaX8WpL/7O26nwCEAbsf8UHlHk=
|
||||||
git.loafle.net/commons/util-go v0.0.0-20191105144744-e18f27e20762 h1:H1u26DfvdLJLe7lyjGrAZP0zzmDIjZkSQuo2Oc34Vug=
|
git.loafle.net/loafer/annotation-go v0.0.0-20191113141909-a254c5695d3b/go.mod h1:1yow6wwbB3nWq6Asgt3BAPfXJTjZeqgMYF+VVPlj9Xk=
|
||||||
git.loafle.net/commons/util-go v0.0.0-20191105144744-e18f27e20762/go.mod h1:YgLbLOUN6ODD9ZPrpUWIjWVADlwGGCDmNZRy6yExbR0=
|
git.loafle.net/loafer/di-go v0.0.0-20191113141126-d08fa9dbe72d h1:PG6EhlgwPTAADqlvjuytXcurtyQH/rzxTA6s06yyhlY=
|
||||||
|
git.loafle.net/loafer/di-go v0.0.0-20191113141126-d08fa9dbe72d/go.mod h1:dccC+qAAeeWATVEZ8acCRFvnSGbwxCasnmwez7cCkrY=
|
||||||
|
git.loafle.net/loafer/util-go v0.0.0-20191112142134-9a567d18b779 h1:TcBAz07pghDjiwGLgWrPVbovK4i8gnROGqFIwzxzarA=
|
||||||
|
git.loafle.net/loafer/util-go v0.0.0-20191112142134-9a567d18b779/go.mod h1:HGVw9FNJIc/UFDIzxmoIj5K2+D9Eadal5jjHOq0NFOU=
|
||||||
|
git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d h1:ESDbDHHzH2Ysq+thQrO/OQtyDkVhzNzshjn0SJIqa0g=
|
||||||
|
git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d/go.mod h1:HGVw9FNJIc/UFDIzxmoIj5K2+D9Eadal5jjHOq0NFOU=
|
||||||
github.com/buaazp/fasthttprouter v0.1.1 h1:4oAnN0C3xZjylvZJdP35cxfclyn4TYkW6Y+DSvS+h8Q=
|
github.com/buaazp/fasthttprouter v0.1.1 h1:4oAnN0C3xZjylvZJdP35cxfclyn4TYkW6Y+DSvS+h8Q=
|
||||||
github.com/buaazp/fasthttprouter v0.1.1/go.mod h1:h/Ap5oRVLeItGKTVBb+heQPks+HdIUtGmI4H5WCYijM=
|
github.com/buaazp/fasthttprouter v0.1.1/go.mod h1:h/Ap5oRVLeItGKTVBb+heQPks+HdIUtGmI4H5WCYijM=
|
||||||
github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs=
|
github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs=
|
||||||
|
|
|
@ -3,17 +3,19 @@ package annotation
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
cda "git.loafle.net/commons/di-go/annotation"
|
"git.loafle.net/loafer/annotation-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ServerAnnotationType = reflect.TypeOf((*ServerAnnotation)(nil))
|
var ServerAnnotationType = reflect.TypeOf((*ServerAnnotation)(nil))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cda.RegisterAnnotation(ServerAnnotationType)
|
if err := annotation.Register(ServerAnnotationType); nil != err {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerAnnotation struct {
|
type ServerAnnotation struct {
|
||||||
cda.TypeAnnotation `@annotation:"@app:Server"`
|
annotation.TypeAnnotation `@annotation:"@app:Server"`
|
||||||
|
|
||||||
RootDir string `json:"rootDir"`
|
RootDir string `json:"rootDir"`
|
||||||
Mount map[string][]string `json:"mount"`
|
Mount map[string][]string `json:"mount"`
|
||||||
|
|
|
@ -1,17 +1,23 @@
|
||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
cdr "git.loafle.net/commons/di-go/registry"
|
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MethodMapping struct {
|
||||||
|
Method string
|
||||||
|
ParamKeys []string
|
||||||
|
}
|
||||||
|
|
||||||
func Run(t reflect.Type) {
|
func Run(t reflect.Type) {
|
||||||
taServer := cdr.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())
|
log.Printf("[%s] is not Server, use @app:Server", t.Elem().Name())
|
||||||
return
|
return
|
||||||
|
@ -19,21 +25,57 @@ func Run(t reflect.Type) {
|
||||||
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 := cdr.GetInstancesByAnnotationType(webAnnotation.RestHandlerAnnotationType)
|
restHandlers, err := di.GetInstancesByAnnotationType(webAnnotation.RestHandlerAnnotationType)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
log.Printf("[%v]", err)
|
log.Printf("[%v]", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, restHandler := range restHandlers {
|
for _, restHandler := range restHandlers {
|
||||||
t := reflect.TypeOf(restHandler)
|
parseRestHandler(restHandler)
|
||||||
ta := cdr.GetTypeAnnotation(t, webAnnotation.RestHandlerAnnotationType)
|
parseRequestMapping(restHandler)
|
||||||
if nil == ta {
|
|
||||||
log.Printf("Service[%s] is not RESTService, use @RESTService", t.Elem().Name())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Printf("%s %v", t.Elem().Name(), ta)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
ParamKeys: ma.Params,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%s %v", t.Elem().Name(), methodMapping)
|
||||||
|
}
|
||||||
|
|
|
@ -3,17 +3,19 @@ package annotation
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
cda "git.loafle.net/commons/di-go/annotation"
|
"git.loafle.net/loafer/annotation-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var RequestMappingAnnotationType = reflect.TypeOf((*RequestMappingAnnotation)(nil))
|
var RequestMappingAnnotationType = reflect.TypeOf((*RequestMappingAnnotation)(nil))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cda.RegisterAnnotation(RequestMappingAnnotationType)
|
if err := annotation.Register(RequestMappingAnnotationType); nil != err {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestMappingAnnotation struct {
|
type RequestMappingAnnotation struct {
|
||||||
cda.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"`
|
||||||
|
|
|
@ -3,17 +3,19 @@ package annotation
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
cda "git.loafle.net/commons/di-go/annotation"
|
"git.loafle.net/loafer/annotation-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var RestHandlerAnnotationType = reflect.TypeOf((*RestHandlerAnnotation)(nil))
|
var RestHandlerAnnotationType = reflect.TypeOf((*RestHandlerAnnotation)(nil))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cda.RegisterAnnotation(RestHandlerAnnotationType)
|
if err := annotation.Register(RestHandlerAnnotationType); nil != err {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type RestHandlerAnnotation struct {
|
type RestHandlerAnnotation struct {
|
||||||
cda.TypeAnnotation `@annotation:"@web:RestHandler"`
|
annotation.TypeAnnotation `@annotation:"@web:RestHandler"`
|
||||||
|
|
||||||
Entry string `json:"entry"`
|
Entry string `json:"entry"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,17 +3,19 @@ package annotation
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
cda "git.loafle.net/commons/di-go/annotation"
|
"git.loafle.net/loafer/annotation-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ServiceAnnotationType = reflect.TypeOf((*ServiceAnnotation)(nil))
|
var ServiceAnnotationType = reflect.TypeOf((*ServiceAnnotation)(nil))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cda.RegisterAnnotation(ServiceAnnotationType)
|
if err := annotation.Register(ServiceAnnotationType); nil != err {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServiceAnnotation struct {
|
type ServiceAnnotation struct {
|
||||||
cda.TypeAnnotation `@annotation:"@web:Service"`
|
annotation.TypeAnnotation `@annotation:"@web:Service"`
|
||||||
|
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,8 @@ import (
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
cda "git.loafle.net/commons/di-go/annotation"
|
"git.loafle.net/loafer/annotation-go"
|
||||||
cdr "git.loafle.net/commons/di-go/registry"
|
"git.loafle.net/loafer/di-go"
|
||||||
|
|
||||||
// For annotation
|
// For annotation
|
||||||
_ "git.loafle.net/totopia/server/pkg/loafer/web/annotation"
|
_ "git.loafle.net/totopia/server/pkg/loafer/web/annotation"
|
||||||
|
@ -16,14 +16,14 @@ import (
|
||||||
var UserHandlerType = reflect.TypeOf((*UserHandler)(nil))
|
var UserHandlerType = reflect.TypeOf((*UserHandler)(nil))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cdr.RegisterType(UserHandlerType)
|
di.RegisterType(UserHandlerType)
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserHandler struct {
|
type UserHandler struct {
|
||||||
cda.TypeAnnotation `annotation:"@web:RestHandler(\"entry\": \"/users\")"`
|
annotation.TypeAnnotation `annotation:"@web:RestHandler(\"entry\": \"/users\")"`
|
||||||
|
|
||||||
_List cda.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"params\": [])"`
|
_List annotation.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"params\": [])"`
|
||||||
_Detail cda.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"entry\": \"/:id\", \"params\"=[\"userId\"])"`
|
_Detail annotation.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"entry\": \"/:id\", \"params\":[\"userId\"])"`
|
||||||
|
|
||||||
UserService *UserService `annotation:"@Inject()"`
|
UserService *UserService `annotation:"@Inject()"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,21 +4,20 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
cda "git.loafle.net/commons/di-go/annotation"
|
|
||||||
cdr "git.loafle.net/commons/di-go/registry"
|
|
||||||
|
|
||||||
// For annotation
|
// For annotation
|
||||||
|
"git.loafle.net/loafer/annotation-go"
|
||||||
|
"git.loafle.net/loafer/di-go"
|
||||||
_ "git.loafle.net/totopia/server/pkg/loafer/web/annotation"
|
_ "git.loafle.net/totopia/server/pkg/loafer/web/annotation"
|
||||||
)
|
)
|
||||||
|
|
||||||
var UserServiceType = reflect.TypeOf((*UserService)(nil))
|
var UserServiceType = reflect.TypeOf((*UserService)(nil))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cdr.RegisterType(UserServiceType)
|
di.RegisterType(UserServiceType)
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserService struct {
|
type UserService struct {
|
||||||
cda.TypeAnnotation `annotation:"@web:Service(\"name\": \"/userService\")"`
|
annotation.TypeAnnotation `annotation:"@web:Service(\"name\": \"/userService\")"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UserService) FindAll() {
|
func (us *UserService) FindAll() {
|
||||||
|
|
|
@ -3,8 +3,8 @@ package server
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
cda "git.loafle.net/commons/di-go/annotation"
|
"git.loafle.net/loafer/annotation-go"
|
||||||
cdr "git.loafle.net/commons/di-go/registry"
|
"git.loafle.net/loafer/di-go"
|
||||||
|
|
||||||
// For annotation
|
// For annotation
|
||||||
_ "git.loafle.net/totopia/server/pkg/loafer/app/annotation"
|
_ "git.loafle.net/totopia/server/pkg/loafer/app/annotation"
|
||||||
|
@ -16,9 +16,11 @@ import (
|
||||||
var ServerType = reflect.TypeOf((*Server)(nil))
|
var ServerType = reflect.TypeOf((*Server)(nil))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cdr.RegisterType(ServerType)
|
if err := di.RegisterType(ServerType); nil != err {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
cda.TypeAnnotation `annotation:"@app:Server(\"httpPort\": 8080)"`
|
annotation.TypeAnnotation `annotation:"@app:Server(\"httpPort\": 8080)"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user