This commit is contained in:
병준 박 2019-11-12 00:00:04 +09:00
parent 73ea7e41cc
commit 29086dfa02
13 changed files with 200 additions and 18 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__debug_bin

View File

@ -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
View File

@ -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
View File

@ -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=

View 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
View 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)
}

View 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"`
}

View 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"`
}

View 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"`
}

View File

@ -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"))
}

View 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
}

View 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
View 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)"`
}