deprecated_overflow_gateway.../server/server_handlers.go

84 lines
2.4 KiB
Go
Raw Permalink Normal View History

2017-11-09 09:14:57 +00:00
package server
import (
2017-11-09 11:09:18 +00:00
"fmt"
2017-11-09 09:14:57 +00:00
"net"
2017-11-09 11:09:18 +00:00
"git.loafle.net/commons_go/logging"
2017-11-09 09:14:57 +00:00
cwf "git.loafle.net/commons_go/websocket_fasthttp"
2017-11-09 11:09:18 +00:00
"git.loafle.net/overflow/overflow_gateway_app/conf"
"git.loafle.net/overflow/overflow_gateway_app/external/redis"
oos "git.loafle.net/overflow/overflow_subscriber"
oosr "git.loafle.net/overflow/overflow_subscriber/redis"
2017-11-09 09:14:57 +00:00
"github.com/valyala/fasthttp"
)
2017-11-09 11:09:18 +00:00
func newServerHandler() ServerHandler {
2017-11-09 09:14:57 +00:00
sh := &ServerHandlers{}
2017-11-09 11:09:18 +00:00
sh.Name = conf.Config.Server.Name
sh.Concurrency = conf.Config.Server.Concurrency
sh.MaxStopWaitTime = conf.Config.Server.MaxStopWaitTime
sh.HandshakeTimeout = conf.Config.Websocket.HandshakeTimeout
sh.ReadBufferSize = conf.Config.Websocket.ReadBufferSize
sh.WriteBufferSize = conf.Config.Websocket.WriteBufferSize
sh.EnableCompression = conf.Config.Websocket.EnableCompression
2017-11-09 09:14:57 +00:00
return sh
}
2017-11-09 11:09:18 +00:00
type ServerHandler interface {
cwf.ServerHandler
RegisterSubscriberHandler(subscriberHandler oos.SubscriberHandler)
}
2017-11-09 09:14:57 +00:00
type ServerHandlers struct {
cwf.ServerHandlers
2017-11-09 11:09:18 +00:00
redisSubscriber oos.Subscriber
subscribers []oos.SubscriberHandler
}
func (sh *ServerHandlers) OnStart() {
sh.redisSubscriber = oosr.New(redis.RedisPool.Get())
if err := sh.redisSubscriber.Start(); nil != err {
logging.Logger().Error(fmt.Sprintf("App: Redist Subscriber did not start %v", err))
return
}
for _, subscriber := range sh.subscribers {
sh.redisSubscriber.Subscribe(subscriber)
}
2017-11-09 09:14:57 +00:00
}
func (sh *ServerHandlers) Listen() (net.Listener, error) {
2017-11-09 11:09:18 +00:00
return net.Listen(conf.Config.Server.Network, conf.Config.Server.Addr)
2017-11-09 09:14:57 +00:00
}
func (sh *ServerHandlers) CheckOrigin(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) {
// no op
}
2017-11-09 11:09:18 +00:00
func (sh *ServerHandlers) OnStop() {
sh.redisSubscriber.Stop()
2017-11-09 09:14:57 +00:00
}
2017-11-09 11:09:18 +00:00
func (sh *ServerHandlers) RegisterSubscriberHandler(subscriberHandler oos.SubscriberHandler) {
if nil == sh.subscribers {
sh.subscribers = make([]oos.SubscriberHandler, 0)
}
sh.subscribers = append(sh.subscribers, subscriberHandler)
2017-11-09 09:14:57 +00:00
}