修改完善nacos熔断机制
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Response 通用响应结构
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// 常见的响应代码
|
||||
const (
|
||||
SuccessCode = 200
|
||||
ErrorCode = 500
|
||||
BadRequestCode = 400
|
||||
NotFoundCode = 404
|
||||
UnauthorizedCode = 401
|
||||
)
|
||||
|
||||
// Success 生成成功响应
|
||||
func Success(c *app.RequestContext, data interface{}) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: SuccessCode,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Error 生成错误响应
|
||||
func Error(c *app.RequestContext, code int, message string) {
|
||||
c.JSON(code, Response{
|
||||
Code: code,
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
// BadRequest 生成400响应
|
||||
func BadRequest(c *app.RequestContext, message string) {
|
||||
Error(c, BadRequestCode, message)
|
||||
}
|
||||
|
||||
// NotFound 生成404响应
|
||||
func NotFound(c *app.RequestContext, message string) {
|
||||
Error(c, NotFoundCode, message)
|
||||
}
|
||||
|
||||
// Unauthorized 生成401响应
|
||||
func Unauthorized(c *app.RequestContext, message string) {
|
||||
Error(c, UnauthorizedCode, message)
|
||||
}
|
||||
|
||||
// FileResponse 生成文件响应
|
||||
func FileResponse(c *app.RequestContext, filePath, fileName string) {
|
||||
// 设置响应头,告诉浏览器这是一个下载文件的请求
|
||||
c.Response.Header.Set("Content-Disposition", "attachment; filename="+fileName)
|
||||
c.Response.Header.Set("Content-Type", "application/octet-stream")
|
||||
|
||||
// 使用 File() 直接发送文件
|
||||
c.File(filePath)
|
||||
}
|
||||
Reference in New Issue
Block a user