1 Star 0 Fork 0

yzsunjianguo / common_pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
common.go 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
yzsunjianguo 提交于 2024-02-08 11:08 . fix
// Package handlerfunc is used for public http request handler.
package handlerfunc
import (
"embed"
"net/http"
"os"
"strings"
"gitee.com/yzsunjianguo/common_pkg/errcode"
"gitee.com/yzsunjianguo/common_pkg/utils"
"github.com/gin-gonic/gin"
)
// checkHealthResponse check health result
type checkHealthResponse struct {
Status string `json:"status"`
Hostname string `json:"hostname"`
}
// CheckHealth check healthy.
// @Summary check health
// @Description check health
// @Tags system
// @Accept json
// @Produce json
// @Success 200 {object} checkHealthResponse{}
// @Router /health [get]
func CheckHealth(c *gin.Context) {
c.JSON(http.StatusOK, checkHealthResponse{Status: "UP", Hostname: utils.GetHostname()})
}
// Ping ping
// @Summary ping
// @Description ping
// @Tags system
// @Accept json
// @Produce json
// @Router /ping [get]
func Ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{})
}
// ListCodes list error codes info
// @Summary list error codes info
// @Description list error codes info
// @Tags system
// @Accept json
// @Produce json
// @Router /codes [get]
func ListCodes(c *gin.Context) {
c.JSON(http.StatusOK, errcode.ListHTTPErrCodes())
}
// BrowserRefresh solve vue using history route 404 problem, for system file
func BrowserRefresh(path string) func(c *gin.Context) {
return func(c *gin.Context) {
accept := c.Request.Header.Get("Accept")
flag := strings.Contains(accept, "text/html")
if flag {
content, err := os.ReadFile(path)
if err != nil {
c.Writer.WriteHeader(404)
_, _ = c.Writer.WriteString("Not Found")
return
}
c.Writer.WriteHeader(200)
c.Writer.Header().Add("Accept", "text/html")
_, _ = c.Writer.Write(content)
c.Writer.Flush()
}
}
}
// BrowserRefreshFS solve vue using history route 404 problem, for embed.FS
func BrowserRefreshFS(fs embed.FS, path string) func(c *gin.Context) {
return func(c *gin.Context) {
accept := c.Request.Header.Get("Accept")
flag := strings.Contains(accept, "text/html")
if flag {
content, err := fs.ReadFile(path)
if err != nil {
c.Writer.WriteHeader(404)
_, _ = c.Writer.WriteString("Not Found")
return
}
c.Writer.WriteHeader(200)
c.Writer.Header().Add("Accept", "text/html")
_, _ = c.Writer.Write(content)
c.Writer.Flush()
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yzsunjianguo/common_pkg.git
git@gitee.com:yzsunjianguo/common_pkg.git
yzsunjianguo
common_pkg
common_pkg
v1.0.1

搜索帮助