84 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package server
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"net"
 | |
| 
 | |
| 	"git.loafle.net/commons_go/logging"
 | |
| 	cwf "git.loafle.net/commons_go/websocket_fasthttp"
 | |
| 	"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"
 | |
| 	"github.com/valyala/fasthttp"
 | |
| )
 | |
| 
 | |
| func newServerHandler() ServerHandler {
 | |
| 	sh := &ServerHandlers{}
 | |
| 	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
 | |
| 
 | |
| 	return sh
 | |
| }
 | |
| 
 | |
| type ServerHandler interface {
 | |
| 	cwf.ServerHandler
 | |
| 
 | |
| 	RegisterSubscriberHandler(subscriberHandler oos.SubscriberHandler)
 | |
| }
 | |
| 
 | |
| type ServerHandlers struct {
 | |
| 	cwf.ServerHandlers
 | |
| 
 | |
| 	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)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (sh *ServerHandlers) Listen() (net.Listener, error) {
 | |
| 	return net.Listen(conf.Config.Server.Network, conf.Config.Server.Addr)
 | |
| }
 | |
| 
 | |
| 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
 | |
| }
 | |
| 
 | |
| func (sh *ServerHandlers) OnStop() {
 | |
| 	sh.redisSubscriber.Stop()
 | |
| }
 | |
| 
 | |
| func (sh *ServerHandlers) RegisterSubscriberHandler(subscriberHandler oos.SubscriberHandler) {
 | |
| 	if nil == sh.subscribers {
 | |
| 		sh.subscribers = make([]oos.SubscriberHandler, 0)
 | |
| 	}
 | |
| 	sh.subscribers = append(sh.subscribers, subscriberHandler)
 | |
| }
 |