kratos V2 框架设计自定义返回结构以及跨域

在Web开发业务中,通常使用业务状态码来判断业务状态,不使用http状态码。在kratos V2中控制 http 的返回值,在 http 返回值外面返回自己熟悉的结构

在NewHTTPServer中进行使用(替换返回结构)

// 过滤,所包含的路由不会经过jwt权限验证
func NewSkipRoutersMatcher() selector.MatchFunc {
	//完整 todo /包名.服务名/方法名
	smn := "/intelligent_analysis.v1.Analysis" //包名.服务名
	skipRouters := make(map[string]struct{})
	skipRouters[smn+"/Login"] = struct{}{} //通过operation匹配规则,并不是http本身的路由  /包名.服务名/方法名
	skipRouters[smn+"/Platform"] = struct{}{}
	return func(ctx context.Context, operation string) bool {
		if _, ok := skipRouters[operation]; ok {
			return false
		}
		return true
	}
}


/=================================================================
// NewHTTPServer new a HTTP server.
func NewHTTPServer(c *conf.Server, jwtc *conf.JWT, cloud_platform *service.AnalysisService, logger log.Logger) *http.Server {
	var opts = []http.ServerOption{
		//自定义返回错误结构
		http.ErrorEncoder(errorEncoder), //替代默认的错误结构
     
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值