This commit is contained in:
oneao committed 2025-11-23 22:43:57 +08:00
1 parent ac0843c041
commit a917be08d1
65 files changed
+8043

No files matched your search

@@ -0,0 +1,27 @@
// Code generated by hertz generator. DO NOT EDIT.
package auth
import (
auth "cn/oneao/base-go/biz/handler/auth"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
{
_auth := root.Group("/auth", _authMw()...)
{
_login := _auth.Group("/login", _loginMw()...)
_login.POST("/default", append(_logindefaultMw(), auth.LoginDefault)...)
}
}
}
@@ -0,0 +1,27 @@
// Code generated by hertz generator.
package auth
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _authMw() []app.HandlerFunc {
// your code...
return nil
}
func _loginMw() []app.HandlerFunc {
// your code...
return nil
}
func _logindefaultMw() []app.HandlerFunc {
// your code...
return nil
}
@@ -0,0 +1,21 @@
// Code generated by hertz generator. DO NOT EDIT.
package hello
import (
hello "cn/oneao/base-go/biz/handler/hello"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
root.GET("/hello", append(_method1Mw(), hello.Method1)...)
}
@@ -0,0 +1,17 @@
// Code generated by hertz generator.
package hello
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _method1Mw() []app.HandlerFunc {
// your code...
return nil
}
@@ -0,0 +1,19 @@
package middleware
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
// Error 中间件
func Error() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
c.Next(ctx)
if err := c.Errors.Last(); err != nil {
// 未知错误 -> 500
c.JSON(500, ErrorResponse{500, "Internal Server Error"})
}
}
}
@@ -0,0 +1,100 @@
package middleware
import (
"context"
"log"
"net/http"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/hertz-contrib/jwt"
)
var identityKey = "UserId"
type loginForm struct {
Account string `form:"username,required" json:"username,required"`
Password string `form:"password,required" json:"password,required"`
Type int `form:"type,required" json:"type,required"`
}
type User struct {
UserId int64
}
var hertzJWTMiddleware *jwt.HertzJWTMiddleware
func init() {
middleware, err := jwt.New(&jwt.HertzJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: 30 * time.Minute,
MaxRefresh: 30 * 24 * time.Hour,
IdentityKey: identityKey,
// Token负载信息
PayloadFunc: func(data interface{}) jwt.MapClaims {
if v, ok := data.(*User); ok {
return jwt.MapClaims{
identityKey: v.UserId,
}
}
return jwt.MapClaims{}
},
// 解析Token取出用户信息
IdentityHandler: func(ctx context.Context, c *app.RequestContext) interface{} {
claims := jwt.ExtractClaims(ctx, c)
return &User{
UserId: claims[identityKey].(int64),
}
},
// 登录
Authenticator: func(ctx context.Context, c *app.RequestContext) (interface{}, error) {
c.Request.RequestURI()
return nil, jwt.ErrFailedAuthentication
},
// 权限控制
Authorizator: func(data interface{}, ctx context.Context, c *app.RequestContext) bool {
return true
},
// 未验证处理
Unauthorized: func(ctx context.Context, c *app.RequestContext, code int, message string) {
c.JSON(code, map[string]interface{}{
"code": code,
"message": message,
})
},
// 登录响应
LoginResponse: func(ctx context.Context, c *app.RequestContext, code int, token string, expire time.Time) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
})
},
// 登出响应
LogoutResponse: func(ctx context.Context, c *app.RequestContext, code int) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
})
},
// 刷新Token
RefreshResponse: func(ctx context.Context, c *app.RequestContext, code int, token string, expire time.Time) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
})
},
TokenLookup: "header: Authorization",
})
if err != nil {
log.Fatal("JWT Error:" + err.Error())
}
hertzJWTMiddleware = middleware
}
func JWT() *jwt.HertzJWTMiddleware {
return hertzJWTMiddleware
}
@@ -0,0 +1,17 @@
// Code generated by hertz generator. DO NOT EDIT.
package router
import (
auth "cn/oneao/base-go/biz/router/auth"
hello "cn/oneao/base-go/biz/router/hello"
"github.com/cloudwego/hertz/pkg/app/server"
)
// GeneratedRegister registers routers generated by IDL.
func GeneratedRegister(r *server.Hertz) {
//INSERT_POINT: DO NOT DELETE THIS LINE!
hello.Register(r)
auth.Register(r)
}