ing
This commit is contained in:
26
server/server.go
Normal file
26
server/server.go
Normal 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
45
server/server_handlers.go
Normal 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) {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user