This commit is contained in:
crusader 2017-11-23 17:06:44 +09:00
parent 1c16846b8c
commit 1639b86706
17 changed files with 175 additions and 187 deletions

View File

@ -1,45 +1,23 @@
{
"server": {
"name": "Gateway Web APP",
"network": "tcp4",
"addr": ":19080",
"tls": false
"tls": false,
"concurrency": 262144,
"maxStopWaitTime": 0
},
"auth": {
"signingKey": "tWB0lUXiCwX4U3qsJZcZ10mKvEH793RHkTJDbDuZVshQTk4uNB6ck59UQ96lhsRi4XNUiEnlIbP8XYQMPabeNtERX3iyHeDcwocgUVAor1nkAajYeq1gNyJszGpMhEOT"
},
"grpc": {
"gRPC": {
"addr": "192.168.1.50:50006",
"tls": false,
"pool": {
"MaxIdle": 1,
"MaxCapacity": 3,
"increaseCapacity": 10
"maxIdle": 1,
"maxCapacity": 3,
"idleTimeout": 240,
"wait": false
}
},
"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"]
}
}

13
config/config.go Normal file
View File

@ -0,0 +1,13 @@
package config
import (
oosc "git.loafle.net/overflow/overflow_server_config"
)
var Config ServerAppConfig
type ServerAppConfig struct {
Server *oosc.Server `json:"server" yaml:"server" toml:"server"`
Auth *oosc.Auth `json:"auth" yaml:"auth" toml:"auth"`
GRPC *oosc.GRPC `json:"gRPC" yaml:"gRPC" toml:"gRPC"`
}

8
constants.go Normal file
View File

@ -0,0 +1,8 @@
package main
const (
ConfigPathFlagName = "config-dir"
ConfigFileName = "config.json"
GRPCUserIDKey = "GRPCUserID"
)

15
external/external.go vendored Normal file
View File

@ -0,0 +1,15 @@
package external
import (
"git.loafle.net/overflow/overflow_server_app/external/grpc"
)
func ExternalInit() {
grpc.ExternalInit()
}
func ExternalDestroy() {
grpc.ExternalDestroy()
}

35
external/grpc/client.go vendored Normal file
View File

@ -0,0 +1,35 @@
package grpc
import (
"context"
"fmt"
"strings"
ooas "git.loafle.net/overflow/overflow_api_server/golang"
)
func Exec(ctx context.Context, method string, params []string) (string, error) {
if nil == grpcPool {
return "", fmt.Errorf("App: GRPC Pool is not initialized")
}
var client interface{}
var err error
if client, err = grpcPool.Get(); nil != err {
return "", err
}
defer grpcPool.Put(client)
sm := strings.Split(method, ".")
si := &ooas.ServerInput{
Target: sm[0],
Method: sm[1],
Params: params,
}
var so *ooas.ServerOutput
if so, err = client.(ooas.OverflowApiServerClient).Exec(ctx, si); nil != err {
return "", err
}
return so.Result, nil
}

50
external/grpc/pool.go vendored Normal file
View File

@ -0,0 +1,50 @@
package grpc
import (
"fmt"
"google.golang.org/grpc"
cgp "git.loafle.net/commons_go/grpc_pool"
"git.loafle.net/commons_go/logging"
ooas "git.loafle.net/overflow/overflow_api_server/golang"
"git.loafle.net/overflow/overflow_server_app/config"
)
var grpcPool cgp.Pool
func ExternalInit() {
ph := &grpcPoolHandlers{}
ph.MaxCapacity = config.Config.GRPC.Pool.MaxCapacity
ph.MaxIdle = config.Config.GRPC.Pool.MaxIdle
ph.IdleTimeout = config.Config.GRPC.Pool.IdleTimeout
ph.Wait = config.Config.GRPC.Pool.Wait
grpcPool = cgp.New(ph)
if err := grpcPool.Start(); nil != err {
logging.Logger().Panic(fmt.Sprintf("App: %v", err))
return
}
}
func ExternalDestroy() {
if nil != grpcPool {
grpcPool.Stop()
}
}
type grpcPoolHandlers struct {
cgp.PoolHandlers
}
func (h *grpcPoolHandlers) Dial() (*grpc.ClientConn, interface{}, error) {
var err error
conn, err := grpc.Dial(config.Config.GRPC.Addr, grpc.WithInsecure())
if nil != err {
return nil, nil, err
}
c := ooas.NewOverflowApiServerClient(conn)
return conn, c, nil
}

View File

@ -12,3 +12,4 @@ import:
- package: github.com/valyala/fasthttp
- package: go.uber.org/zap
- package: google.golang.org/grpc
- package: git.loafle.net/overflow/overflow_server_config

View File

@ -1,28 +0,0 @@
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
}

View File

@ -1,28 +0,0 @@
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))
}
}

View File

@ -1,30 +0,0 @@
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
}

67
main.go
View File

@ -1,79 +1,46 @@
package main
import (
"context"
"encoding/json"
"flag"
"log"
"go.uber.org/zap"
"git.loafle.net/commons_go/config"
cc "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/config"
"git.loafle.net/overflow/overflow_server_app/external"
"git.loafle.net/overflow/overflow_server_app/module/member"
"git.loafle.net/overflow/overflow_server_app/server"
"github.com/valyala/fasthttp"
)
var logger *zap.Logger
func main() {
configPath := flag.String("config", ".", "The path of config file")
func init() {
configPath := flag.String(ConfigPathFlagName, "./", "The path of config file")
flag.Parse()
loadConfig(*configPath)
ctx := newContext()
defer logger.Sync()
cc.SetConfigPath(*configPath)
if err := cc.Load(&config.Config, ConfigFileName); nil != err {
panic(err)
}
}
c := cors_fasthttp.AllowAll(ctx)
grpc.InitializePool(ctx)
func main() {
defer logging.Logger().Sync()
s := server.New(ctx)
external.ExternalInit()
s := server.New()
s.Route("POST", "/account/signin", member.SignIn)
s.Route("POST", "/account/signup", member.SignUp)
s.Route("POST", "/account/forgot_password", member.ForgotPassword)
s.Route("POST", "/account/reset_password", member.ResetPassword)
s.Route("GET", "/account/check_email", member.CheckEmail)
fasthttp.ListenAndServe(config.GetString("server.addr"), c.Handler(s.Handler))
}
c := cors_fasthttp.AllowAll()
fasthttp.ListenAndServe(config.Config.Server.Addr, c.Handler(s.Handler))
func loadConfig(path string) {
config.SetConfigName("config")
config.AddConfigPath(path)
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
external.ExternalDestroy()
}
//

View File

@ -1,11 +1,12 @@
package member
import (
"context"
"fmt"
"net/url"
"git.loafle.net/overflow/overflow_server_app/grpc"
"git.loafle.net/overflow/overflow_server_app/external/grpc"
"github.com/valyala/fasthttp"
)
@ -14,7 +15,8 @@ func CheckEmail(ctx *fasthttp.RequestCtx) {
params := []string{url.QueryEscape(key)}
r, err := grpc.Exec("EmailAuthService", "readByAuthKey", params)
gRPCCtx := context.Background()
r, err := grpc.Exec(gRPCCtx, "EmailAuthService.readByAuthKey", params)
if err != nil {
fmt.Fprintf(ctx, "Error!!!!: %s\n", err)

View File

@ -1,12 +1,13 @@
package member
import (
"context"
"fmt"
"encoding/json"
"log"
"git.loafle.net/overflow/overflow_server_app/grpc"
"git.loafle.net/overflow/overflow_server_app/external/grpc"
"github.com/valyala/fasthttp"
)
@ -37,7 +38,10 @@ func ForgotPassword(ctx *fasthttp.RequestCtx) {
params := []string{dd["signinId"].(string)}
fmt.Println(params)
r, err := grpc.Exec("MemberService", "sendEmailForPassword", params)
gRPCCtx := context.Background()
r, err := grpc.Exec(gRPCCtx, "MemberService.sendEmailForPassword", params)
ctx.SetContentType("application/javascript")
log.Printf("M:%s", r)
ctx.SetBody([]byte(r))

View File

@ -1,13 +1,14 @@
package member
import (
"context"
"encoding/json"
"fmt"
"log"
"net/url"
"strings"
"git.loafle.net/overflow/overflow_server_app/grpc"
"git.loafle.net/overflow/overflow_server_app/external/grpc"
"github.com/valyala/fasthttp"
)
@ -44,7 +45,8 @@ func ResetPassword(ctx *fasthttp.RequestCtx) {
grpcParams := []string{keys[1], pw}
r, err := grpc.Exec("MemberService", "resetPassword", grpcParams)
gRPCCtx := context.Background()
r, err := grpc.Exec(gRPCCtx, "MemberService.resetPassword", grpcParams)
ctx.SetContentType("application/javascript")
log.Printf("M:%s", r)

View File

@ -1,14 +1,15 @@
package member
import (
"context"
"fmt"
"time"
"encoding/json"
"log"
"git.loafle.net/commons_go/config"
"git.loafle.net/overflow/overflow_server_app/grpc"
"git.loafle.net/overflow/overflow_server_app/config"
"git.loafle.net/overflow/overflow_server_app/external/grpc"
jwt "github.com/dgrijalva/jwt-go"
"github.com/valyala/fasthttp"
)
@ -38,7 +39,8 @@ func SignIn(ctx *fasthttp.RequestCtx) {
params := []string{signinId, signinPw}
r, err := grpc.Exec("MemberService", "signin", params)
gRPCCtx := context.Background()
r, err := grpc.Exec(gRPCCtx, "MemberService.signin", params)
if nil != err {
fmt.Fprintf(ctx, "%v", err)
return
@ -57,7 +59,7 @@ func SignIn(ctx *fasthttp.RequestCtx) {
claims["sub"] = signinId
/* Sign the token with our secret */
tokenString, _ := token.SignedString([]byte(config.GetString("auth.signingKey")))
tokenString, _ := token.SignedString([]byte(config.Config.Auth.SigningKey))
var jwtCookie fasthttp.Cookie
jwtCookie.SetKey("AuthToken")

View File

@ -1,11 +1,12 @@
package member
import (
"context"
"fmt"
"encoding/json"
"git.loafle.net/overflow/overflow_server_app/grpc"
"git.loafle.net/overflow/overflow_server_app/external/grpc"
"github.com/valyala/fasthttp"
)
@ -47,7 +48,8 @@ func SignUp(ctx *fasthttp.RequestCtx) {
mm, _ := json.Marshal(m)
params := []string{string(mm), string(m.Pw)}
r, err := grpc.Exec("MemberService", "signup", params)
gRPCCtx := context.Background()
r, err := grpc.Exec(gRPCCtx, "MemberService.signup", params)
fmt.Fprintf(ctx, "Welcome!!!!: %s\n", r)
}

View File

@ -1,8 +1,6 @@
package server
import (
"context"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
@ -13,14 +11,11 @@ type Server interface {
}
type server struct {
ctx context.Context
router *fasthttprouter.Router
}
func New(ctx context.Context) Server {
s := &server{
ctx: ctx,
}
func New() Server {
s := &server{}
s.router = fasthttprouter.New()