project shared
This commit is contained in:
commit
181562a5c1
68
.gitignore
vendored
Normal file
68
.gitignore
vendored
Normal file
|
@ -0,0 +1,68 @@
|
|||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### JetBrains template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff:
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/dictionaries
|
||||
|
||||
# Sensitive or high-churn files:
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.xml
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Mongo Explorer plugin:
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
## File-based project format:
|
||||
*.iws
|
||||
|
||||
## Plugin-specific files:
|
||||
|
||||
# IntelliJ
|
||||
/out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
### Go template
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, build with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||
.glide/
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
vendor/
|
||||
glide.lock
|
||||
.DS_Store
|
||||
dist/
|
||||
debug
|
32
.vscode/launch.json
vendored
Normal file
32
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Debug",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"remotePath": "",
|
||||
"port": 2345,
|
||||
"host": "127.0.0.1",
|
||||
"program": "${workspaceRoot}/main.go",
|
||||
"env": {},
|
||||
"args": [],
|
||||
"showLog": true
|
||||
},
|
||||
{
|
||||
"name": "File Debug",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"remotePath": "",
|
||||
"port": 2345,
|
||||
"host": "127.0.0.1",
|
||||
"program": "${fileDirname}",
|
||||
"env": {},
|
||||
"args": [],
|
||||
"showLog": true
|
||||
}
|
||||
|
||||
]
|
||||
}
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
// Place your settings in this file to overwrite default and user settings.
|
||||
{
|
||||
}
|
36
Dockerfile
Normal file
36
Dockerfile
Normal file
|
@ -0,0 +1,36 @@
|
|||
FROM alpine:latest
|
||||
|
||||
ENV APP_VERSION=1.0.0 \
|
||||
APP_HOME=/opt \
|
||||
APP_NAME=overflow_gateway_probe
|
||||
|
||||
ENV APP_CONFIG_PATH="${APP_HOME}/config" \
|
||||
APP_LOGS_PATH="${APP_HOME}/logs" \
|
||||
PATH="$PATH:${APP_HOME}/bin"
|
||||
|
||||
ADD dist/${APP_NAME} ${APP_HOME}/bin/
|
||||
ADD _docker/bin/*.sh ${APP_HOME}/bin/
|
||||
|
||||
RUN apk add --no-cache curl \
|
||||
&& chmod +x ${APP_HOME}/bin/*.sh \
|
||||
&& mkdir -p ${APP_LOGS_PATH}
|
||||
|
||||
ENV TINI_VERSION='0.15.0' \
|
||||
TINI_SHA='4007655082f573603c02bc1d2137443c8e153af047ffd088d02ccc01e6f06170'
|
||||
|
||||
# Use tini as subreaper in Docker container to adopt zombie processes
|
||||
RUN curl -fsSL https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini-static-amd64 -o /bin/tini \
|
||||
&& chmod +x /bin/tini \
|
||||
&& echo "$TINI_SHA /bin/tini" | sha256sum -c -
|
||||
|
||||
VOLUME ${APP_CONFIG_PATH}
|
||||
VOLUME ${APP_LOGS_PATH}
|
||||
|
||||
EXPOSE 80 443
|
||||
|
||||
ENTRYPOINT ["/bin/tini", "--"]
|
||||
CMD ["docker-entrypoint.sh"]
|
||||
|
||||
|
||||
# docker build -f Dockerfile -t docker.loafle.net/overflow/overflow_service_websocket:1.0.0-alpine .
|
||||
# docker run -d -p 18081:80 -t docker.loafle.net/overflow/overflow_service_websocket:1.0.0-alpine
|
7
_docker/bin/docker-entrypoint.sh
Normal file
7
_docker/bin/docker-entrypoint.sh
Normal file
|
@ -0,0 +1,7 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
/opt/bin/overflow_gateway_probe -config=/opt/config/
|
||||
|
||||
exec "$@"
|
67
_docker/config/config.json
Normal file
67
_docker/config/config.json
Normal file
|
@ -0,0 +1,67 @@
|
|||
{
|
||||
"server": {
|
||||
"name": "Gateway Probe",
|
||||
"network": "tcp4",
|
||||
"addr": ":80",
|
||||
"tls": false,
|
||||
"concurrency": 262144,
|
||||
"maxStopWaitTime": 0
|
||||
},
|
||||
"auth": {
|
||||
"signingKey": "tWB0lUXiCwX4U3qsJZcZ10mKvEH793RHkTJDbDuZVshQTk4uNB6ck59UQ96lhsRi4XNUiEnlIbP8XYQMPabeNtERX3iyHeDcwocgUVAor1nkAajYeq1gNyJszGpMhEOT"
|
||||
},
|
||||
"websocket": {
|
||||
"handshakeTimeout": 0,
|
||||
"readBufferSize": 8192,
|
||||
"writeBufferSize": 8192,
|
||||
"enableCompression": false
|
||||
},
|
||||
"gRPC": {
|
||||
"addr": "192.168.1.50:50006",
|
||||
"tls": false,
|
||||
"pool": {
|
||||
"maxIdle": 1,
|
||||
"maxCapacity": 3,
|
||||
"idleTimeout": 240,
|
||||
"wait": false
|
||||
}
|
||||
},
|
||||
"redis": {
|
||||
"network": "tcp",
|
||||
"addr": "192.168.1.50:6379",
|
||||
"tls": false,
|
||||
"pool": {
|
||||
"maxIdle": 1,
|
||||
"maxCapacity": 3,
|
||||
"idleTimeout": 240,
|
||||
"wait": false
|
||||
}
|
||||
},
|
||||
"servlets": {
|
||||
"web": {
|
||||
"entry": "/web",
|
||||
"socket": {
|
||||
"maxMessageSize": 8192,
|
||||
"writeTimeout": 0,
|
||||
"readTimeout": 0,
|
||||
"pongTimeout": 60,
|
||||
"pingTimeout": 10,
|
||||
"pingPeriod": 10,
|
||||
"binaryMessage": false
|
||||
}
|
||||
},
|
||||
"file": {
|
||||
"entry": "/file",
|
||||
"socket": {
|
||||
"maxMessageSize": 8192,
|
||||
"writeTimeout": 0,
|
||||
"readTimeout": 0,
|
||||
"pongTimeout": 60,
|
||||
"pingTimeout": 10,
|
||||
"pingPeriod": 10,
|
||||
"binaryMessage": false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
27
_docker/config/logging.json
Normal file
27
_docker/config/logging.json
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"level": "debug",
|
||||
"development": true,
|
||||
"disableCaller": true,
|
||||
"disableStacktrace": true,
|
||||
"sampling": {
|
||||
"initial": 100,
|
||||
"thereafter": 100
|
||||
},
|
||||
"encoding": "console",
|
||||
"encoderConfig": {
|
||||
"messageKey": "message",
|
||||
"levelKey": "level",
|
||||
"timeKey": "time",
|
||||
"nameKey": "name",
|
||||
"callerKey": "caller",
|
||||
"stacktraceKey": "stacktrace",
|
||||
"lineEnding": "\n",
|
||||
"levelEncoder": "color",
|
||||
"timeEncoder": "ISO8601",
|
||||
"durationEncoder": "string",
|
||||
"callerEncoder": "full",
|
||||
"nameEncoder": "full"
|
||||
},
|
||||
"outputPaths": ["/opt/logs/log"],
|
||||
"errorOutputPaths": ["stderr"]
|
||||
}
|
4
build.sh
Executable file
4
build.sh
Executable file
|
@ -0,0 +1,4 @@
|
|||
rm ./dist/*
|
||||
CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o ./dist/overflow_gateway_probe
|
||||
|
||||
docker build -f Dockerfile -t docker.loafle.net/overflow/overflow_gateway_probe:1.0.0-alpine .
|
79
config.json
Normal file
79
config.json
Normal file
|
@ -0,0 +1,79 @@
|
|||
{
|
||||
"server": {
|
||||
"name": "Gateway Probe",
|
||||
"network": "tcp4",
|
||||
"addr": ":19190",
|
||||
"tls": false,
|
||||
"concurrency": 262144,
|
||||
"maxStopWaitTime": 0
|
||||
},
|
||||
"auth": {
|
||||
"signingKey": "tWB0lUXiCwX4U3qsJZcZ10mKvEH793RHkTJDbDuZVshQTk4uNB6ck59UQ96lhsRi4XNUiEnlIbP8XYQMPabeNtERX3iyHeDcwocgUVAor1nkAajYeq1gNyJszGpMhEOT"
|
||||
},
|
||||
"websocket": {
|
||||
"handshakeTimeout": 0,
|
||||
"readBufferSize": 8192,
|
||||
"writeBufferSize": 8192,
|
||||
"enableCompression": false
|
||||
},
|
||||
"gRPC": {
|
||||
"addr": "127.0.0.1:50006",
|
||||
"tls": false,
|
||||
"pool": {
|
||||
"maxIdle": 1,
|
||||
"maxCapacity": 3,
|
||||
"idleTimeout": 240,
|
||||
"wait": false
|
||||
}
|
||||
},
|
||||
"redis": {
|
||||
"network": "tcp",
|
||||
"addr": "192.168.1.50:6379",
|
||||
"tls": false,
|
||||
"pool": {
|
||||
"maxIdle": 1,
|
||||
"maxCapacity": 3,
|
||||
"idleTimeout": 240,
|
||||
"wait": false
|
||||
}
|
||||
},
|
||||
"servlets": {
|
||||
"noauth": {
|
||||
"entry": "/noauth",
|
||||
"socket": {
|
||||
"maxMessageSize": 8192,
|
||||
"writeTimeout": 0,
|
||||
"readTimeout": 0,
|
||||
"pongTimeout": 60,
|
||||
"pingTimeout": 10,
|
||||
"pingPeriod": 10,
|
||||
"binaryMessage": false
|
||||
}
|
||||
},
|
||||
"probe": {
|
||||
"entry": "/probe",
|
||||
"socket": {
|
||||
"maxMessageSize": 8192,
|
||||
"writeTimeout": 0,
|
||||
"readTimeout": 0,
|
||||
"pongTimeout": 60,
|
||||
"pingTimeout": 10,
|
||||
"pingPeriod": 10,
|
||||
"binaryMessage": false
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"entry": "/data",
|
||||
"socket": {
|
||||
"maxMessageSize": 8192,
|
||||
"writeTimeout": 0,
|
||||
"readTimeout": 0,
|
||||
"pongTimeout": 60,
|
||||
"pingTimeout": 10,
|
||||
"pingPeriod": 10,
|
||||
"binaryMessage": false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
33
docker-compose.yml
Normal file
33
docker-compose.yml
Normal file
|
@ -0,0 +1,33 @@
|
|||
version: "3"
|
||||
|
||||
services:
|
||||
overflow_gateway_probe:
|
||||
restart: always
|
||||
image: docker.loafle.net/overflow/overflow_gateway_probe:1.0.0-alpine
|
||||
container_name: overflow_gateway_probe
|
||||
volumes:
|
||||
- /service/overflow_gateway_probe/data/opt/config:/opt/config
|
||||
- /service/overflow_gateway_probe/data/opt/logs:/opt/logs
|
||||
# - /home/crusader/Temp/docker/overflow_gateway_probe/config:/opt/config
|
||||
# - /home/crusader/Temp/docker/overflow_gateway_probe/logs:/opt/logs
|
||||
ports:
|
||||
- "19090:80"
|
||||
- "19443:443"
|
||||
|
||||
# postgresql:
|
||||
# restart: always
|
||||
# image: postgres:9.6-alpine
|
||||
# container_name: overFlow-dao-postgres
|
||||
# environment:
|
||||
# - POSTGRES_DB=overflow
|
||||
# - POSTGRES_USER=overflow
|
||||
# - POSTGRES_PASSWORD=qwer5795
|
||||
# # - POSTGRES_INITDB_ARGS="--data-checksums"
|
||||
# ports:
|
||||
# - "5432:5432"
|
||||
|
||||
# docker-compose up -d
|
||||
# docker-compose stop
|
||||
# docker-compose rm
|
||||
# or
|
||||
# docker-compose -f ./docker-compose.yml up -d
|
6
glide.yaml
Normal file
6
glide.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
package: git.loafle.net/overflow/overflow_gateway_probe
|
||||
import:
|
||||
- package: git.loafle.net/commons_go/logging
|
||||
- package: git.loafle.net/overflow/overflow_gateway_websocket
|
||||
subpackages:
|
||||
- server
|
44
main.go
Normal file
44
main.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"git.loafle.net/commons_go/logging"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"log"
|
||||
"fmt"
|
||||
"git.loafle.net/overflow/overflow_gateway_probe/server"
|
||||
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
|
||||
"git.loafle.net/overflow/overflow_gateway_probe/servlet"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
defer logging.Logger().Sync()
|
||||
|
||||
sh := server.NewServerHandler()
|
||||
servlet.ServletInit(sh)
|
||||
|
||||
s := oogws.New(sh)
|
||||
|
||||
stop := make(chan os.Signal)
|
||||
signal.Notify(stop, syscall.SIGINT)
|
||||
|
||||
go func() {
|
||||
if err := s.Start(); nil != err {
|
||||
log.Printf("Server: Start error %v", err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case signal := <-stop:
|
||||
fmt.Printf("Got signal: %v\n", signal)
|
||||
}
|
||||
|
||||
servlet.ServletDestroy()
|
||||
|
||||
s.Stop()
|
||||
}
|
||||
|
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
|
||||
}
|
51
server/server_handlers.go
Normal file
51
server/server_handlers.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
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 {
|
||||
oogws.ServerHandler
|
||||
}
|
||||
|
||||
// Init invoked before the server is started
|
||||
// If you override ths method, must call
|
||||
func (sh *ServerHandlers) Init() error {
|
||||
if err := sh.ServerHandler.Init(); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (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.ServerHandler.OnStop()
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) Validate() {
|
||||
sh.ServerHandler.Validate()
|
||||
}
|
36
servlet/data/data.go
Normal file
36
servlet/data/data.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package data
|
||||
|
||||
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"
|
||||
oosc "git.loafle.net/overflow/overflow_server_config"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "data"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *oosc.Servlet
|
||||
)
|
||||
|
||||
func ServletInit(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)
|
||||
}
|
||||
|
||||
|
||||
func ServletDestroy() {
|
||||
|
||||
}
|
56
servlet/data/rpc_gateway_handlers.go
Normal file
56
servlet/data/rpc_gateway_handlers.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package data
|
||||
|
||||
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()
|
||||
}
|
34
servlet/data/subscriber_handlers.go
Normal file
34
servlet/data/subscriber_handlers.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package data
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
36
servlet/noauth/noauth.go
Normal file
36
servlet/noauth/noauth.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package noauth
|
||||
|
||||
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"
|
||||
oosc "git.loafle.net/overflow/overflow_server_config"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "noauth"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *oosc.Servlet
|
||||
)
|
||||
|
||||
func ServletInit(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)
|
||||
}
|
||||
|
||||
|
||||
func ServletDestroy() {
|
||||
|
||||
}
|
56
servlet/noauth/rpc_gateway_handlers.go
Normal file
56
servlet/noauth/rpc_gateway_handlers.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package noauth
|
||||
|
||||
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()
|
||||
}
|
34
servlet/noauth/subscriber_handlers.go
Normal file
34
servlet/noauth/subscriber_handlers.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package noauth
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
37
servlet/probe/probe.go
Normal file
37
servlet/probe/probe.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package probe
|
||||
|
||||
|
||||
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"
|
||||
oosc "git.loafle.net/overflow/overflow_server_config"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "probe"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *oosc.Servlet
|
||||
)
|
||||
|
||||
func ServletInit(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)
|
||||
}
|
||||
|
||||
|
||||
func ServletDestroy() {
|
||||
|
||||
}
|
56
servlet/probe/rpc_gateway_handlers.go
Normal file
56
servlet/probe/rpc_gateway_handlers.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package probe
|
||||
|
||||
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()
|
||||
}
|
34
servlet/probe/subscriber_handlers.go
Normal file
34
servlet/probe/subscriber_handlers.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package probe
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
21
servlet/servlet.go
Normal file
21
servlet/servlet.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package servlet
|
||||
|
||||
|
||||
import (
|
||||
oogws "git.loafle.net/overflow/overflow_gateway_websocket/server"
|
||||
"git.loafle.net/overflow/overflow_gateway_probe/servlet/data"
|
||||
"git.loafle.net/overflow/overflow_gateway_probe/servlet/noauth"
|
||||
"git.loafle.net/overflow/overflow_gateway_probe/servlet/probe"
|
||||
)
|
||||
|
||||
|
||||
func ServletInit(sh oogws.ServerHandler) {
|
||||
data.ServletInit(sh)
|
||||
noauth.ServletInit(sh)
|
||||
probe.ServletInit(sh)
|
||||
}
|
||||
|
||||
|
||||
func ServletDestroy() {
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user