commit 36c101725107b02ce863253a0ad05472d233f4da Author: crusader Date: Fri Apr 6 19:08:38 2018 +0900 ing diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3733e36 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2ca2b1d --- /dev/null +++ b/.vscode/launch.json @@ -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 + } + + ] +} \ No newline at end of file diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..abeed13 --- /dev/null +++ b/glide.yaml @@ -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 diff --git a/main.go b/main.go new file mode 100644 index 0000000..d3b2cbe --- /dev/null +++ b/main.go @@ -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) + } +} diff --git a/server/server-handler.go b/server/server-handler.go new file mode 100644 index 0000000..673196a --- /dev/null +++ b/server/server-handler.go @@ -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 +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..eda538e --- /dev/null +++ b/server/server.go @@ -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 +} diff --git a/service/member-service.go b/service/member-service.go new file mode 100644 index 0000000..f9824da --- /dev/null +++ b/service/member-service.go @@ -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 +} diff --git a/servlet/rest-servlet.go b/servlet/rest-servlet.go new file mode 100644 index 0000000..434fa06 --- /dev/null +++ b/servlet/rest-servlet.go @@ -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 +} diff --git a/servlet/webapp-servlet.go b/servlet/webapp-servlet.go new file mode 100644 index 0000000..dbebd54 --- /dev/null +++ b/servlet/webapp-servlet.go @@ -0,0 +1,5 @@ +package servlet + +type WebappServlet struct { + RESTServlet +}