You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hzy
8424c4af83
|
1 year ago | |
---|---|---|
.. | ||
http | 1 year ago | |
README.md | 2 years ago | |
transport.pb.go | 2 years ago |
README.md
interceptor-通用grpcInterceptor
说明
http请求和响应封装
- request
- 验证前端参数签名
- response
- ResponseEncoderWithEncrypt 正式服加密后端响应参数
- bff原样返回server错误及自身业务错误,正式服其它错误统一返回InternalServerError
使用示例
http
package server
import (
userV1 "gitea.drugeyes.vip/ebm/ebm-bff/api/v1/user"
"gitea.drugeyes.vip/ebm/ebm-bff/internal/conf"
"gitea.drugeyes.vip/ebm/ebm-bff/internal/service"
trace "gitea.drugeyes.vip/ebm/ebm-bff/middleware/trace"
http1 "gitea.drugeyes.vip/pharnexbase/utils/transport/v1/http"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/middleware/validate"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/gorilla/handlers"
)
// NewHTTPServer new HTTP server.
func NewHTTPServer(
c *conf.Bootstrap,
user *service.UserService,
logger log.Logger) *http.Server {
headersOk := handlers.AllowedHeaders([]string{
"Content-Type", "X-Requested-With",
"Authorization", "User-Agent", "Accept", "Referer",
"Client-Version"})
originsOk := handlers.AllowedOrigins([]string{"*"})
methodsOk := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"})
httpTrans := http1.NewTransport(c.Env, "sm4Key", "apiKey")
var opts = []http.ServerOption{
http.Filter(handlers.CORS(headersOk, originsOk, methodsOk)),
http.RequestDecoder(httpTrans.RequestDecoder()), // body解码
http.RequestQueryDecoder(httpTrans.RequestQueryDecoder()), // query解码
http.ResponseEncoder(httpTrans.ResponseEncoder()), // 正常响应
http.ErrorEncoder(httpTrans.ErrorEncoder()), // 错误响应
http.Middleware(
recovery.Recovery(),
tracing.Server(),
logging.Server(logger),
validate.Validator(),
trace.SetTraceIdToHeader,
),
}
if c.Server.Http.Network != "" {
opts = append(opts, http.Network(c.Server.Http.Network))
}
if c.Server.Http.Addr != "" {
opts = append(opts, http.Address(c.Server.Http.Addr))
}
if c.Server.Http.Timeout != nil {
opts = append(opts, http.Timeout(c.Server.Http.Timeout.AsDuration()))
}
srv := http.NewServer(opts...)
userV1.RegisterUserCenterHTTPServer(srv, user)
return srv
}