ing
This commit is contained in:
parent
73ea7e41cc
commit
29086dfa02
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
__debug_bin
|
|
@ -6,14 +6,14 @@ import (
|
|||
"github.com/buaazp/fasthttprouter"
|
||||
"github.com/valyala/fasthttp"
|
||||
|
||||
userHandlers "git.loafle.net/totopia/server/pkg/modules/user/handlers"
|
||||
app "git.loafle.net/totopia/server/pkg/loafer/app"
|
||||
"git.loafle.net/totopia/server/pkg/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := fasthttprouter.New()
|
||||
|
||||
router.GET("/user/users", userHandlers.GetUsers)
|
||||
router.POST("/user/users", userHandlers.PostUsers)
|
||||
app.Run(server.ServerType)
|
||||
|
||||
log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler))
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module git.loafle.net/totopia/server
|
|||
go 1.13
|
||||
|
||||
require (
|
||||
git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da
|
||||
github.com/buaazp/fasthttprouter v0.1.1
|
||||
github.com/valyala/fasthttp v1.6.0
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,3 +1,7 @@
|
|||
git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da h1:g0syubB28a2puIRlb/wbKLYSs+5FwszAe0xbixqn8eY=
|
||||
git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da/go.mod h1:k0EtoXnmtf/Qdh1ZLTsR18Zi5hNiGan9fa4/LiNW5z8=
|
||||
git.loafle.net/commons/util-go v0.0.0-20191105144744-e18f27e20762 h1:H1u26DfvdLJLe7lyjGrAZP0zzmDIjZkSQuo2Oc34Vug=
|
||||
git.loafle.net/commons/util-go v0.0.0-20191105144744-e18f27e20762/go.mod h1:YgLbLOUN6ODD9ZPrpUWIjWVADlwGGCDmNZRy6yExbR0=
|
||||
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/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs=
|
||||
|
|
23
pkg/loafer/app/annotation/server.go
Normal file
23
pkg/loafer/app/annotation/server.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package annotation
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
cda "git.loafle.net/commons/di-go/annotation"
|
||||
)
|
||||
|
||||
var ServerAnnotationType = reflect.TypeOf((*ServerAnnotation)(nil))
|
||||
|
||||
func init() {
|
||||
cda.RegisterAnnotation(ServerAnnotationType)
|
||||
}
|
||||
|
||||
type ServerAnnotation struct {
|
||||
cda.TypeAnnotation `@annotation:"@app:Server"`
|
||||
|
||||
RootDir string `json:"rootDir"`
|
||||
Mount map[string][]string `json:"mount"`
|
||||
UploadDir string `json:"uploadDir"`
|
||||
HTTPPort int16 `json:"httpPort"`
|
||||
HTTPSPort int16 `json:"httpsPort"`
|
||||
}
|
21
pkg/loafer/app/app.go
Normal file
21
pkg/loafer/app/app.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
cdr "git.loafle.net/commons/di-go/registry"
|
||||
|
||||
annotation "git.loafle.net/totopia/server/pkg/loafer/app/annotation"
|
||||
)
|
||||
|
||||
func Run(t reflect.Type) {
|
||||
ta := cdr.GetTypeAnnotation(t, annotation.ServerAnnotationType)
|
||||
if nil == ta {
|
||||
log.Printf("[%s] is not Server, use @app:Server", t.Elem().Name())
|
||||
return
|
||||
}
|
||||
vta := ta.(*annotation.ServerAnnotation)
|
||||
log.Printf("%s %d", t.Elem().Name(), vta.HTTPPort)
|
||||
|
||||
}
|
21
pkg/loafer/web/annotation/request-mapping.go
Normal file
21
pkg/loafer/web/annotation/request-mapping.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package annotation
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
cda "git.loafle.net/commons/di-go/annotation"
|
||||
)
|
||||
|
||||
var RequestMappingAnnotationType = reflect.TypeOf((*RequestMappingAnnotation)(nil))
|
||||
|
||||
func init() {
|
||||
cda.RegisterAnnotation(RequestMappingAnnotationType)
|
||||
}
|
||||
|
||||
type RequestMappingAnnotation struct {
|
||||
cda.TypeAnnotation `@annotation:"@web:RequestMapping"`
|
||||
|
||||
Method string `json:"method"`
|
||||
Entry string `json:"entry"`
|
||||
Params []string `json:"params"`
|
||||
}
|
19
pkg/loafer/web/annotation/rest-handler.go
Normal file
19
pkg/loafer/web/annotation/rest-handler.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package annotation
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
cda "git.loafle.net/commons/di-go/annotation"
|
||||
)
|
||||
|
||||
var RestHandlerAnnotationType = reflect.TypeOf((*RestHandlerAnnotation)(nil))
|
||||
|
||||
func init() {
|
||||
cda.RegisterAnnotation(RestHandlerAnnotationType)
|
||||
}
|
||||
|
||||
type RestHandlerAnnotation struct {
|
||||
cda.TypeAnnotation `@annotation:"@web:RestHandler"`
|
||||
|
||||
Entry string `json:"entry"`
|
||||
}
|
19
pkg/loafer/web/annotation/service.go
Normal file
19
pkg/loafer/web/annotation/service.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package annotation
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
cda "git.loafle.net/commons/di-go/annotation"
|
||||
)
|
||||
|
||||
var ServiceAnnotationType = reflect.TypeOf((*ServiceAnnotation)(nil))
|
||||
|
||||
func init() {
|
||||
cda.RegisterAnnotation(ServiceAnnotationType)
|
||||
}
|
||||
|
||||
type ServiceAnnotation struct {
|
||||
cda.TypeAnnotation `@annotation:"@web:Service"`
|
||||
|
||||
Name string `json:"name"`
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package users
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func GetUsers(ctx *fasthttp.RequestCtx) {
|
||||
fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name"))
|
||||
}
|
||||
|
||||
func PostUsers(ctx *fasthttp.RequestCtx) {
|
||||
fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name"))
|
||||
}
|
41
pkg/modules/user/user-handler.go
Normal file
41
pkg/modules/user/user-handler.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
|
||||
cda "git.loafle.net/commons/di-go/annotation"
|
||||
cdr "git.loafle.net/commons/di-go/registry"
|
||||
|
||||
// For annotation
|
||||
_ "git.loafle.net/totopia/server/pkg/loafer/web/annotation"
|
||||
)
|
||||
|
||||
var UserHandlerType = reflect.TypeOf((*UserHandler)(nil))
|
||||
|
||||
func init() {
|
||||
cdr.RegisterType(UserHandlerType)
|
||||
}
|
||||
|
||||
type UserHandler struct {
|
||||
cda.TypeAnnotation `annotation:"@web:RestHandler(\"entry\": \"/users\")"`
|
||||
|
||||
_List cda.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"params\": [])"`
|
||||
_Detail cda.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"entry\": \"/:id\", \"params\"=[\"userId\"])"`
|
||||
|
||||
UserService *UserService `annotation:"@Inject()"`
|
||||
}
|
||||
|
||||
func (us *UserHandler) List(ctx *fasthttp.RequestCtx) error {
|
||||
fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name"))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserHandler) Detail(ctx *fasthttp.RequestCtx, userId string) error {
|
||||
fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name"))
|
||||
|
||||
return nil
|
||||
}
|
26
pkg/modules/user/user-service.go
Normal file
26
pkg/modules/user/user-service.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
cda "git.loafle.net/commons/di-go/annotation"
|
||||
cdr "git.loafle.net/commons/di-go/registry"
|
||||
|
||||
// For annotation
|
||||
_ "git.loafle.net/totopia/server/pkg/loafer/web/annotation"
|
||||
)
|
||||
|
||||
var UserServiceType = reflect.TypeOf((*UserService)(nil))
|
||||
|
||||
func init() {
|
||||
cdr.RegisterType(UserServiceType)
|
||||
}
|
||||
|
||||
type UserService struct {
|
||||
cda.TypeAnnotation `annotation:"@web:Service(\"name\": \"/userService\")"`
|
||||
}
|
||||
|
||||
func (us *UserService) FindAll() {
|
||||
log.Printf("UserService FindAll")
|
||||
}
|
21
pkg/server/server.go
Normal file
21
pkg/server/server.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
cda "git.loafle.net/commons/di-go/annotation"
|
||||
cdr "git.loafle.net/commons/di-go/registry"
|
||||
|
||||
// For annotation
|
||||
_ "git.loafle.net/totopia/server/pkg/loafer/app/annotation"
|
||||
)
|
||||
|
||||
var ServerType = reflect.TypeOf((*Server)(nil))
|
||||
|
||||
func init() {
|
||||
cdr.RegisterType(ServerType)
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
cda.TypeAnnotation `annotation:"@app:Server(\"httpPort\": 8080)"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user