deprecated_overflow_gateway.../main.go

71 lines
1.8 KiB
Go
Raw Normal View History

2017-08-24 11:51:02 +00:00
package main
import (
2017-08-25 10:41:43 +00:00
"log"
2017-08-28 09:41:41 +00:00
"google.golang.org/grpc"
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"
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-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-25 10:41:43 +00:00
s := gws.NewServer(o)
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-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
}