This commit is contained in:
crusader 2017-11-14 19:10:28 +09:00
parent 0b34a62e25
commit e92a8ead89
9 changed files with 163 additions and 10 deletions

View File

@ -37,7 +37,7 @@
"wait": false
}
},
"handlers": {
"servlets": {
"web": {
"entry": "/web",
"socket": {

View File

@ -7,3 +7,5 @@ import:
- package: git.loafle.net/overflow/overflow_gateway_websocket
subpackages:
- server
- package: github.com/dgrijalva/jwt-go
version: v3.1.0

13
main.go
View File

@ -9,14 +9,14 @@ import (
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_gateway_webapp/server"
"git.loafle.net/overflow/overflow_gateway_webapp/servlet/web"
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
)
func main() {
defer logging.Logger().Sync()
sh := &server.ServerHandlers{}
s := oogws.New(sh)
s := initializeServer()
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT)
@ -36,3 +36,12 @@ func main() {
s.Stop()
}
func initializeServer() oogws.Server {
sh := server.NewServerHandler()
web.Initialize(sh)
s := oogws.New(sh)
return s
}

9
server/server_handler.go Normal file
View File

@ -0,0 +1,9 @@
package server
import (
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
)
type ServerHandler interface {
oogws.ServerHandler
}

View File

@ -1,17 +1,24 @@
package server
import (
oofws "git.loafle.net/overflow/overflow_gateway_websocket/server"
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
"github.com/valyala/fasthttp"
)
func NewServerHandler() ServerHandler {
sh := &ServerHandlers{}
sh.ServerHandler = oogws.NewServerHandler()
return sh
}
type ServerHandlers struct {
oofws.ServerHandlers
oogws.ServerHandler
}
// Init invoked before the server is started
// If you override ths method, must call
func (sh *ServerHandlers) Init() error {
if err := sh.ServerHandlers.Init(); nil != err {
if err := sh.ServerHandler.Init(); nil != err {
return err
}
@ -19,15 +26,26 @@ func (sh *ServerHandlers) Init() error {
}
func (sh *ServerHandlers) OnStart() {
sh.ServerHandlers.OnStart()
sh.ServerHandler.OnStart()
}
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) OnStop() {
sh.ServerHandlers.OnStop()
sh.ServerHandler.OnStop()
}
func (sh *ServerHandlers) Validate() {
sh.ServerHandlers.Validate()
sh.ServerHandler.Validate()
}

View File

@ -0,0 +1,56 @@
package web
import (
"fmt"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_gateway_websocket/config"
"git.loafle.net/overflow/overflow_gateway_websocket/rpc"
jwt "github.com/dgrijalva/jwt-go"
"github.com/valyala/fasthttp"
)
func newRPCGatewayHandler() rpc.RPCGatewayHandler {
gh := &RPCGatewayHandlers{}
return gh
}
type RPCGatewayHandlers struct {
rpc.RPCGatewayHandlers
}
func (sh *RPCGatewayHandlers) Init() error {
return nil
}
func (sh *RPCGatewayHandlers) Handshake(ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) {
var ok bool
// tokenString := string(soc.Conn().Headers().Cookie("AuthToken"))
tokenString := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJvdmVyRmxvdyIsImlhdCI6MTUwNDU5NTExOSwiZXhwIjoxNTM2MTMxMTE5LCJhdWQiOiJ3d3cub3ZlcmZsb3cuY2xvdWQiLCJzdWIiOiJvdmVyZmxvd0Bsb2FmbGUuY29tIn0.-WQi3OykPlJ9x8RcZGhWXEtGw4GhU6wmyJ_AWh2rMeUatQylfPzvmum2Xfp6pwKLMmcP76XoDPNyq06i7RKWNQ"
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Webapp: Unexpected signing method: %v", token.Header["alg"])
}
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
return []byte(config.Config.Auth.SigningKey), nil
})
if nil != err {
return "", nil
}
var claims jwt.MapClaims
if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid {
logging.Logger().Warn(fmt.Sprintf("Webapp: Token is not valid %v", token))
return "", nil
}
return claims["sub"].(string), nil
}
func (sh *RPCGatewayHandlers) Validate() {
sh.RPCGatewayHandlers.Validate()
}

View File

@ -1 +0,0 @@
package web

View File

@ -1,2 +1,32 @@
package web
import (
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
oogws "git.loafle.net/overflow/overflow_gateway_websocket/servlet"
oogwsub "git.loafle.net/overflow/overflow_gateway_websocket/subscribe"
oos "git.loafle.net/overflow/overflow_subscriber"
)
func newSubscriberHandler(servletHandler oogws.ServletHandler) oogwsub.SubscriberHandler {
sh := &SubscriberHandlers{}
sh.ServletHandler = servletHandler
sh.Channel = cfg.Entry
return sh
}
type SubscriberHandlers struct {
oogwsub.SubscriberHandlers
}
func (sh *SubscriberHandlers) OnSubscribe(channel string, message oos.SubscribeMessage) {
for _, uid := range message.Targets {
if soc := sh.GetSocket(uid); nil != soc {
soc.WriteMessage(websocket.TextMessage, message.Message)
}
}
}
func (sh *SubscriberHandlers) Validate() {
sh.SubscriberHandlers.Validate()
}

30
servlet/web/web.go Normal file
View File

@ -0,0 +1,30 @@
package web
import (
"fmt"
"git.loafle.net/commons_go/logging"
oogwc "git.loafle.net/overflow/overflow_gateway_websocket/config"
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
)
const (
Name = "web"
)
var (
cfg *oogwc.Servlet
)
func Initialize(sh oogws.ServerHandler) {
cfg = oogwc.Config.Servlets[Name]
if nil == cfg {
logging.Logger().Panic(fmt.Sprintf("Webapp: config of servlet[%s] is not exist", Name))
}
rpcGH := newRPCGatewayHandler()
rpcSH := sh.RegisterRPCGatewayServlet(Name, rpcGH)
sub := newSubscriberHandler(rpcSH)
sh.RegisterSubscriber(sub)
}