mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-03 15:56:22 +00:00
⚡ add middleware for authentication and captcha, implement initial API routes for user, post, label, and page
This commit is contained in:
1
internal/controller/v1/comment.go
Normal file
1
internal/controller/v1/comment.go
Normal file
@ -0,0 +1 @@
|
||||
package v1
|
1
internal/controller/v1/file.go
Normal file
1
internal/controller/v1/file.go
Normal file
@ -0,0 +1 @@
|
||||
package v1
|
30
internal/controller/v1/label.go
Normal file
30
internal/controller/v1/label.go
Normal 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
|
||||
}
|
30
internal/controller/v1/page.go
Normal file
30
internal/controller/v1/page.go
Normal 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
|
||||
}
|
30
internal/controller/v1/post.go
Normal file
30
internal/controller/v1/post.go
Normal 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
|
||||
}
|
42
internal/controller/v1/user.go
Normal file
42
internal/controller/v1/user.go
Normal 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
|
||||
}
|
12
internal/middleware/auth.go
Normal file
12
internal/middleware/auth.go
Normal 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
|
||||
}
|
||||
}
|
12
internal/middleware/captcha.go
Normal file
12
internal/middleware/captcha.go
Normal 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
|
||||
}
|
||||
}
|
7
internal/router/apiv1/comment.go
Normal file
7
internal/router/apiv1/comment.go
Normal file
@ -0,0 +1,7 @@
|
||||
package apiv1
|
||||
|
||||
import "github.com/cloudwego/hertz/pkg/route"
|
||||
|
||||
func registerCommentRoutes(group *route.RouterGroup) {
|
||||
// TODO: Implement comment routes
|
||||
}
|
7
internal/router/apiv1/file.go
Normal file
7
internal/router/apiv1/file.go
Normal file
@ -0,0 +1,7 @@
|
||||
package apiv1
|
||||
|
||||
import "github.com/cloudwego/hertz/pkg/route"
|
||||
|
||||
func registerFileRoutes(group *route.RouterGroup) {
|
||||
// TODO: Impl file routes
|
||||
}
|
20
internal/router/apiv1/label.go
Normal file
20
internal/router/apiv1/label.go
Normal 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)
|
||||
}
|
||||
}
|
22
internal/router/apiv1/page.go
Normal file
22
internal/router/apiv1/page.go
Normal 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)
|
||||
}
|
||||
}
|
22
internal/router/apiv1/post.go
Normal file
22
internal/router/apiv1/post.go
Normal 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)
|
||||
}
|
||||
}
|
23
internal/router/apiv1/user.go
Normal file
23
internal/router/apiv1/user.go
Normal 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)
|
||||
}
|
||||
}
|
15
internal/router/apiv1/v1.go
Normal file
15
internal/router/apiv1/v1.go
Normal 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)
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package v1
|
||||
|
||||
import "github.com/cloudwego/hertz/pkg/route"
|
||||
|
||||
func registerUserRoutes(group *route.RouterGroup) {
|
||||
|
||||
}
|
@ -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)
|
||||
}
|
Reference in New Issue
Block a user