deprecated_overflow_gateway.../main.go

132 lines
3.5 KiB
Go
Raw Normal View History

2017-08-24 11:51:02 +00:00
package main
import (
2017-08-29 08:58:37 +00:00
"context"
2017-08-30 10:26:32 +00:00
"log"
2017-08-29 08:58:37 +00:00
"time"
2017-08-25 10:41:43 +00:00
2017-08-30 06:50:56 +00:00
"go.uber.org/zap"
2017-08-28 09:41:41 +00:00
"google.golang.org/grpc"
2017-08-29 08:58:37 +00:00
"github.com/garyburd/redigo/redis"
2017-08-25 10:41:43 +00:00
"github.com/valyala/fasthttp"
2017-08-30 10:26:32 +00:00
"git.loafle.net/commons_go/config"
2017-08-30 06:50:56 +00:00
"git.loafle.net/commons_go/logging"
2017-08-28 09:41:41 +00:00
backGRpc "git.loafle.net/overflow/overflow_api_server/golang"
2017-08-25 10:41:43 +00:00
"git.loafle.net/overflow/overflow_gateway_web/handler/file"
"git.loafle.net/overflow/overflow_gateway_web/handler/web"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
2017-08-28 09:41:41 +00:00
grpcPool "git.loafle.net/overflow/overflow_grpc_pool"
2017-08-30 06:50:56 +00:00
subscriberRedis "git.loafle.net/overflow/overflow_subscriber/redis"
2017-08-24 11:51:02 +00:00
)
2017-08-30 06:50:56 +00:00
var logger *zap.Logger
2017-08-24 11:51:02 +00:00
func main() {
2017-08-30 10:26:32 +00:00
config.SetConfigName("config")
config.AddConfigPath(".")
err := config.ReadInConfig()
if nil != err {
log.Fatalf("config error: %v", err)
}
2017-08-29 08:58:37 +00:00
ctx, cancel := NewContext()
defer cancel()
2017-08-30 06:50:56 +00:00
logger = logging.WithContext(ctx)
2017-08-25 10:41:43 +00:00
o := &gws.ServerOptions{
2017-08-30 10:26:32 +00:00
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"),
2017-08-24 11:51:02 +00:00
}
2017-08-29 08:58:37 +00:00
s := gws.NewServer(ctx, o)
rPool := &redis.Pool{
2017-08-30 10:26:32 +00:00
MaxIdle: config.GetInt("redis.pool.MaxIdle"),
IdleTimeout: time.Duration(config.GetInt("redis.pool.IdleTimeout")) * time.Second,
2017-08-29 08:58:37 +00:00
Dial: func() (redis.Conn, error) {
2017-08-30 10:26:32 +00:00
return redis.Dial(config.GetString("redis.network"), config.GetString("redis.addr"))
2017-08-29 08:58:37 +00:00
},
}
2017-08-30 06:50:56 +00:00
defer func() {
rPool.Close()
}()
2017-08-29 08:58:37 +00:00
2017-08-30 06:50:56 +00:00
noti := subscriberRedis.New(ctx, rPool.Get())
2017-08-29 08:58:37 +00:00
noti.Subscribe("web", func(channel string, payload string) {
2017-08-30 06:50:56 +00:00
logger.Info("Subscribe",
zap.String("channel", channel),
zap.String("payload", payload),
)
2017-08-29 08:58:37 +00:00
})
2017-08-24 11:51:02 +00:00
2017-08-28 09:41:41 +00:00
bo := &grpcPool.Options{
2017-08-30 10:26:32 +00:00
MaxIdle: config.GetInt("grpc.pool.MaxIdle"),
MaxCapacity: config.GetInt("grpc.pool.MaxCapacity"),
2017-08-28 09:41:41 +00:00
Creators: func() (*grpc.ClientConn, interface{}, error) {
var err error
2017-08-30 10:26:32 +00:00
conn, err := grpc.Dial(config.GetString("grpc.addr"), grpc.WithInsecure())
2017-08-28 09:41:41 +00:00
if nil != err {
return nil, nil, err
}
c := backGRpc.NewOverflowApiServerClient(conn)
return conn, c, nil
},
}
2017-08-30 06:50:56 +00:00
bPool, err := grpcPool.New(ctx, bo)
2017-08-28 09:41:41 +00:00
if nil != err {
2017-08-30 06:50:56 +00:00
logger.Panic("Cannot create Pool of GRPC")
2017-08-28 09:41:41 +00:00
}
2017-08-30 06:50:56 +00:00
wh := web.New(ctx, bPool)
fh := file.New(ctx)
2017-08-24 11:51:02 +00:00
2017-08-30 10:26:32 +00:00
s.HandleSocket(config.GetString("handlers.web.entry"), wh.GetSocketOption())
s.HandleSocket(config.GetString("handlers.file.entry"), fh.GetSocketOption())
2017-08-24 11:51:02 +00:00
2017-08-30 10:26:32 +00:00
s.ListenAndServe(config.GetString("server.addr"))
2017-08-24 11:51:02 +00:00
}
2017-08-29 08:58:37 +00:00
func NewContext() (context.Context, context.CancelFunc) {
ctx := context.Background()
2017-08-30 06:50:56 +00:00
ctx = logging.NewContext(ctx, nil)
2017-08-29 08:58:37 +00:00
ctx, cancel := context.WithCancel(ctx)
return ctx, cancel
}
2017-08-25 10:41:43 +00:00
func onCheckOrigin(ctx *fasthttp.RequestCtx) bool {
if origin := string(ctx.Request.Header.Peek("Origin")); origin != "" {
ctx.Response.Header.Set("Access-Control-Allow-Origin", origin)
if string(ctx.Method()) == "OPTIONS" && string(ctx.Request.Header.Peek("Access-Control-Request-Method")) != "" {
ctx.Response.Header.Set("Access-Control-Allow-Headers", "Content-Type, Accept")
ctx.Response.Header.Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE")
}
}
return true
2017-08-24 11:51:02 +00:00
}
2017-08-25 10:41:43 +00:00
func onConnection(soc gws.Socket) {
2017-08-30 06:50:56 +00:00
logger.Info("connect",
zap.String("path", soc.Path()),
zap.String("id", soc.ID()),
)
2017-08-24 11:51:02 +00:00
}
2017-08-25 10:41:43 +00:00
func onDisconnected(soc gws.Socket) {
2017-08-30 10:26:32 +00:00
logger.Info("disconnect",
2017-08-30 06:50:56 +00:00
zap.String("path", soc.Path()),
zap.String("id", soc.ID()),
)
2017-08-24 11:51:02 +00:00
}