ing
This commit is contained in:
parent
0b34a62e25
commit
e92a8ead89
|
@ -37,7 +37,7 @@
|
||||||
"wait": false
|
"wait": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"handlers": {
|
"servlets": {
|
||||||
"web": {
|
"web": {
|
||||||
"entry": "/web",
|
"entry": "/web",
|
||||||
"socket": {
|
"socket": {
|
||||||
|
|
|
@ -7,3 +7,5 @@ import:
|
||||||
- package: git.loafle.net/overflow/overflow_gateway_websocket
|
- package: git.loafle.net/overflow/overflow_gateway_websocket
|
||||||
subpackages:
|
subpackages:
|
||||||
- server
|
- server
|
||||||
|
- package: github.com/dgrijalva/jwt-go
|
||||||
|
version: v3.1.0
|
||||||
|
|
13
main.go
13
main.go
|
@ -9,14 +9,14 @@ import (
|
||||||
|
|
||||||
"git.loafle.net/commons_go/logging"
|
"git.loafle.net/commons_go/logging"
|
||||||
"git.loafle.net/overflow/overflow_gateway_webapp/server"
|
"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"
|
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
defer logging.Logger().Sync()
|
defer logging.Logger().Sync()
|
||||||
|
|
||||||
sh := &server.ServerHandlers{}
|
s := initializeServer()
|
||||||
s := oogws.New(sh)
|
|
||||||
|
|
||||||
stop := make(chan os.Signal)
|
stop := make(chan os.Signal)
|
||||||
signal.Notify(stop, syscall.SIGINT)
|
signal.Notify(stop, syscall.SIGINT)
|
||||||
|
@ -36,3 +36,12 @@ func main() {
|
||||||
s.Stop()
|
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
9
server/server_handler.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServerHandler interface {
|
||||||
|
oogws.ServerHandler
|
||||||
|
}
|
|
@ -1,17 +1,24 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
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 {
|
type ServerHandlers struct {
|
||||||
oofws.ServerHandlers
|
oogws.ServerHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init invoked before the server is started
|
// Init invoked before the server is started
|
||||||
// If you override ths method, must call
|
// If you override ths method, must call
|
||||||
func (sh *ServerHandlers) Init() error {
|
func (sh *ServerHandlers) Init() error {
|
||||||
if err := sh.ServerHandlers.Init(); nil != err {
|
if err := sh.ServerHandler.Init(); nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,15 +26,26 @@ func (sh *ServerHandlers) Init() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sh *ServerHandlers) OnStart() {
|
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() {
|
func (sh *ServerHandlers) OnStop() {
|
||||||
|
|
||||||
sh.ServerHandlers.OnStop()
|
sh.ServerHandler.OnStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sh *ServerHandlers) Validate() {
|
func (sh *ServerHandlers) Validate() {
|
||||||
sh.ServerHandlers.Validate()
|
sh.ServerHandler.Validate()
|
||||||
}
|
}
|
||||||
|
|
56
servlet/web/rpc_gateway_handlers.go
Normal file
56
servlet/web/rpc_gateway_handlers.go
Normal 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()
|
||||||
|
}
|
|
@ -1 +0,0 @@
|
||||||
package web
|
|
|
@ -1,2 +1,32 @@
|
||||||
package web
|
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
30
servlet/web/web.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user