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 ...
随机推荐
- 第六十篇:Vue的基本使用
好家伙,要来了,经典"hello world" 试用一下vue ① 导入 vue.js的 script 脚本文件 ② 在页面中声明一个将要被vue所控制的DOM区域 ③ 创建vm实 ...
- KingbaseES 查询计划剖析
概述:了解KingbaseES查询计划对于开发人员和数据库管理员来说都是一项关键技能.这可能是优化SQL查询的第一件事,也是验证优化的SQL查询是否确实实现期望结果的方式. 1.KingbaseES数 ...
- plpgsql 编译执行
Oracle 的存储过程或函数提供了两种执行方式: 解释执行:将源代码逐条转换成目标代码,解释一条,执行一条的过程.PLPGSQL将语句翻译成中间形式的系统代码,并在运行时进行解释. 编译执行:将源代 ...
- Linux Netlink学习笔记
参考链接:https://www.systutorials.com/docs/linux/man/7-netlink/ 1. 监听Netlink消息类型示例 Netlink是用户程序与内核通信的soc ...
- Redis变慢?深入浅出Redis性能诊断系列文章(三)
(本文首发于"数据库架构师"公号,订阅"数据库架构师"公号,一起学习数据库技术,助力职业发展) 本篇为Redis性能问题诊断系列的第三篇,主要从Redis服务层 ...
- k8s中pod的容器日志查看命令
如果容器已经崩溃停止,您可以仍然使用 kubectl logs --previous 获取该容器的日志,只不过需要添加参数 --previous. 如果 Pod 中包含多个容器,而您想要看其中某一个容 ...
- prometheus和granfana企业级监控实战v5
文件地址:https://files.cnblogs.com/files/sanduzxcvbnm/prometheus和granfana企业级监控实战v5.pdf
- PostgreSQL 创建数据库
PostgreSQL 创建数据库可以用以下三种方式: 1.使用 CREATE DATABASE SQL 语句来创建. 2.使用 createdb 命令来创建. 3.使用 pgAdmin 工具. CRE ...
- 《吐血整理》高级系列教程-吃透Fiddler抓包教程(24)-Fiddler如何优雅地在正式和测试环境之间来回切换-中篇
1.简介 在开发或者测试的过程中,由于项目环境比较多,往往需要来来回回地反复切换,那么如何优雅地切换呢?宏哥今天介绍几种方法供小伙伴或者童鞋们进行参考. 2.实际工作场景 2.1问题场景 (1)已发布 ...
- 用AR Engine手部骨骼跟踪能力实现虚拟手表试戴
AR技术的落地应用,推动着电商领域的不断升级,通过增强现实为用户带来了虚拟与现实结合的AR购物体验.如AR试衣.AR试鞋.AR试妆等功能的出现让用户在手机上就能体验产品的佩戴效果,可以让用户更直观.更 ...