You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package routers
import (
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
// "go.uber.org/zap"
"goskeleton/app/global/consts"
"goskeleton/app/global/variable"
"goskeleton/app/http/controller/captcha"
"goskeleton/app/http/middleware/authorization"
"goskeleton/app/http/middleware/cors"
validatorFactory "goskeleton/app/http/validator/core/factory"
// "goskeleton/app/utils/gin_release"
// "net/http"
)
func InitWebRouter_Co() *gin.Engine {
var router *gin.Engine = gin.Default()
// 日志显示在控制台
pprof.Register(router)
//根据配置进行设置跨域
if variable.ConfigYml.GetBool("HttpServer.AllowCrossDomain") {
router.Use(cors.Next())
}
// 创建一个验证码路由
verifyCode := router.Group("captcha")
{
// 验证码业务,该业务无需专门校验参数,所以可以直接调用控制器
verifyCode.GET("/", (&captcha.Captcha{}).GenerateId) // 获取验证码ID
verifyCode.GET("/:captcha_id", (&captcha.Captcha{}).GetImg) // 获取图像地址
verifyCode.GET("/:captcha_id/:captcha_value", (&captcha.Captcha{}).CheckCode) // 校验验证码
}
// 创建一个后端接口路由组
backend := router.Group("/admin/")
{
// 【不需要token】中间件验证的路由 用户注册、登录
noAuth := backend.Group("users/")
{
// 关于路由的第二个参数用法说明
// 1.编写一个表单参数验证器结构体,参见代码: app/http/validator/web/users/register.go
// 2.将以上表单参数验证器注册,遵守 键 =》值 格式注册即可 app/http/validator/common/register_validator/web_register_validator.go 20行就是注册时候的键 consts.ValidatorPrefix+"UsersRegister"
// 3.按照注册时的键,直接从容器调用即可 validatorFactory.Create(consts.ValidatorPrefix+"UsersRegister")
noAuth.POST("register", validatorFactory.Create(consts.ValidatorPrefix+"UsersRegister")) // ok
// 不需要验证码即可登陆
noAuth.POST("login", validatorFactory.Create(consts.ValidatorPrefix+"UsersLogin")) // ok
// 如果加载了验证码中间件那么就需要提交验证码才可以登陆本质上就是给登陆接口增加了2个参数验证码id提交时的键captcha_id 和 验证码值提交时的键 captcha_value具体参见配置文件
//noAuth.Use(authorization.CheckCaptchaAuth()).POST("login", validatorFactory.Create(consts.ValidatorPrefix+"UsersLogin"))
// 获取公钥,用于密码等敏感信息的加密
noAuth.POST("publickey", validatorFactory.Create(consts.ValidatorPrefix+"PublicKey"))
}
// 刷新token
refreshToken := backend.Group("users/")
{
// 刷新token当过期的token在允许失效的延长时间范围内用旧token换取新token
refreshToken.Use(authorization.RefreshTokenConditionCheck()).POST("refreshtoken", validatorFactory.Create(consts.ValidatorPrefix+"RefreshToken"))
}
// 【需要token】中间件验证的路由
backend.Use(authorization.CheckTokenAuth())
{
// 用户组路由
users := backend.Group("users/")
{
// 查询 这里的验证器直接从容器获取是因为程序启动时将验证器注册在了容器具体代码位置App\Http\Validator\Web\Users\xxx
// 展示用户详细信息-用户名、ID
users.GET("info", validatorFactory.Create(consts.ValidatorPrefix+"UsersInfo"))
// 新增
// users.POST("create", validatorFactory.Create(consts.ValidatorPrefix+"UsersStore")) // not need
// 更新用户名或密码
users.POST("username", validatorFactory.Create(consts.ValidatorPrefix+"UsersNameUpdate"))
users.POST("password", validatorFactory.Create(consts.ValidatorPrefix+"UsersPasswordUpdate"))
// 注销用户
users.POST("delete", validatorFactory.Create(consts.ValidatorPrefix+"UsersDestroy"))
// 退出登录
users.POST("logout", validatorFactory.Create(consts.ValidatorPrefix+"UsersLogout"))
}
}
}
return router
}