deprecated_overflow_gateway.../server/server_handlers.go
crusader 3f7d55695c ing
2017-09-21 20:04:19 +09:00

76 lines
1.9 KiB
Go

package server
import (
"context"
"time"
"git.loafle.net/commons_go/config"
"git.loafle.net/overflow/overflow_gateway_probe/conf"
"github.com/valyala/fasthttp"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
)
var ofSigningKey []byte
func newServerHandler(ctx context.Context) ogw.ServerHandler {
h := &serverHandlers{
ctx: ctx,
servlets: make(map[string]Servlet, 2),
}
h.HandshakeTimeout = conf.Config.Websocket.HandshakeTimeout * time.Second
h.ReadBufferSize = conf.Config.Websocket.ReadBufferSize
h.WriteBufferSize = conf.Config.Websocket.WriteBufferSize
h.EnableCompression = conf.Config.Websocket.EnableCompression
h.servlets["/auth"] = newAuthServlet()
h.servlets["/probe"] = newProbeServlet()
return h
}
type serverHandlers struct {
ogw.ServerHandlers
ctx context.Context
cfg config.Configurator
servlets map[string]Servlet
}
func (h *serverHandlers) OnConnect(ctx *fasthttp.RequestCtx) bool {
if s, ok := h.servlets[string(ctx.Path())]; ok {
return s.IsCanConnect(ctx)
}
return false
}
func (h *serverHandlers) OnConnected(soc ogw.Socket) {
if s, ok := h.servlets[soc.Path()]; ok {
if uid := s.SessionUID(soc); "" != uid {
path := soc.Path()
AddSocket(path, uid, soc)
}
}
}
func (h *serverHandlers) OnDisconnected(soc ogw.Socket) {
path := soc.Path()
RemoveSocket(path, soc)
}
func (h *serverHandlers) 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 (h *serverHandlers) OnError(ctx *fasthttp.RequestCtx, status int, reason error) {
}