38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
"easygoadmin/internal/consts"
|
|
"easygoadmin/utility/response"
|
|
)
|
|
|
|
// ============================== 演示模式中间件 ==============================
|
|
|
|
// Demo 演示模式拦截
|
|
//
|
|
// 当配置 app.demo 为 true 时启用,规则如下:
|
|
// - 读操作(GET/HEAD/OPTIONS)放行,演示站可正常浏览
|
|
// - 写操作(POST/PUT/PATCH/DELETE)一律拒绝并提示"演示环境,禁止操作!"
|
|
// - 公开白名单路径(登录/注册/验证码/令牌兑换)放行,保证演示站可登录
|
|
func Demo(r *ghttp.Request) {
|
|
if !g.Cfg().MustGet(r.Context(), "app.demo").Bool() {
|
|
r.Middleware.Next()
|
|
return
|
|
}
|
|
switch r.Method {
|
|
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
|
r.Middleware.Next()
|
|
return
|
|
}
|
|
if IsPublicPath(r.URL.Path) {
|
|
r.Middleware.Next()
|
|
return
|
|
}
|
|
response.Write(r, response.Fail(gerror.NewCode(consts.CodeDemoMode)))
|
|
}
|