Skip to content

Commit

Permalink
fix: render release
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Dec 16, 2024
1 parent aae3951 commit c262183
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/http/middleware/entrance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ func Entrance(conf *koanf.Koanf, session *sessions.Manager) func(next http.Handl
sess, err := session.GetSession(r)
if err != nil {
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusInternalServerError)
render.JSON(chix.M{
"message": err.Error(),
})
return
}

entrance := conf.String("http.entrance")
if strings.TrimSuffix(r.URL.Path, "/") == strings.TrimSuffix(entrance, "/") {
sess.Put("verify_entrance", true)
render := chix.NewRender(w, r)
defer render.Release()
render.Redirect("/login")
return
}
Expand All @@ -35,6 +38,7 @@ func Entrance(conf *koanf.Koanf, session *sessions.Manager) func(next http.Handl
!cast.ToBool(sess.Get("verify_entrance", false)) &&
r.URL.Path != "/robots.txt" {
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusTeapot)
render.JSON(chix.M{
"message": "请通过正确的入口访问",
Expand Down
2 changes: 2 additions & 0 deletions internal/http/middleware/must_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func MustInstall(app biz.AppRepo) func(next http.Handler) http.Handler {
pathArr := strings.Split(r.URL.Path, "/")
if len(pathArr) < 4 {
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusForbidden)
render.JSON(chix.M{
"message": "应用不存在",
Expand All @@ -41,6 +42,7 @@ func MustInstall(app biz.AppRepo) func(next http.Handler) http.Handler {
}
if !flag && len(slugs) > 0 {
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusForbidden)
render.JSON(chix.M{
"message": fmt.Sprintf("应用 %s 未安装", slugs),
Expand Down
5 changes: 5 additions & 0 deletions internal/http/middleware/must_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ func MustLogin(session *sessions.Manager) func(next http.Handler) http.Handler {
sess, err := session.GetSession(r)
if err != nil {
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusInternalServerError)
render.JSON(chix.M{
"message": err.Error(),
})
return
}

// 对白名单和非 API 请求放行
Expand All @@ -43,6 +45,7 @@ func MustLogin(session *sessions.Manager) func(next http.Handler) http.Handler {

if sess.Missing("user_id") {
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusUnauthorized)
render.JSON(chix.M{
"message": "会话已过期,请重新登录",
Expand All @@ -53,6 +56,7 @@ func MustLogin(session *sessions.Manager) func(next http.Handler) http.Handler {
userID := cast.ToUint(sess.Get("user_id"))
if userID == 0 {
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusUnauthorized)
render.JSON(chix.M{
"message": "会话无效,请重新登录",
Expand All @@ -67,6 +71,7 @@ func MustLogin(session *sessions.Manager) func(next http.Handler) http.Handler {
clientHash := fmt.Sprintf("%x", sha256.Sum256([]byte(ip)))
if safeClientHash != clientHash || safeClientHash == "" {
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusUnauthorized)
render.JSON(chix.M{
"message": "客户端IP/UA变化,请重新登录",
Expand Down
4 changes: 4 additions & 0 deletions internal/http/middleware/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,31 @@ func Status(next http.Handler) http.Handler {
switch app.Status {
case app.StatusUpgrade:
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusServiceUnavailable)
render.JSON(chix.M{
"message": "面板更新中,请稍后刷新",
})
return
case app.StatusMaintain:
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusServiceUnavailable)
render.JSON(chix.M{
"message": "面板正在运行维护任务,请稍后刷新",
})
return
case app.StatusClosed:
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusForbidden)
render.JSON(chix.M{
"message": "面板已关闭",
})
return
case app.StatusFailed:
render := chix.NewRender(w)
defer render.Release()
render.Status(http.StatusInternalServerError)
render.JSON(chix.M{
"message": "面板运行出错,请检查排除或联系支持",
Expand Down

0 comments on commit c262183

Please sign in to comment.