Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
fix config
Browse files Browse the repository at this point in the history
  • Loading branch information
November-12 committed Nov 3, 2023
1 parent 72f9660 commit 3e4f1d0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
5 changes: 5 additions & 0 deletions conf/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ api:

grpc:
tptodb_server: 127.0.0.1:50052

openapi:
httpport: 9990
sign : false
timestamp: 5 # X-OpenAPI-Timestamp 过期时间(分钟)
12 changes: 5 additions & 7 deletions middleware/openapi_middle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
adapter "github.com/beego/beego/v2/adapter"
"github.com/beego/beego/v2/adapter/context"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
context2 "github.com/beego/beego/v2/server/web/context"
"github.com/spf13/viper"
"github.com/thinkeridea/go-extend/exnet"
)

Expand All @@ -33,12 +33,14 @@ var dataServicesApiList []string = []string{
// openapi 访问过滤
func openapiFilter(ctx *context.Context) {
fmt.Println("请求接口:", ctx.Request.URL.Path)

signFlag := viper.GetBool("openapi.sign")
// 判断 请求头中是否携带X-OpenAPI-Timestamp
if timestamp := ctx.Request.Header.Get("X-OpenAPI-Timestamp"); timestamp == "" {
utils.SuccessWithMessage(401, "时间戳不存在", (*context2.Context)(ctx))
return
} else {
signFlag, _ := web.AppConfig.Bool("openapi.sign")

if signFlag {
//校验X-OpenAPI-Timestamp时间
curTimestamp := time.Now().Unix()
Expand All @@ -47,7 +49,7 @@ func openapiFilter(ctx *context.Context) {
logs.Error("时间戳格式错误", err.Error())
}
// 过期时间
expirTime, _ := web.AppConfig.Int64("openapi.timestamp")
expirTime := viper.GetInt64("openapi.timestamp")
if (curTimestamp - reqTimestamp) > (expirTime * 60) {
utils.SuccessWithMessage(401, "访问权限超时", (*context2.Context)(ctx))
return
Expand Down Expand Up @@ -90,8 +92,6 @@ func openapiFilter(ctx *context.Context) {
return
}
}
// 获取配置文件中的openapi.sign
signFlag, _ := web.AppConfig.Bool("openapi.sign")
if signFlag {
//验签X-OpenAPI-Signature
Signature := ctx.Request.Header.Get("X-OpenAPI-Signature")
Expand Down Expand Up @@ -119,8 +119,6 @@ func openapiFilter(ctx *context.Context) {
return
}
}
// 获取配置文件中的openapi.sign
signFlag, _ := web.AppConfig.Bool("openapi.sign")
if signFlag {
//验签X-OpenAPI-Signature
Signature := ctx.Request.Header.Get("X-OpenAPI-Signature")
Expand Down
9 changes: 5 additions & 4 deletions openapi/openapi_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package services
import (
"bytes"
"fmt"
"github.com/beego/beego/logs"
"github.com/beego/beego/v2/server/web"
"io"
"io/ioutil"
"net/http"

"github.com/beego/beego/logs"
"github.com/spf13/viper"
)

func init() {
go func() {

mux := http.NewServeMux()
mux.HandleFunc("/", handler)
port, _ := web.AppConfig.String("openapi.httpport")
port := viper.GetString("openapi.httpport")
err := http.ListenAndServe(":"+port, mux)
if err != nil {
logs.Error("OpenApi服务启动失败", err.Error())
Expand All @@ -32,7 +33,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
return
}
req.Body = ioutil.NopCloser(bytes.NewReader(body))
apiPort, _ := web.AppConfig.String("httpport")
apiPort := viper.GetString("openapi.httpport")
url := fmt.Sprintf("%s://%s%s", "http", "127.0.0.1:"+apiPort, req.RequestURI)

proxyReq, err := http.NewRequest(req.Method, url, bytes.NewReader(body))
Expand Down
9 changes: 5 additions & 4 deletions services/openapi_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package services
import (
"bytes"
"fmt"
"github.com/beego/beego/logs"
"github.com/beego/beego/v2/server/web"
"io"
"io/ioutil"
"net/http"

"github.com/beego/beego/logs"
"github.com/spf13/viper"
)

func init() {
go func() {

mux := http.NewServeMux()
mux.HandleFunc("/", handler)
port, _ := web.AppConfig.String("openapi.httpport")
port := viper.GetString("openapi.httpport")
err := http.ListenAndServe(":"+port, mux)
if err != nil {
logs.Error("OpenApi服务启动失败", err.Error())
Expand All @@ -32,7 +33,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
return
}
req.Body = ioutil.NopCloser(bytes.NewReader(body))
apiPort, _ := web.AppConfig.String("httpport")
apiPort := viper.GetString("openapi.httpport")
url := fmt.Sprintf("%s://%s%s", "http", "127.0.0.1:"+apiPort, req.RequestURI)

proxyReq, err := http.NewRequest(req.Method, url, bytes.NewReader(body))
Expand Down
1 change: 0 additions & 1 deletion services/tskv_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ func (*TSKVService) GatewayMsgProc(body []byte, topic string, messages chan map[
logs.Error(err.Error())
return false
} else {

// 写入协程数
// writeWorkers, _ := web.AppConfig.Int("write_workers")
// for i := 0; i < writeWorkers; i++ {
Expand Down

0 comments on commit 3e4f1d0

Please sign in to comment.