From d92eac572717240400b6836a301f94193aad4ccb Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 5 Sep 2017 15:37:33 +0900 Subject: [PATCH] refactoring --- backend/client.go | 6 -- backend/options.go | 28 ------ backend/pool.go | 144 ------------------------------- config.json | 45 ++++++++++ glide.yaml | 15 ++-- grpc/client.go | 28 ++++++ grpc/pool.go | 28 ++++++ grpc/pool_handlers.go | 30 +++++++ main.go | 92 +++++++++++--------- module/member/check_email.go | 22 ++--- module/member/forgot_password.go | 27 ++---- module/member/reset_password.go | 36 +++----- module/member/signin.go | 20 +---- module/member/signup.go | 23 ++--- server/context.go | 19 ---- server/handler.go | 6 +- server/server.go | 18 ++-- 17 files changed, 237 insertions(+), 350 deletions(-) delete mode 100644 backend/client.go delete mode 100644 backend/options.go delete mode 100644 backend/pool.go create mode 100644 config.json create mode 100644 grpc/client.go create mode 100644 grpc/pool.go create mode 100644 grpc/pool_handlers.go delete mode 100644 server/context.go diff --git a/backend/client.go b/backend/client.go deleted file mode 100644 index 6381067..0000000 --- a/backend/client.go +++ /dev/null @@ -1,6 +0,0 @@ -package backend - -type PooledClient interface { - Exec(target string, method string, params []string) (string, error) - Close() -} diff --git a/backend/options.go b/backend/options.go deleted file mode 100644 index 492c823..0000000 --- a/backend/options.go +++ /dev/null @@ -1,28 +0,0 @@ -package backend - -import ( - "google.golang.org/grpc" -) - -type Options struct { - // Dial is an application supplied function for creating and configuring a - // grpc connection. - // - // The grpc.ClientConn returned from Dial - Dial func() (*grpc.ClientConn, error) - - // NewClient is an application supplied function for creating and configuring a - // client. - // - // The client returned from NewClient - NewClient func(*grpc.ClientConn) (interface{}, error) - - Exec func(client interface{}, target string, method string, params []string) (string, error) - - // Initial number of clients in the pool. - InitCapacity int - - // Maximum number of clients allocated by the pool at a given time. - // When zero, there is no limit on the number of clients in the pool. - MaxCapacity int -} diff --git a/backend/pool.go b/backend/pool.go deleted file mode 100644 index 27eaf75..0000000 --- a/backend/pool.go +++ /dev/null @@ -1,144 +0,0 @@ -package backend - -import ( - "fmt" - "sync" - - "google.golang.org/grpc" -) - -type Pool interface { - // Get gets a client. The application must close the returned client. - // This method always returns a valid client so that applications can defer - // error handling to the first use of the client. If there is an error - // getting an underlying client, then the client Err, Do, Send, Flush - // and Receive methods return that error. - Get() (PooledClient, error) - - // Capacity returns the number of maximum clients in the pool. - Capacity() int - - // Available returns the number of avaliable clients in the pool. - Available() int - - // Destroy releases the resources used by the pool. - Destroy() -} - -type pool struct { - dial func() (*grpc.ClientConn, error) - newClient func(*grpc.ClientConn) (interface{}, error) - exec func(client interface{}, target string, method string, params []string) (string, error) - initCapacity int - maxCapacity int - - conn *grpc.ClientConn - mtx sync.Mutex - clients chan PooledClient -} - -func NewPool(o Options) (Pool, error) { - if o.Dial == nil { - return nil, fmt.Errorf("invalid Dial settings") - } - - if o.NewClient == nil { - return nil, fmt.Errorf("invalid NewClient settings") - } - - if o.InitCapacity < 0 || o.MaxCapacity < 0 { - return nil, fmt.Errorf("invalid capacity settings") - } - - p := &pool{ - dial: o.Dial, - newClient: o.NewClient, - exec: o.Exec, - initCapacity: o.InitCapacity, - maxCapacity: o.MaxCapacity, - clients: make(chan PooledClient, o.InitCapacity), - } - - var err error - p.conn, err = p.dial() - if nil != err { - return nil, err - } - - for i := 0; i < p.initCapacity; i++ { - pc, err := p.create() - if err != nil { - p.conn.Close() - return nil, err - } - p.clients <- pc - } - - return p, nil -} - -func (p *pool) Capacity() int { - return cap(p.clients) -} - -func (p *pool) Available() int { - return len(p.clients) -} - -func (p *pool) Get() (PooledClient, error) { - if p.clients == nil { - // pool aleardy destroyed, returns new client - return p.create() - } - for { - select { - case c := <-p.clients: - return c, nil - default: - return p.create() - } - } -} - -func (p *pool) Destroy() { - p.mtx.Lock() - defer p.mtx.Unlock() - if p.clients == nil { - // pool aleardy destroyed - return - } - close(p.clients) - p.clients = nil - p.conn.Close() -} - -func (p *pool) create() (PooledClient, error) { - c, err := p.newClient(p.conn) - if err != nil { - return nil, err - } - pc := &pooledClient{ - p: p, - c: c, - } - return pc, nil -} - -type pooledClient struct { - p *pool - c interface{} -} - -func (pc *pooledClient) Exec(target string, method string, params []string) (string, error) { - return pc.p.exec(pc.c, target, method, params) -} - -func (pc *pooledClient) Close() { - select { - case pc.p.clients <- pc: - return - default: - // pool is full, close passed connection - return - } -} diff --git a/config.json b/config.json new file mode 100644 index 0000000..a528c21 --- /dev/null +++ b/config.json @@ -0,0 +1,45 @@ +{ + "server": { + "addr": ":19080", + "tls": false + }, + "auth": { + "signingKey": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" + }, + "grpc": { + "addr": "127.0.0.1:50006", + "tls": false, + "pool": { + "MaxIdle": 1, + "MaxCapacity": 3, + "increaseCapacity": 10 + } + }, + "logging": { + "level": "debug", + "development": true, + "disableCaller": true, + "disableStacktrace": true, + "sampling": { + "initial": 100, + "thereafter": 100 + }, + "encoding": "console", + "encoderConfig": { + "messageKey": "message", + "levelKey": "level", + "timeKey": "time", + "nameKey": "name", + "callerKey": "caller", + "stacktraceKey": "stacktrace", + "lineEnding": "\n", + "levelEncoder": "color", + "timeEncoder": "ISO8601", + "durationEncoder": "string", + "callerEncoder": "full", + "nameEncoder": "full" + }, + "outputPaths": ["stdout", "/tmp/logs"], + "errorOutputPaths": ["stderr"] + } +} \ No newline at end of file diff --git a/glide.yaml b/glide.yaml index ef07ca0..f4ba12e 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,13 +1,14 @@ package: git.loafle.net/overflow/overflow_server_app import: -- package: github.com/valyala/fasthttp - version: v20160617 -- package: github.com/buaazp/fasthttprouter - version: v0.1.1 -- package: google.golang.org/grpc - version: v1.5.2 +- package: git.loafle.net/commons_go/config +- package: git.loafle.net/commons_go/cors_fasthttp +- package: git.loafle.net/commons_go/grpc_pool +- package: git.loafle.net/commons_go/logging - package: git.loafle.net/overflow/overflow_api_server subpackages: - golang +- package: github.com/buaazp/fasthttprouter - package: github.com/dgrijalva/jwt-go - version: v3.0.0 +- package: github.com/valyala/fasthttp +- package: go.uber.org/zap +- package: google.golang.org/grpc diff --git a/grpc/client.go b/grpc/client.go new file mode 100644 index 0000000..e24fef5 --- /dev/null +++ b/grpc/client.go @@ -0,0 +1,28 @@ +package grpc + +import ( + "context" + + oas "git.loafle.net/overflow/overflow_api_server/golang" +) + +func Exec(service string, method string, params []string) (string, error) { + c, err := _pool.Get() + if nil != err { + + } + defer _pool.Put(c) + + si := &oas.ServerInput{ + Target: service, + Method: method, + Params: params, + } + ctx := context.Background() + so, err := c.(oas.OverflowApiServerClient).Exec(ctx, si) + if err != nil { + return "", err + } + + return so.Result, nil +} diff --git a/grpc/pool.go b/grpc/pool.go new file mode 100644 index 0000000..0eb539c --- /dev/null +++ b/grpc/pool.go @@ -0,0 +1,28 @@ +package grpc + +import ( + "context" + "fmt" + + "git.loafle.net/commons_go/config" + cgp "git.loafle.net/commons_go/grpc_pool" + "git.loafle.net/commons_go/logging" +) + +var _pool cgp.Pool + +func InitializePool(ctx context.Context) { + var err error + h := &poolHandlers{ + ctx: ctx, + logger: logging.WithContext(ctx), + } + h.cfg = config.Sub("grpc") + h.MaxIdle = h.cfg.GetInt("pool.MaxIdle") + h.MaxCapacity = h.cfg.GetInt("pool.MaxCapacity") + + _pool, err = cgp.New(ctx, h) + if nil != err { + h.logger.Fatal(fmt.Sprintf("GRpc Pool: %v", err)) + } +} diff --git a/grpc/pool_handlers.go b/grpc/pool_handlers.go new file mode 100644 index 0000000..7d39c48 --- /dev/null +++ b/grpc/pool_handlers.go @@ -0,0 +1,30 @@ +package grpc + +import ( + "context" + + "google.golang.org/grpc" + + "git.loafle.net/commons_go/config" + cgp "git.loafle.net/commons_go/grpc_pool" + oas "git.loafle.net/overflow/overflow_api_server/golang" + "go.uber.org/zap" +) + +type poolHandlers struct { + cgp.PoolHandlers + ctx context.Context + logger *zap.Logger + cfg config.Configurator +} + +func (h *poolHandlers) OnCreate() (*grpc.ClientConn, interface{}, error) { + var err error + conn, err := grpc.Dial(config.GetString("grpc.addr"), grpc.WithInsecure()) + if nil != err { + return nil, nil, err + } + c := oas.NewOverflowApiServerClient(conn) + return conn, c, nil + +} diff --git a/main.go b/main.go index 9accb21..6ac8a57 100644 --- a/main.go +++ b/main.go @@ -2,55 +2,32 @@ package main import ( "context" + "encoding/json" + "log" - "git.loafle.net/overflow/overflow_server_app/backend" + "go.uber.org/zap" + + "git.loafle.net/commons_go/config" + "git.loafle.net/commons_go/cors_fasthttp" + "git.loafle.net/commons_go/logging" + "git.loafle.net/overflow/overflow_server_app/grpc" "git.loafle.net/overflow/overflow_server_app/module/member" "git.loafle.net/overflow/overflow_server_app/server" - "git.loafle.net/commons_go/cors_fasthttp" - - grpcAPI "git.loafle.net/overflow/overflow_api_server/golang" "github.com/valyala/fasthttp" - "google.golang.org/grpc" - ) +var logger *zap.Logger + func main() { - ctx := context.Background() + loadConfig() + ctx := newContext() + defer logger.Sync() + c := cors_fasthttp.AllowAll(ctx) + grpc.InitializePool(ctx) - s := server.New() - - grpcPool, err := backend.NewPool(backend.Options{ - InitCapacity: 2, - MaxCapacity: 4, - Dial: func() (*grpc.ClientConn, error) { - return grpc.Dial("192.168.1.103:50006", grpc.WithInsecure()) - }, - NewClient: func(conn *grpc.ClientConn) (interface{}, error) { - return grpcAPI.NewOverflowApiServerClient(conn), nil - }, - Exec: func(client interface{}, target string, method string, params []string) (string, error) { - ctx := context.Background() - c := client.(grpcAPI.OverflowApiServerClient) - si := &grpcAPI.ServerInput{ - Target: target, - Method: method, - Params: params, - } - so, err := c.Exec(ctx, si) - if nil != err { - return "", err - } - - return so.Result, nil - }, - }) - if nil != err { - - } - - s.SetContextValue("grpc", grpcPool) + s := server.New(ctx) s.Route("POST", "/account/signin", member.SignIn) s.Route("POST", "/account/signup", member.SignUp) @@ -58,9 +35,42 @@ func main() { s.Route("POST", "/account/reset_password", member.ResetPassword) s.Route("GET", "/account/check_email", member.CheckEmail) - fasthttp.ListenAndServe(":19080", c.Handler(s.Handler)) } + +func loadConfig() { + config.SetConfigName("config") + config.AddConfigPath(".") + err := config.ReadInConfig() + if nil != err { + log.Fatalf("config error: %v", err) + } +} + +func newContext() context.Context { + var err error + ctx := context.Background() + logConfig := config.Sub("logging") + + buf, err := logConfig.Marshal("json") + if err != nil { + panic(err) + } + var cfg zap.Config + if err = json.Unmarshal(buf, &cfg); err != nil { + panic(err) + } + + logger, err = cfg.Build() + if err != nil { + panic(err) + } + + ctx = logging.NewContext(ctx, logger) + + return ctx +} + // //func CORS(next server.RequestHandler) server.RequestHandler { // return server.RequestHandler(func(sctx *server.ServerContext, ctx * fasthttp.RequestCtx) { @@ -72,4 +82,4 @@ func main() { // // next(sctx, ctx) // }) -//} \ No newline at end of file +//} diff --git a/module/member/check_email.go b/module/member/check_email.go index b29ace5..19fa11f 100644 --- a/module/member/check_email.go +++ b/module/member/check_email.go @@ -3,30 +3,18 @@ package member import ( "fmt" - "git.loafle.net/overflow/overflow_server_app/server" - - "github.com/valyala/fasthttp" - "git.loafle.net/overflow/overflow_server_app/backend" "net/url" + + "git.loafle.net/overflow/overflow_server_app/grpc" + "github.com/valyala/fasthttp" ) -func CheckEmail(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { - //msg := sctx.Value("key1") - - grpcPool := sctx.Value("grpc").(backend.Pool) - c, err := grpcPool.Get() - if nil != err { - - } - defer c.Close() - - fmt.Fprintf(ctx, "avail: %d\n", grpcPool.Available()) - +func CheckEmail(ctx *fasthttp.RequestCtx) { key := string(ctx.FormValue("key")) params := []string{url.QueryEscape(key)} - r, err := c.Exec("EmailAuthService", "readByAuthKey", params) + r, err := grpc.Exec("EmailAuthService", "readByAuthKey", params) if err != nil { fmt.Fprintf(ctx, "Error!!!!: %s\n", err) diff --git a/module/member/forgot_password.go b/module/member/forgot_password.go index 9299b58..7aecec5 100644 --- a/module/member/forgot_password.go +++ b/module/member/forgot_password.go @@ -3,29 +3,16 @@ package member import ( "fmt" - "git.loafle.net/overflow/overflow_server_app/server" - - "github.com/valyala/fasthttp" - "git.loafle.net/overflow/overflow_server_app/backend" "encoding/json" "log" + + "git.loafle.net/overflow/overflow_server_app/grpc" + "github.com/valyala/fasthttp" ) -func ForgotPassword(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { - //msg := sctx.Value("key1") - // - //fmt.Fprintf(ctx, "Welcome!!!!: %s \n", msg) - - grpcPool := sctx.Value("grpc").(backend.Pool) - c, err := grpcPool.Get() - if nil != err { - - } - defer c.Close() - - fmt.Fprintf(ctx, "avail: %d\n", grpcPool.Available()) - +func ForgotPassword(ctx *fasthttp.RequestCtx) { var ss []interface{} + var err error signinId := ctx.PostBody() fmt.Println(string(signinId)) @@ -41,7 +28,7 @@ func ForgotPassword(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { length := len(ss) - if length < 0{ + if length < 0 { fmt.Println("eeee") } @@ -50,7 +37,7 @@ func ForgotPassword(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { params := []string{dd["signinId"].(string)} fmt.Println(params) - r, err := c.Exec("MemberService", "sendEmailForPassword", params) + r, err := grpc.Exec("MemberService", "sendEmailForPassword", params) ctx.SetContentType("application/javascript") log.Printf("M:%s", r) ctx.SetBody([]byte(r)) diff --git a/module/member/reset_password.go b/module/member/reset_password.go index c967a7a..c3a6a0d 100644 --- a/module/member/reset_password.go +++ b/module/member/reset_password.go @@ -1,32 +1,20 @@ package member import ( - "github.com/valyala/fasthttp" - "git.loafle.net/overflow/overflow_server_app/server" - "git.loafle.net/overflow/overflow_server_app/backend" - "fmt" "encoding/json" - "strings" - "net/url" + "fmt" "log" + "net/url" + "strings" + + "git.loafle.net/overflow/overflow_server_app/grpc" + "github.com/valyala/fasthttp" ) // dZQgXM1o%2FCx48X8DM%2B6ec%2FoPfqA2l%2FLdWtijOZ2EnWk%3D // dZQgXM1o%252FCx48X8DM%252B6ec%252FoPfqA2l%252FLdWtijOZ2EnWk%253D -func ResetPassword(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { - //msg := sctx.Value("key1") - // - //fmt.Fprintf(ctx, "Welcome!!!!: %s \n", msg) - - grpcPool := sctx.Value("grpc").(backend.Pool) - c, err := grpcPool.Get() - if nil != err { - - } - defer c.Close() - - fmt.Fprintf(ctx, "avail: %d\n", grpcPool.Available()) - +func ResetPassword(ctx *fasthttp.RequestCtx) { + var err error //signinId := string(ctx.FormValue("signinId")) //signinPw := string(ctx.FormValue("signinPw")) @@ -40,7 +28,7 @@ func ResetPassword(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { length := len(webParams) - if length < 0{ + if length < 0 { fmt.Println("eeee") } @@ -54,11 +42,11 @@ func ResetPassword(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { fmt.Println("No Escape", keys[1]) fmt.Println("queryEscape", url.QueryEscape(keys[1])) - grpcParams := []string{keys[1], pw } + grpcParams := []string{keys[1], pw} - r, err := c.Exec("MemberService", "resetPassword", grpcParams) + r, err := grpc.Exec("MemberService", "resetPassword", grpcParams) ctx.SetContentType("application/javascript") log.Printf("M:%s", r) ctx.SetBody([]byte(r)) -} \ No newline at end of file +} diff --git a/module/member/signin.go b/module/member/signin.go index 2aec844..cf9fd2c 100644 --- a/module/member/signin.go +++ b/module/member/signin.go @@ -4,30 +4,18 @@ import ( "fmt" "time" - "git.loafle.net/overflow/overflow_server_app/backend" - - "git.loafle.net/overflow/overflow_server_app/server" - "encoding/json" "log" + "git.loafle.net/overflow/overflow_server_app/grpc" jwt "github.com/dgrijalva/jwt-go" "github.com/valyala/fasthttp" ) var ofSigningKey = []byte("secret") -func SignIn(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { - grpcPool := sctx.Value("grpc").(backend.Pool) - fmt.Fprintf(ctx, "avail: %d\n", grpcPool.Available()) - c, err := grpcPool.Get() - if nil != err { - - } - defer c.Close() - - fmt.Fprintf(ctx, "avail: %d\n", grpcPool.Available()) - +func SignIn(ctx *fasthttp.RequestCtx) { + var err error //signinId := string(ctx.FormValue("signinId")) //signinPw := string(ctx.FormValue("signinPw")) @@ -51,7 +39,7 @@ func SignIn(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { params := []string{signinId, signinPw} - r, err := c.Exec("MemberService", "signin", params) + r, err := grpc.Exec("MemberService", "signin", params) if nil != err { fmt.Fprintf(ctx, "%v", err) return diff --git a/module/member/signup.go b/module/member/signup.go index 83e6c68..4a25c74 100644 --- a/module/member/signup.go +++ b/module/member/signup.go @@ -3,11 +3,10 @@ package member import ( "fmt" - "git.loafle.net/overflow/overflow_server_app/server" - - "git.loafle.net/overflow/overflow_server_app/backend" - "github.com/valyala/fasthttp" "encoding/json" + + "git.loafle.net/overflow/overflow_server_app/grpc" + "github.com/valyala/fasthttp" ) type Member struct { @@ -19,16 +18,8 @@ type Member struct { Phone string `json:"phone"` } -func SignUp(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { - grpcPool := sctx.Value("grpc").(backend.Pool) - c, err := grpcPool.Get() - if nil != err { - - } - defer c.Close() - - fmt.Fprintf(ctx, "avail: %d\n", grpcPool.Available()) - +func SignUp(ctx *fasthttp.RequestCtx) { + var err error var webParams []interface{} webBytes := ctx.PostBody() err = json.Unmarshal(webBytes, &webParams) @@ -53,10 +44,10 @@ func SignUp(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { m.Phone = memberMap["phone"].(string) m.Pw = memberMap["pw"].(string) - mm,_ := json.Marshal(m) + mm, _ := json.Marshal(m) params := []string{string(mm), string(m.Pw)} - r, err := c.Exec("MemberService", "signup", params) + r, err := grpc.Exec("MemberService", "signup", params) fmt.Fprintf(ctx, "Welcome!!!!: %s\n", r) } diff --git a/server/context.go b/server/context.go deleted file mode 100644 index fb111fc..0000000 --- a/server/context.go +++ /dev/null @@ -1,19 +0,0 @@ -package server - -type ServerContext struct { - values map[interface{}]interface{} -} - -func (c *ServerContext) Value(key interface{}) interface{} { - if nil == c.values { - return nil - } - return c.values[key] -} - -func (c *ServerContext) setValue(key interface{}, value interface{}) { - if nil == c.values { - c.values = make(map[interface{}]interface{}, 1) - } - c.values[key] = value -} diff --git a/server/handler.go b/server/handler.go index 38a7635..03c6301 100644 --- a/server/handler.go +++ b/server/handler.go @@ -1,5 +1,7 @@ package server -import "github.com/valyala/fasthttp" +import ( + "github.com/valyala/fasthttp" +) -type RequestHandler func(sctx *ServerContext, ctx *fasthttp.RequestCtx) +type RequestHandler func(ctx *fasthttp.RequestCtx) diff --git a/server/server.go b/server/server.go index 5db9f94..5d0c00a 100644 --- a/server/server.go +++ b/server/server.go @@ -1,6 +1,8 @@ package server import ( + "context" + "github.com/buaazp/fasthttprouter" "github.com/valyala/fasthttp" ) @@ -8,27 +10,23 @@ import ( type Server interface { Handler(ctx *fasthttp.RequestCtx) Route(method, path string, handler RequestHandler) - SetContextValue(key interface{}, value interface{}) } type server struct { - ctx *ServerContext + ctx context.Context router *fasthttprouter.Router } -func New() Server { - s := &server{} +func New(ctx context.Context) Server { + s := &server{ + ctx: ctx, + } s.router = fasthttprouter.New() - s.ctx = &ServerContext{} return s } -func (s *server) SetContextValue(key interface{}, value interface{}) { - s.ctx.setValue(key, value) -} - func (s *server) Handler(ctx *fasthttp.RequestCtx) { s.router.Handler(ctx) } @@ -39,6 +37,6 @@ func (s *server) Route(method, path string, handler RequestHandler) { func (s *server) wrapHandler(handler RequestHandler) fasthttp.RequestHandler { return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) { - handler(s.ctx, ctx) + handler(ctx) }) }