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 ...
随机推荐
- 【AGC】使用云调试优惠扣费、华为设备上触发崩溃、无法下载华为应用市场问题小结
1.使用云调试剩余优惠时长还剩300分钟,但还会扣费的情况. 问题描述:用户的云调试账户显示剩余优惠时长还有300分钟,但在使用云调试的过程中,还是产生了扣费的情况. 解决方案: 当开发者成功 ...
- VS Code中Markdown常用插件
目录 目录 1.Markdown All in One 2.Markdown Preview Enhanced 3.markdownlint 1.Markdown All in One 自动生成目录 ...
- KingbaseES V8R6备份恢复案例之---同一数据库创建不同stanza备份
案例说明: 在生产环境,有的应用需要调用数据库的sys_rman做备份,为了区分数据库自身的sys_rman备份和应用的备份,可以使用不同的stanza name创建备份.本案例介绍了,如何在King ...
- 使用 MAUI 在 Windows 和 Linux 上绘制 PPT 的图表
我在做一个图表工具软件,这个软件使用 MAUI 开发.我的需求是图表的内容需要和 PPT 的图表对接,需要用到 OpenXML 解析 PPT 内容,读取到 PPT 图表元素的内容,接着使用 MAUI ...
- pat乙级每日习题
欢迎加入我们:qq群:1054587486 1:https://pintia.cn/problem-sets/994805260223102976/problems/99480532591848652 ...
- WebDriver常见操作
本文当个记录贴,记录WebDriver常用的一些函数(含自己封装的函数) 让WebDriver使用浏览器用户设置 1 option = webdriver.ChromeOptions() 2 opti ...
- Chrome 浏览器缓存的问题
浏览器缓存的问题 Chrome 在浏览器中设置: 1 Ctrl + Shift + Delete 2 Ctrl + F5 强制刷新 3 F12 打开控制台 ---> Network ---> ...
- 第二章:视图层 - 5:HttpRequest对象
每当一个用户请求发送过来,Django将HTTP数据包中的相关内容,打包成为一个HttpRequest对象,并传递给每个视图函数作为第一位置参数,也就是request,供我们调用. HttpReque ...
- Service中spec.type 字段的值:ClusterIP和NodePort理解
ClusterIP(默认) 在群集中的内部IP上公布服务,这种方式的 Service(服务)只在集群内部可以访问到 [root@master ~]# kubectl get service -n te ...
- 痞子衡嵌入式:浅谈i.MXRT10xx系列MCU外接24MHz晶振的作用
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT10xx系列MCU外接24MHz晶振的作用. 痞子衡之前写过一篇关于时钟引脚的文章 <i.MXRT1xxx系列MCU时 ...