120 lines
2.8 KiB
Go
120 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/garyburd/redigo/redis"
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
backGRpc "git.loafle.net/overflow/overflow_api_server/golang"
|
|
"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"
|
|
grpcPool "git.loafle.net/overflow/overflow_grpc_pool"
|
|
subscriberRedis "git.loafle.net/overflow/overflow_subscriber/redis"
|
|
)
|
|
|
|
var logger *zap.Logger
|
|
|
|
func main() {
|
|
ctx, cancel := NewContext()
|
|
defer cancel()
|
|
|
|
logger = logging.WithContext(ctx)
|
|
|
|
o := &gws.ServerOptions{
|
|
OnConnection: onConnection,
|
|
OnDisconnected: onDisconnected,
|
|
OnCheckOrigin: onCheckOrigin,
|
|
}
|
|
s := gws.NewServer(ctx, o)
|
|
|
|
rPool := &redis.Pool{
|
|
MaxIdle: 3,
|
|
IdleTimeout: 240 * time.Second,
|
|
Dial: func() (redis.Conn, error) {
|
|
return redis.Dial("tcp", "127.0.0.1:6379")
|
|
},
|
|
}
|
|
defer func() {
|
|
rPool.Close()
|
|
}()
|
|
|
|
noti := subscriberRedis.New(ctx, rPool.Get())
|
|
|
|
noti.Subscribe("web", func(channel string, payload string) {
|
|
logger.Info("Subscribe",
|
|
zap.String("channel", channel),
|
|
zap.String("payload", payload),
|
|
)
|
|
})
|
|
|
|
bo := &grpcPool.Options{
|
|
MaxIdle: 1,
|
|
MaxCapacity: 3,
|
|
|
|
Creators: func() (*grpc.ClientConn, interface{}, error) {
|
|
var err error
|
|
conn, err := grpc.Dial(":50006", grpc.WithInsecure())
|
|
if nil != err {
|
|
return nil, nil, err
|
|
}
|
|
c := backGRpc.NewOverflowApiServerClient(conn)
|
|
return conn, c, nil
|
|
},
|
|
}
|
|
bPool, err := grpcPool.New(ctx, bo)
|
|
if nil != err {
|
|
logger.Panic("Cannot create Pool of GRPC")
|
|
}
|
|
|
|
wh := web.New(ctx, bPool)
|
|
fh := file.New(ctx)
|
|
|
|
s.HandleSocket("/web", wh.GetSocketOption())
|
|
s.HandleSocket("/file", fh.GetSocketOption())
|
|
|
|
s.ListenAndServe(":19090")
|
|
}
|
|
|
|
func NewContext() (context.Context, context.CancelFunc) {
|
|
ctx := context.Background()
|
|
ctx = logging.NewContext(ctx, nil)
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
ctx = context.WithValue(ctx, "key1", "val1")
|
|
|
|
return ctx, cancel
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func onConnection(soc gws.Socket) {
|
|
logger.Info("connect",
|
|
zap.String("path", soc.Path()),
|
|
zap.String("id", soc.ID()),
|
|
)
|
|
}
|
|
|
|
func onDisconnected(soc gws.Socket) {
|
|
logger.Info("connect",
|
|
zap.String("path", soc.Path()),
|
|
zap.String("id", soc.ID()),
|
|
)
|
|
}
|