deprecated_overflow_gateway.../main.go

48 lines
1.3 KiB
Go
Raw Normal View History

2017-08-24 11:51:02 +00:00
package main
import (
2017-08-25 10:41:43 +00:00
"log"
"github.com/valyala/fasthttp"
"git.loafle.net/overflow/overflow_gateway_web/handler/file"
"git.loafle.net/overflow/overflow_gateway_web/handler/web"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
2017-08-24 11:51:02 +00:00
)
func main() {
2017-08-25 10:41:43 +00:00
o := &gws.ServerOptions{
2017-08-24 11:51:02 +00:00
OnConnection: onConnection,
OnDisconnected: onDisconnected,
2017-08-25 10:41:43 +00:00
OnCheckOrigin: onCheckOrigin,
2017-08-24 11:51:02 +00:00
}
2017-08-25 10:41:43 +00:00
s := gws.NewServer(o)
2017-08-24 11:51:02 +00:00
2017-08-25 10:41:43 +00:00
wh := web.New()
fh := file.New()
2017-08-24 11:51:02 +00:00
2017-08-25 10:41:43 +00:00
s.HandleSocket("/web", wh.GetSocketOption())
s.HandleSocket("/file", fh.GetSocketOption())
2017-08-24 11:51:02 +00:00
s.ListenAndServe(":19090")
}
2017-08-25 10:41:43 +00:00
func 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
2017-08-24 11:51:02 +00:00
}
2017-08-25 10:41:43 +00:00
func onConnection(soc gws.Socket) {
log.Printf("connect: path: %s, id:%s\n", soc.Path(), soc.ID())
2017-08-24 11:51:02 +00:00
}
2017-08-25 10:41:43 +00:00
func onDisconnected(soc gws.Socket) {
log.Printf("disconnect: path: %s, id:%s\n", soc.Path(), soc.ID())
2017-08-24 11:51:02 +00:00
}