mindxdl--common--head_handler.go
// Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.
// Package common the common package of the program
package common
import (
"errors"
"github.com/gin-gonic/gin"
"golang.org/x/net/context"
"huawei.com/npu-exporter/hwlog"
"net/http"
"strconv"
)
const (
base = 10
bitSize = 64
// HeaderUserIDKey HeaderUserIDKey
HeaderUserIDKey = "UserID"
// HeaderGroupIDKey HeaderGroupIDKey
HeaderGroupIDKey = "GroupID"
// HeaderRoleIDKey HeaderRoleIDKey
HeaderRoleIDKey = "RoleID"
// HeaderRequestIDKey HeaderRequestIDKey
HeaderRequestIDKey = "RequestID"
// HeaderRoleCodeKey HeaderRoleCodeKey
HeaderRoleCodeKey = "RoleCode"
)
// RespMsg response msg
type RespMsg struct {
Status string `json:"status"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
}
// HeaderInfo header struct
type HeaderInfo struct {
TraceID string `sql:"-" json:"requestID,omitempty"`
UserID uint64 `gorm:"" json:"userId,omitempty"`
GroupID uint64 `gorm:"" json:"groupId,omitempty"`
RoleID uint64 `sql:"-" json:"roleId,omitempty"`
RoleCode string `sql:"-" json:"roleCode,omitempty"`
}
// ConstructResp construct response
func ConstructResp(c *gin.Context, errorCode string, msg string, data interface{}) {
ConstructRespWithStatus(c, errorCode, msg, data, http.StatusOK)
}
// ConstructRespWithStatus construct response
func ConstructRespWithStatus(c *gin.Context, errorCode string, msg string, data interface{}, status int) {
if msg == "" {
msg = ErrorMap[errorCode]
}
result := RespMsg{
Status: errorCode,
Msg: msg,
Data: data,
}
c.JSON(status, result)
}
// GetHeaderInfo get header info
func GetHeaderInfo(c *gin.Context) (HeaderInfo, string, error) {
var hInfo HeaderInfo
uuid := c.GetHeader(HeaderRequestIDKey)
hInfo.TraceID = uuid
userIDStr := c.GetHeader(HeaderUserIDKey)
if userIDStr == "" {
return HeaderInfo{}, ErrorNotFoundUserID, errors.New("no login")
}
userID, err := strconv.ParseUint(userIDStr, base, bitSize)
if err != nil {
return HeaderInfo{}, ErrorConvertStr2Uint, errors.New("convert userID failed")
}
hInfo.UserID = userID
groupIDStr := c.GetHeader(HeaderGroupIDKey)
if groupIDStr == "" {
return hInfo, ErrorNotFoundGroupID, errors.New("not found groupID")
}
groupID, err := strconv.ParseUint(groupIDStr, base, bitSize)
if err != nil {
return HeaderInfo{}, ErrorConvertStr2Uint, errors.New("convert groupID failed")
}
hInfo.GroupID = groupID
roleIDStr := c.GetHeader(HeaderRoleIDKey)
if roleIDStr == "" {
return hInfo, ErrorNotFoundRoleID, errors.New("not found roleID")
}
roleID, err := strconv.ParseUint(roleIDStr, base, bitSize)
if err != nil {
return HeaderInfo{}, ErrorConvertStr2Uint, errors.New("convert groupID failed")
}
hInfo.RoleID = roleID
code := c.GetHeader(HeaderRoleCodeKey)
hInfo.RoleCode = code
return hInfo, "", nil
}
// BaseCtx the base of all controller
type BaseCtx struct {
Ctx context.Context
HdInfo HeaderInfo
}
// WrapHeader check and wrap the header parameter
func WrapHeader() gin.HandlerFunc {
return func(c *gin.Context) {
hInfo, errorCode, err := GetHeaderInfo(c)
if err != nil {
hwlog.OpLog.Error(err)
c.Abort()
ConstructRespWithStatus(c, errorCode, err.Error(), nil, http.StatusUnauthorized)
return
}
ctx := context.WithValue(context.TODO(), hwlog.ReqID, hInfo.TraceID)
ctx = context.WithValue(ctx, hwlog.UserID, hInfo.UserID)
baseCtx := BaseCtx{
Ctx: ctx,
HdInfo: hInfo,
}
c.Set("Ctx", baseCtx)
}
}
// Convert extract the baseCtx from gin.context and use as a parameter
func Convert(f func(b BaseCtx, c *gin.Context)) gin.HandlerFunc {
return func(c *gin.Context) {
baseCtx, ok := c.Get("Ctx")
if !ok {
ConstructResp(c, ErrNoLoginInfo, "", nil)
}
b, err := baseCtx.(BaseCtx)
if !err {
ConstructResp(c, ErrTypeConvert, "", nil)
}
f(b, c)
}
}
mindxdl--common--head_handler.go的更多相关文章
- Socket聊天程序——Common
写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...
- angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)
common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...
- Common Bugs in C Programming
There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
[题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下: C++ Code 123456 struct BinaryTreeNode { int ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
随机推荐
- [SDR] GNU Radio 系列教程(一) —— 什么是 GNU Radio
目录 1.GNU Radio 是什么 2.我为什么要用 GNU Radio 3.数字信号处理 3.1 一点信号理论 3.2 将数字信号处理应用于无线电传输 4.基于流程图的模块化数字信号处理方法 本文 ...
- Logstash:运用 fingerprint 过滤器处理重复的文档
文章转载自:https://blog.csdn.net/UbuntuTouch/article/details/106639848 背景:Elasticsearch 索引 在介绍重复数据删除解决方案之 ...
- 四、frp内网穿透服务端frps.ini各配置参数详解
[必须]标识头[common]是不可或缺的部分 [必须]服务器IPbind_addr = 0.0.0.00.0.0.0为服务器全局所有IP可用,假如你的服务器有多个IP则可以这样做,或者填写为指定其中 ...
- ProxySQL(1):简介和安装
文章转载自:https://www.cnblogs.com/f-ck-need-u/p/9278818.html ProxySQL有两个版本:官方版和percona版,percona版是在官方版的基础 ...
- kubernetes(k8s)命令大全
状态查询 # 查看集群信息 # kubectl cluster-info Kubernetes control plane is running at https://127.0.0.1:8443 K ...
- Exporter介绍
Exporter是什么 广义上讲所有可以向Prometheus提供监控样本数据的程序都可以被称为一个Exporter.而Exporter的一个实例称为target,如下所示,Prometheus通过轮 ...
- 影响 erp 系统实施成功的因素是什么?
影响ERP系统实施成功的因素很多,主要有以下几点:企业一把手是否大力支持.实施顾问是否专业负责.ERP系统是否强大灵活且适用三个方面!没有企业一把手的大力支持,ERP的应用基本上不可能获得成功.ERP ...
- NSIS Studio2.1汉化版
这个东西早些时候是我从老外那里下载回来后放在了群共享里面,中间跟着作者的节奏更新了几次,后来和LmTec聊这个软件的时候提出了汉化的设想,可能是LmTec兄弟看这个软件确实不错,就一口答应了下来,结果 ...
- Wine 安装迅雷5.8.14.176
测试过的系统版本:Kubuntu 22.04 测试过的Wine版本 Wine7.8 程序下载地址: https://pan.baidu.com/s/1pSgunVH3WtACssX5we3DdQ 提取 ...
- C#-2 C#程序
一 C#程序是一组类型声明 C#程序或DLL的源代码是一组一种或多种类型声明. 对于可执行程序,类型声明中必须有一个包含Main方法的类. 命名空间是一种把相关的类型声明分组并命名的方法.是类在程序集 ...