This commit is contained in:
crusader 2018-04-06 19:08:38 +09:00
commit 36c1017251
9 changed files with 332 additions and 0 deletions

68
.gitignore vendored Normal file
View File

@ -0,0 +1,68 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
# Gradle:
.idea/**/gradle.xml
.idea/**/libraries
# Mongo Explorer plugin:
.idea/**/mongoSettings.xml
## File-based project format:
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Go template
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
.idea/
*.iml
vendor/
glide.lock
.DS_Store
dist/
debug

32
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,32 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}/main.go",
"env": {},
"args": [],
"showLog": true
},
{
"name": "File Debug",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
}
]
}

4
glide.yaml Normal file
View File

@ -0,0 +1,4 @@
package: git.loafle.net/overflow/member_gateway_rest
import:
- package: git.loafle.net/commons/server-go
- package: git.loafle.net/commons/rpc-go

44
main.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"git.loafle.net/commons/logging-go"
"git.loafle.net/overflow/member_gateway_rest/server"
)
func init() {
logging.InitializeLogger("")
}
func main() {
s := server.NewServer()
go func() {
err := s.ListenAndServe()
if nil != err {
log.Printf("err: %v", err)
}
}()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt,
syscall.SIGKILL,
syscall.SIGSTOP,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
<-interrupt
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
logging.Logger().Errorf("error: %v", err)
}
}

21
server/server-handler.go Normal file
View File

@ -0,0 +1,21 @@
package server
import (
"net"
"git.loafle.net/commons/server-go"
cswf "git.loafle.net/commons/server-go/web/fasthttp"
)
type ServerHandlers struct {
cswf.ServerHandlers
}
func (sh *ServerHandlers) Listener(serverCtx server.ServerCtx) (net.Listener, error) {
l, err := net.Listen("tcp", "192.168.1.101:44449")
if nil != err {
return nil, err
}
return l, nil
}

33
server/server.go Normal file
View File

@ -0,0 +1,33 @@
package server
import (
logging "git.loafle.net/commons/logging-go"
crpj "git.loafle.net/commons/rpc-go/protocol/json"
crr "git.loafle.net/commons/rpc-go/registry"
cswf "git.loafle.net/commons/server-go/web/fasthttp"
"git.loafle.net/overflow/member_gateway_rest/service"
"git.loafle.net/overflow/member_gateway_rest/servlet"
)
func NewServer() *cswf.Server {
serverCodec := crpj.NewServerCodec()
rpcRegistry := crr.NewRPCRegistry()
ms := &service.MemberService{}
if err := rpcRegistry.RegisterService(ms, ""); nil != err {
logging.Logger().Error(err)
}
webappS := &servlet.WebappServlet{}
webappS.ServerCodec = serverCodec
webappS.RPCInvoker = rpcRegistry
sh := &ServerHandlers{}
sh.RegisterServlet("/webapp", webappS)
s := &cswf.Server{
ServerHandler: sh,
}
return s
}

19
service/member-service.go Normal file
View File

@ -0,0 +1,19 @@
package service
import (
"git.loafle.net/commons/server-go"
"github.com/valyala/fasthttp"
)
type MemberService struct {
}
func (ms *MemberService) Signin(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, id string, pw string) (string, error) {
return "dkfksddfk", nil
}
func (ms *MemberService) Register(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, id string, pw string) (string, error) {
return "dkfksddfk", nil
}

106
servlet/rest-servlet.go Normal file
View File

@ -0,0 +1,106 @@
package servlet
import (
"fmt"
crp "git.loafle.net/commons/rpc-go/protocol"
crr "git.loafle.net/commons/rpc-go/registry"
"git.loafle.net/commons/server-go"
csw "git.loafle.net/commons/server-go/web"
cswf "git.loafle.net/commons/server-go/web/fasthttp"
"github.com/valyala/fasthttp"
)
type MethodMapping struct {
Method string
ParamKeys []string
}
type RESTServlet struct {
cswf.Servlets
ServerCodec crp.ServerCodec
RPCInvoker crr.RPCInvoker
MethodMapping map[string]MethodMapping
}
func (s *RESTServlet) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error {
method := string(ctx.Method())
switch method {
case "GET":
return s.HandleGet(servletCtx, ctx)
case "POST":
return s.HandlePost(servletCtx, ctx)
}
return nil
}
func (s *RESTServlet) HandleGet(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error {
path := string(ctx.Path())
if nil == s.MethodMapping {
return csw.NewError(fasthttp.StatusNotFound, fmt.Errorf("Not Found for %s", path))
}
mapping, ok := s.MethodMapping[path]
if !ok {
return csw.NewError(fasthttp.StatusNotFound, fmt.Errorf("Not Found for %s", path))
}
method := mapping.Method
params := make([]string, 0)
if nil != mapping.ParamKeys && 0 < len(mapping.ParamKeys) {
qargs := ctx.QueryArgs()
if nil == qargs {
return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter is not valied"))
}
for _, k := range mapping.ParamKeys {
buf := qargs.Peek(k)
if nil == buf {
return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter for %s is not valied", k))
}
params = append(params, string(buf))
}
}
reqCodec, err := s.ServerCodec.NewRequestWithString(method, params, nil)
if nil != err {
return csw.NewError(fasthttp.StatusBadRequest, err)
}
reply, err := s.RPCInvoker.Invoke(reqCodec, servletCtx, ctx)
buf, err := reqCodec.NewResponse(reply, err)
if nil != err {
return csw.NewError(fasthttp.StatusInternalServerError, err)
}
ctx.SetBody(buf)
return nil
}
func (s *RESTServlet) HandlePost(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error {
buf := ctx.PostBody()
if nil == buf {
return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter is not valied"))
}
reqCodec, err := s.ServerCodec.NewRequest(buf)
if nil != err {
return csw.NewError(fasthttp.StatusBadRequest, err)
}
reply, err := s.RPCInvoker.Invoke(reqCodec, servletCtx, ctx)
buf, err = reqCodec.NewResponse(reply, err)
if nil != err {
return csw.NewError(fasthttp.StatusInternalServerError, err)
}
ctx.SetBody(buf)
return nil
}

View File

@ -0,0 +1,5 @@
package servlet
type WebappServlet struct {
RESTServlet
}