overflow_gateway_webapp/server/server_handlers.go

53 lines
1.4 KiB
Go
Raw Permalink Normal View History

2017-11-14 03:45:48 +00:00
package server
import (
2017-11-28 11:08:46 +00:00
cwf "git.loafle.net/commons_go/websocket_fasthttp"
2017-11-14 10:10:28 +00:00
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
"github.com/valyala/fasthttp"
2017-11-14 03:45:48 +00:00
)
2017-11-14 10:10:28 +00:00
func NewServerHandler() ServerHandler {
sh := &ServerHandlers{}
sh.ServerHandler = oogws.NewServerHandler()
return sh
}
2017-11-14 03:45:48 +00:00
type ServerHandlers struct {
2017-11-14 10:10:28 +00:00
oogws.ServerHandler
2017-11-14 03:45:48 +00:00
}
// Init invoked before the server is started
// If you override ths method, must call
2017-11-28 11:08:46 +00:00
func (sh *ServerHandlers) Init(serverCTX cwf.ServerContext) error {
if err := sh.ServerHandler.Init(serverCTX); nil != err {
2017-11-14 03:45:48 +00:00
return err
}
return nil
}
2017-11-28 11:08:46 +00:00
func (sh *ServerHandlers) OnStart(serverCTX cwf.ServerContext) {
sh.ServerHandler.OnStart(serverCTX)
2017-11-14 03:45:48 +00:00
}
2017-11-14 10:10:28 +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
}
2017-11-28 11:08:46 +00:00
func (sh *ServerHandlers) OnStop(serverCTX cwf.ServerContext) {
2017-11-14 03:45:48 +00:00
2017-11-28 11:08:46 +00:00
sh.ServerHandler.OnStop(serverCTX)
2017-11-14 03:45:48 +00:00
}
func (sh *ServerHandlers) Validate() {
2017-11-14 10:10:28 +00:00
sh.ServerHandler.Validate()
2017-11-14 03:45:48 +00:00
}