This commit is contained in:
crusader
2017-09-01 15:42:23 +09:00
parent d3f40ec6be
commit 1e1e212954
15 changed files with 365 additions and 288 deletions

26
server/server.go Normal file
View File

@@ -0,0 +1,26 @@
package server
import (
"context"
"time"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
)
func NewServer(ctx context.Context) ogw.Server {
h := &serverHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
}
h.cfg = config.Sub("websocket")
h.HandshakeTimeout = h.cfg.GetDuration("HandshakeTimeout") * time.Second
h.ReadBufferSize = h.cfg.GetInt("ReadBufferSize")
h.WriteBufferSize = h.cfg.GetInt("WriteBufferSize")
h.EnableCompression = h.cfg.GetBool("EnableCompression")
s := ogw.NewServer(ctx, h)
return s
}

45
server/server_handlers.go Normal file
View File

@@ -0,0 +1,45 @@
package server
import (
"context"
"git.loafle.net/commons_go/config"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
)
type serverHandlers struct {
ogw.ServerHandlers
ctx context.Context
logger *zap.Logger
cfg config.Configurator
}
func (sh *serverHandlers) OnConnection(soc ogw.Socket) {
sh.logger.Info("connect",
zap.String("path", soc.Path()),
zap.String("id", soc.ID()),
)
}
func (sh *serverHandlers) OnDisconnected(soc ogw.Socket) {
sh.logger.Info("disconnect",
zap.String("path", soc.Path()),
zap.String("id", soc.ID()),
)
}
func (sh *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 (sh *serverHandlers) OnError(ctx *fasthttp.RequestCtx, status int, reason error) {
}