overflow_gateway_webapp/server/server_handlers.go

52 lines
1.2 KiB
Go
Raw Normal View History

2017-11-14 03:45:48 +00:00
package server
import (
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
func (sh *ServerHandlers) Init() error {
2017-11-14 10:10:28 +00:00
if err := sh.ServerHandler.Init(); nil != err {
2017-11-14 03:45:48 +00:00
return err
}
return nil
}
func (sh *ServerHandlers) OnStart() {
2017-11-14 10:10:28 +00:00
sh.ServerHandler.OnStart()
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-14 03:45:48 +00:00
func (sh *ServerHandlers) OnStop() {
2017-11-14 10:10:28 +00:00
sh.ServerHandler.OnStop()
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
}