This commit is contained in:
crusader 2017-08-30 19:26:32 +09:00
parent 5daa3f6f4e
commit db25683a3a
5 changed files with 115 additions and 16 deletions

68
config.json Normal file
View File

@ -0,0 +1,68 @@
{
"server": {
"addr": ":19090",
"tls": false
},
"websocket": {
"HandshakeTimeout": 0,
"ReadBufferSize": 8192,
"WriteBufferSize": 8192,
"EnableCompression": false
},
"grpc": {
"addr": "127.0.0.1:50006",
"tls": false,
"pool": {
"MaxIdle": 1,
"MaxCapacity": 3,
"increaseCapacity": 10
}
},
"redis": {
"network": "tcp",
"addr": "127.0.0.1:6379",
"tls": false,
"pool": {
"MaxIdle": 3,
"IdleTimeout": 240,
"increaseCapacity": 10
}
},
"logging": {
"addr": "127.0.0.1:6379",
"tls": false,
"pool": {
"initialCapacity": 30,
"maxCapacity": 100,
"increaseCapacity": 10
}
},
"handlers": {
"web": {
"entry": "/web",
"socket": {
"MaxMessageSize": 8192,
"WriteTimeout": 0,
"ReadTimeout": 0,
"PongTimeout": 60,
"PingTimeout": 10,
"PingPeriod": 10,
"BinaryMessage": false
}
},
"file": {
"entry": "/file",
"socket": {
"MaxMessageSize": 8192,
"WriteTimeout": 0,
"ReadTimeout": 0,
"PongTimeout": 60,
"PingTimeout": 10,
"PingPeriod": 10,
"BinaryMessage": false
}
}
}
}

View File

@ -19,3 +19,4 @@ import:
subpackages:
- metadata
- package: git.loafle.net/overflow/overflow_gateway_websocket
- package: git.loafle.net/commons_go/config

View File

@ -3,9 +3,11 @@ package file
import (
"context"
"log"
"time"
"go.uber.org/zap"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_gateway_web/handler"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
@ -38,7 +40,14 @@ func New(ctx context.Context) FileHandler {
h.handler = jsonrpc.NewHandler(ctx, h.ho)
h.co = &gws.SocketOptions{
Handler: h.handler,
Handler: h.handler,
MaxMessageSize: config.GetInt64("handlers.file.socket.MaxMessageSize"),
WriteTimeout: time.Duration(config.GetInt("handlers.file.socket.WriteTimeout")) * time.Second,
ReadTimeout: time.Duration(config.GetInt("handlers.file.socket.ReadTimeout")) * time.Second,
PongTimeout: time.Duration(config.GetInt("handlers.file.socket.PongTimeout")) * time.Second,
PingTimeout: time.Duration(config.GetInt("handlers.file.socket.PingTimeout")) * time.Second,
PingPeriod: time.Duration(config.GetInt("handlers.file.socket.PingPeriod")) * time.Second,
BinaryMessage: config.GetBool("handlers.file.socket.BinaryMessage"),
}
return h

View File

@ -3,11 +3,13 @@ package web
import (
"context"
"strings"
"time"
"go.uber.org/zap"
"google.golang.org/grpc/metadata"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
backGRpc "git.loafle.net/overflow/overflow_api_server/golang"
"git.loafle.net/overflow/overflow_gateway_web/handler"
@ -44,7 +46,14 @@ func New(ctx context.Context, pool grpcPool.Pool) WebHandler {
h.handler = jsonrpc.NewHandler(ctx, h.ro)
h.so = &gws.SocketOptions{
Handler: h.handler,
Handler: h.handler,
MaxMessageSize: config.GetInt64("handlers.web.socket.MaxMessageSize"),
WriteTimeout: time.Duration(config.GetInt("handlers.web.socket.WriteTimeout")) * time.Second,
ReadTimeout: time.Duration(config.GetInt("handlers.web.socket.ReadTimeout")) * time.Second,
PongTimeout: time.Duration(config.GetInt("handlers.web.socket.PongTimeout")) * time.Second,
PingTimeout: time.Duration(config.GetInt("handlers.web.socket.PingTimeout")) * time.Second,
PingPeriod: time.Duration(config.GetInt("handlers.web.socket.PingPeriod")) * time.Second,
BinaryMessage: config.GetBool("handlers.web.socket.BinaryMessage"),
}
return h

40
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"context"
"log"
"time"
"go.uber.org/zap"
@ -11,6 +12,7 @@ import (
"github.com/garyburd/redigo/redis"
"github.com/valyala/fasthttp"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
backGRpc "git.loafle.net/overflow/overflow_api_server/golang"
"git.loafle.net/overflow/overflow_gateway_web/handler/file"
@ -23,23 +25,34 @@ import (
var logger *zap.Logger
func main() {
config.SetConfigName("config")
config.AddConfigPath(".")
err := config.ReadInConfig()
if nil != err {
log.Fatalf("config error: %v", err)
}
ctx, cancel := NewContext()
defer cancel()
logger = logging.WithContext(ctx)
o := &gws.ServerOptions{
OnConnection: onConnection,
OnDisconnected: onDisconnected,
OnCheckOrigin: onCheckOrigin,
OnConnection: onConnection,
OnDisconnected: onDisconnected,
OnCheckOrigin: onCheckOrigin,
HandshakeTimeout: time.Duration(config.GetInt("websocket.HandshakeTimeout")) * time.Second,
ReadBufferSize: config.GetInt("websocket.ReadBufferSize"),
WriteBufferSize: config.GetInt("websocket.WriteBufferSize"),
EnableCompression: config.GetBool("websocket.EnableCompression"),
}
s := gws.NewServer(ctx, o)
rPool := &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
MaxIdle: config.GetInt("redis.pool.MaxIdle"),
IdleTimeout: time.Duration(config.GetInt("redis.pool.IdleTimeout")) * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "127.0.0.1:6379")
return redis.Dial(config.GetString("redis.network"), config.GetString("redis.addr"))
},
}
defer func() {
@ -56,12 +69,12 @@ func main() {
})
bo := &grpcPool.Options{
MaxIdle: 1,
MaxCapacity: 3,
MaxIdle: config.GetInt("grpc.pool.MaxIdle"),
MaxCapacity: config.GetInt("grpc.pool.MaxCapacity"),
Creators: func() (*grpc.ClientConn, interface{}, error) {
var err error
conn, err := grpc.Dial(":50006", grpc.WithInsecure())
conn, err := grpc.Dial(config.GetString("grpc.addr"), grpc.WithInsecure())
if nil != err {
return nil, nil, err
}
@ -77,10 +90,10 @@ func main() {
wh := web.New(ctx, bPool)
fh := file.New(ctx)
s.HandleSocket("/web", wh.GetSocketOption())
s.HandleSocket("/file", fh.GetSocketOption())
s.HandleSocket(config.GetString("handlers.web.entry"), wh.GetSocketOption())
s.HandleSocket(config.GetString("handlers.file.entry"), fh.GetSocketOption())
s.ListenAndServe(":19090")
s.ListenAndServe(config.GetString("server.addr"))
}
func NewContext() (context.Context, context.CancelFunc) {
@ -88,7 +101,6 @@ func NewContext() (context.Context, context.CancelFunc) {
ctx = logging.NewContext(ctx, nil)
ctx, cancel := context.WithCancel(ctx)
ctx = context.WithValue(ctx, "key1", "val1")
return ctx, cancel
}
@ -112,7 +124,7 @@ func onConnection(soc gws.Socket) {
}
func onDisconnected(soc gws.Socket) {
logger.Info("connect",
logger.Info("disconnect",
zap.String("path", soc.Path()),
zap.String("id", soc.ID()),
)