2017-08-24 11:51:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-08-29 08:58:37 +00:00
|
|
|
"context"
|
2017-08-25 10:41:43 +00:00
|
|
|
"log"
|
2017-08-29 08:58:37 +00:00
|
|
|
"time"
|
2017-08-25 10:41:43 +00:00
|
|
|
|
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-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"
|
2017-08-29 08:58:37 +00:00
|
|
|
notiRedis "git.loafle.net/overflow/overflow_gateway_web/notification/redis"
|
2017-08-25 10:41:43 +00:00
|
|
|
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-24 11:51:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-08-29 08:58:37 +00:00
|
|
|
ctx, cancel := NewContext()
|
|
|
|
defer cancel()
|
|
|
|
|
2017-08-25 10:41:43 +00:00
|
|
|
o := &gws.ServerOptions{
|
2017-08-24 11:51:02 +00:00
|
|
|
OnConnection: onConnection,
|
|
|
|
OnDisconnected: onDisconnected,
|
2017-08-25 10:41:43 +00:00
|
|
|
OnCheckOrigin: onCheckOrigin,
|
2017-08-24 11:51:02 +00:00
|
|
|
}
|
2017-08-29 08:58:37 +00:00
|
|
|
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 rPool.Close()
|
|
|
|
|
|
|
|
noti := notiRedis.New(ctx, rPool.Get())
|
|
|
|
|
|
|
|
noti.Subscribe("web", func(channel string, payload string) {
|
|
|
|
log.Printf("c:%s, p:%s", channel, payload)
|
|
|
|
})
|
2017-08-24 11:51:02 +00:00
|
|
|
|
2017-08-28 09:41:41 +00:00
|
|
|
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(bo)
|
|
|
|
if nil != err {
|
|
|
|
log.Panic("Cannot create Pool of GRPC")
|
|
|
|
}
|
|
|
|
|
|
|
|
wh := web.New(bPool)
|
2017-08-25 10:41:43 +00:00
|
|
|
fh := file.New()
|
2017-08-24 11:51:02 +00:00
|
|
|
|
2017-08-25 10:41:43 +00:00
|
|
|
s.HandleSocket("/web", wh.GetSocketOption())
|
|
|
|
s.HandleSocket("/file", fh.GetSocketOption())
|
2017-08-24 11:51:02 +00:00
|
|
|
|
|
|
|
s.ListenAndServe(":19090")
|
|
|
|
}
|
|
|
|
|
2017-08-29 08:58:37 +00:00
|
|
|
func NewContext() (context.Context, context.CancelFunc) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
ctx = context.WithValue(ctx, "key1", "val1")
|
|
|
|
|
|
|
|
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) {
|
|
|
|
log.Printf("connect: path: %s, id:%s\n", soc.Path(), soc.ID())
|
2017-08-24 11:51:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 10:41:43 +00:00
|
|
|
func onDisconnected(soc gws.Socket) {
|
|
|
|
log.Printf("disconnect: path: %s, id:%s\n", soc.Path(), soc.ID())
|
2017-08-24 11:51:02 +00:00
|
|
|
}
|