refactoring

This commit is contained in:
crusader 2017-09-05 15:37:33 +09:00
parent 3ad163d316
commit d92eac5727
17 changed files with 237 additions and 350 deletions

View File

@ -1,6 +0,0 @@
package backend
type PooledClient interface {
Exec(target string, method string, params []string) (string, error)
Close()
}

View File

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

View File

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

45
config.json Normal file
View File

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

View File

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

28
grpc/client.go Normal file
View File

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

28
grpc/pool.go Normal file
View File

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

30
grpc/pool_handlers.go Normal file
View File

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

92
main.go
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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