add middleware for authentication and captcha, implement initial API routes for user, post, label, and page

This commit is contained in:
2025-07-18 03:26:15 +08:00
parent 70b653a88b
commit 99a3f80e12
40 changed files with 4545 additions and 21 deletions

View File

@ -0,0 +1 @@
package v1

View File

@ -0,0 +1 @@
package v1

View File

@ -0,0 +1,30 @@
package v1
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
type labelType struct{}
var Label = new(labelType)
func (l *labelType) Create(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (l *labelType) Delete(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (l *labelType) Get(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (l *labelType) Update(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (l *labelType) List(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}

View File

@ -0,0 +1,30 @@
package v1
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
type pageType struct{}
var Page = new(pageType)
func (p *pageType) Create(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (p *pageType) Delete(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (p *pageType) Get(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (p *pageType) Update(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (p *pageType) List(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}

View File

@ -0,0 +1,30 @@
package v1
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
type postType struct{}
var Post = new(postType)
func (p *postType) Create(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (p *postType) Delete(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (p *postType) Get(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (p *postType) Update(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (p *postType) List(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}

View File

@ -0,0 +1,42 @@
package v1
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
type userType struct{}
var User = new(userType)
func (u *userType) Login(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (u *userType) Register(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (u *userType) Logout(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (u *userType) OidcList(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (u *userType) OidcLogin(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (u *userType) Get(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (u *userType) Update(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}
func (u *userType) Delete(ctx context.Context, c *app.RequestContext) {
// TODO: Impl
}

View File

@ -0,0 +1,12 @@
package middleware
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
func UseAuth() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
// TODO: Implement authentication logic here
}
}

View File

@ -0,0 +1,12 @@
package middleware
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
func UseCaptcha() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
// TODO: Implement captcha validation logic here
}
}

View File

@ -0,0 +1,7 @@
package apiv1
import "github.com/cloudwego/hertz/pkg/route"
func registerCommentRoutes(group *route.RouterGroup) {
// TODO: Implement comment routes
}

View File

@ -0,0 +1,7 @@
package apiv1
import "github.com/cloudwego/hertz/pkg/route"
func registerFileRoutes(group *route.RouterGroup) {
// TODO: Impl file routes
}

View File

@ -0,0 +1,20 @@
package apiv1
import (
"github.com/cloudwego/hertz/pkg/route"
v1 "github.com/snowykami/neo-blog/internal/controller/v1"
"github.com/snowykami/neo-blog/internal/middleware"
)
func registerLabelRoutes(group *route.RouterGroup) {
labelGroup := group.Group("/label").Use(middleware.UseAuth())
labelGroupWithoutAuth := group.Group("/label")
{
labelGroupWithoutAuth.GET("/l/:id", v1.Label.Get)
labelGroupWithoutAuth.GET("/list", v1.Label.List)
labelGroup.POST("/l", v1.Label.Create)
labelGroup.DELETE("/l/:id", v1.Label.Delete)
labelGroup.PUT("/l/:id", v1.Label.Update)
}
}

View File

@ -0,0 +1,22 @@
package apiv1
import (
"github.com/cloudwego/hertz/pkg/route"
v1 "github.com/snowykami/neo-blog/internal/controller/v1"
"github.com/snowykami/neo-blog/internal/middleware"
)
// page 页面API路由
func registerPageRoutes(group *route.RouterGroup) {
postGroup := group.Group("/page").Use(middleware.UseAuth())
postGroupWithoutAuth := group.Group("/page")
{
postGroupWithoutAuth.GET("/p/:id", v1.Page.Get)
postGroupWithoutAuth.GET("/list", v1.Page.List)
postGroup.POST("/p", v1.Page.Create)
postGroup.PUT("/p", v1.Page.Update)
postGroup.DELETE("/p", v1.Page.Delete)
}
}

View File

@ -0,0 +1,22 @@
package apiv1
import (
"github.com/cloudwego/hertz/pkg/route"
v1 "github.com/snowykami/neo-blog/internal/controller/v1"
"github.com/snowykami/neo-blog/internal/middleware"
)
// post 文章API路由
func registerPostRoutes(group *route.RouterGroup) {
postGroup := group.Group("/post").Use(middleware.UseAuth())
postGroupWithoutAuth := group.Group("/post")
{
postGroupWithoutAuth.GET("/p/:id", v1.Post.Get)
postGroupWithoutAuth.GET("/list", v1.Post.List)
postGroup.POST("/p", v1.Post.Create)
postGroup.PUT("/p", v1.Post.Update)
postGroup.DELETE("/p", v1.Post.Delete)
}
}

View File

@ -0,0 +1,23 @@
package apiv1
import (
"github.com/cloudwego/hertz/pkg/route"
"github.com/snowykami/neo-blog/internal/controller/v1"
"github.com/snowykami/neo-blog/internal/middleware"
)
func registerUserRoutes(group *route.RouterGroup) {
userGroup := group.Group("/user").Use(middleware.UseAuth())
userGroupWithoutAuth := group.Group("/user")
userGroupWithoutAuthNeedsCaptcha := userGroupWithoutAuth.Use(middleware.UseCaptcha())
{
userGroupWithoutAuthNeedsCaptcha.POST("/login", v1.User.Login)
userGroupWithoutAuthNeedsCaptcha.POST("/register", v1.User.Register)
userGroupWithoutAuth.GET("/oidc/list", v1.User.OidcList)
userGroupWithoutAuth.GET("/oidc/login/:name", v1.User.OidcLogin)
userGroupWithoutAuth.GET("/u/:id", v1.User.Get)
userGroup.POST("/logout", v1.User.Logout)
userGroup.PUT("/u/:id", v1.User.Update)
userGroup.DELETE("/u/:id", v1.User.Delete)
}
}

View File

@ -0,0 +1,15 @@
package apiv1
import "github.com/cloudwego/hertz/pkg/app/server"
func RegisterRoutes(h *server.Hertz) {
apiV1Group := h.Group("/api/v1")
{
registerCommentRoutes(apiV1Group)
registerFileRoutes(apiV1Group)
registerLabelRoutes(apiV1Group)
registerPageRoutes(apiV1Group)
registerPostRoutes(apiV1Group)
registerUserRoutes(apiV1Group)
}
}

View File

@ -1,15 +1,32 @@
package router
import (
"errors"
"github.com/cloudwego/hertz/pkg/app/server"
v1 "github.com/snowykami/neo-blog/internal/router/v1"
"github.com/snowykami/neo-blog/internal/router/apiv1"
"github.com/snowykami/neo-blog/pkg/constant"
"github.com/snowykami/neo-blog/pkg/utils"
)
var h *server.Hertz
func Run() error {
mode := utils.Getenv("MODE", constant.ModeProd) // dev | prod
switch mode {
case constant.ModeProd:
h.Spin()
return nil
case constant.ModeDev:
return h.Run()
default:
return errors.New("unknown mode: " + mode)
}
}
func init() {
h := server.New(
h = server.New(
server.WithHostPorts(":"+utils.Getenv("PORT", "8888")),
server.WithMaxRequestBodySize(utils.GetenvAsInt("MAX_REQUEST_BODY_SIZE", 1048576000)), // 1000MiB
)
v1.RegisterRoutes()
apiv1.RegisterRoutes(h)
}

View File

@ -1,7 +0,0 @@
package v1
import "github.com/cloudwego/hertz/pkg/route"
func registerUserRoutes(group *route.RouterGroup) {
}

View File

@ -1,8 +0,0 @@
package v1
import "github.com/cloudwego/hertz/pkg/app/server"
func RegisterRoutes(h *server.Hertz) {
apiV1Group := h.Group("/api/v1")
registerUserRoutes(apiV1Group)
}