当前位置:网站首页>Go语学习笔记 - 跨域配置、全局异常捕获 | Web框架Gin(四)
Go语学习笔记 - 跨域配置、全局异常捕获 | Web框架Gin(四)
2022-06-10 18:40:00 【剑客阿良_ALiang】
学习笔记,写到哪是哪。
接着上一篇的文章构建的项目:Go语学习笔记 - 项目规范结构调整 | Web框架Gin(三)_剑客阿良_ALiang的博客-CSDN博客
项目结构进一步调整为mvc结构,但还是需要加一些配置,比如跨域配置和全局异常捕获。
在项目开发中,某些非业务类的异常通常都会使用全局异常捕获的方法,简化代码量,下面我来调整一下之前的项目。
项目地址:github地址
跨域配置
在app/router文件夹下增加middlewares.go文件。
增加跨域支持方法,代码如下:
//跨域设置
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin") //请求头部
if origin != "" {
c.Header("Access-Control-Allow-Origin", "*") // 可将将 * 替换为指定的域名
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
}
//允许类型校验
if method == "OPTIONS" {
c.JSON(http.StatusOK, "ok!")
}
defer func() {
if err := recover(); err != nil {
log.Logger.Error("HttpError", zap.Any("HttpError", err))
}
}()
c.Next()
}
}全局捕获
在app/router文件夹下middlewares.go文件,增加全局捕获方法Recovery,代码如下。
//全局捕获
func Recovery(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
log.Logger.Error("gin catch error: ", log.Any("gin catch error: ", r))
c.JSON(http.StatusOK, rsp.FailMsg("系统内部错误"))
}
}()
c.Next()
}其中的rsp是返回结构体目录,在app/pojo下创建rsp目录,增加response_msg.go文件。
response_msg.go文件代码如下:
package rsp
type ResponseMsg struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func SuccessMsg(data interface{}) *ResponseMsg {
msg := &ResponseMsg{
Code: 0,
Msg: "SUCCESS",
Data: data,
}
return msg
}
func FailMsg(msg string) *ResponseMsg {
msgObj := &ResponseMsg{
Code: -1,
Msg: msg,
}
return msgObj
}
func FailCodeMsg(code int, msg string) *ResponseMsg {
msgObj := &ResponseMsg{
Code: code,
Msg: msg,
}
return msgObj
}
验证
app/router下的middlewares.go文件,完整代码如下:
package router
import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"learn-gin/app/pojo/rsp"
"learn-gin/config/log"
"net/http"
)
//跨域设置
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin") //请求头部
if origin != "" {
c.Header("Access-Control-Allow-Origin", "*") // 可将将 * 替换为指定的域名
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
}
//允许类型校验
if method == "OPTIONS" {
c.JSON(http.StatusOK, "ok!")
}
defer func() {
if err := recover(); err != nil {
log.Logger.Error("HttpError", zap.Any("HttpError", err))
}
}()
c.Next()
}
}
//全局捕获
func Recovery(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
log.Logger.Error("gin catch error: ", log.Any("gin catch error: ", r))
c.JSON(http.StatusOK, rsp.FailMsg("系统内部错误"))
}
}()
c.Next()
}
main.go文件调整,代码如下:
package main
import (
"github.com/gin-gonic/gin"
"learn-gin/app/router"
"learn-gin/config/log"
"learn-gin/config/toml"
"net/http"
"time"
)
func main() {
log.InitLogger(toml.GetConfig().Log.Path, toml.GetConfig().Log.Level)
log.Logger.Info("hahahah")
log.Logger.Info("config", log.Any("config", toml.GetConfig()))
r := gin.Default()
r.Use(router.Cors())
r.Use(router.Recovery)
router.InitRouter(r)
s := &http.Server{
Addr: ":8080",
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
err := s.ListenAndServe()
if nil != err {
log.Logger.Error("server error", log.Any("serverError", err))
}
}
注意
1、在服务启动也调整了一下,不直接run。
2、通过r.Use方法,将跨域方法和全局异常监听加入启动项。
验证一下
在需要添加参数的请求,不添加后,会返回全局统一异常报文。

看一下日志

可以看到接口未处理的异常被全局捕获,返回统一的报文。
小结
现在项目看上去结构比较完整了,虽然一些常用的工具类还没有放进来,但是基本上可以作为开发样板服务。后面我想先把一些常用的工具类放进来,然后试着开始连接数据库,做一些基本业务功能。
边栏推荐
- 叮咚抢菜-派送时段监听及推送工具
- Analysis of Muduo source code -- an analysis of the rigor, efficiency and flexibility of Muduo library code design with three slices
- 第6章 关系数据理论练习
- 【C语言】一不小心写出bug?凡人教你如何写出好代码【详解vs中调试技巧】
- Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close registration | geek headlines
- 618大促将至,用AI挖掘差评,零代码实现亿级评论观点情感分析
- 《Single Image Haze Removal Using Dark Channel Prior》去雾代码实现分析
- nodejs-基本架构分析-解析引擎目录-插件安装-核心模块
- 云图说|每个成功的业务系统都离不开APIG的保驾护航
- 【C语言进阶】指针的进阶【上篇】
猜你喜欢

Openssl1.1.1 compilation error can't locate win32/console pm in @INC

【代理】10分钟掌握正向代理和反向代理的本质区别

MySQL (17 trigger)

Before we learn about high-performance computing, let's take a look at its history

【杂谈】恭喜自己获得CSDN专家称号,努力终会换来结果
![[web] personal homepage web homework](/img/16/90b7b559e43e7cd6d5e32865eb0c00.png)
[web] personal homepage web homework "timetable", "photo album" and "message board"

Tencent cloud database tdsql- a big guy talks about the past, present and future of basic software

我的第一部作品:TensorFlow2.x

Super simple course design SSM student management system (including simple addition, deletion, modification and query of source code)

618大促将至,用AI挖掘差评,零代码实现亿级评论观点情感分析
随机推荐
Analysis of optical storage direct flexible power distribution system
【代理】10分钟掌握正向代理和反向代理的本质区别
领域驱动设计(六) - 架构设计浅谈
ESP8266 系统环境搭建
Design and implementation of SSM based traffic metering cloud system Rar (thesis + project source code)
WordPress 6.0 “Arturo阿图罗” 发布
Analysis of Muduo source code -- an analysis of the rigor, efficiency and flexibility of Muduo library code design with three slices
【C语言进阶】数据的存储【下篇】【万字总结】
【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off
北京地铁票务系统
Key and encryption mechanism in financial industry
Basic improvement - tree DP supplement
Prospect of database firewall technology [final chapter]
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
Lingo12 software download and lingo language introduction resources
Chapter 161 SQL function year
C knowledge exercise
Matlab draws ellipse code according to any angle, sampling points (resolution), position and size
【C语言进阶】指针的进阶【上篇】
MySQL数据库设计概念(多表查询&事务操作)