init
This commit is contained in:
207
backend/app/http/api/controller.go
Normal file
207
backend/app/http/api/controller.go
Normal file
@@ -0,0 +1,207 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/tenancy"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type ApiController struct{}
|
||||
|
||||
type PostsQuery struct {
|
||||
Page int `query:"page"`
|
||||
Limit int `query:"limit"`
|
||||
Keyword string `query:"keyword"`
|
||||
}
|
||||
|
||||
type UpdateUsernameReq struct {
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
// WeChatOAuthStart
|
||||
//
|
||||
// @Router /v1/auth/wechat [get]
|
||||
func (a *ApiController) WeChatOAuthStart(ctx fiber.Ctx) error {
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "wechat oauth: /auth/wechat"})
|
||||
}
|
||||
|
||||
// WeChatOAuthCallback
|
||||
//
|
||||
// @Router /v1/auth/login [get]
|
||||
func (a *ApiController) WeChatOAuthCallback(ctx fiber.Ctx) error {
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "wechat oauth: /auth/login"})
|
||||
}
|
||||
|
||||
// ListPosts
|
||||
//
|
||||
// @Router /v1/posts [get]
|
||||
// @Bind query query
|
||||
func (a *ApiController) ListPosts(ctx fiber.Ctx, query *PostsQuery) error {
|
||||
page := 1
|
||||
limit := 10
|
||||
if query != nil {
|
||||
if query.Page > 0 {
|
||||
page = query.Page
|
||||
}
|
||||
if query.Limit > 0 {
|
||||
limit = query.Limit
|
||||
}
|
||||
}
|
||||
return ctx.JSON(fiber.Map{
|
||||
"items": []any{},
|
||||
"total": 0,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
})
|
||||
}
|
||||
|
||||
// ShowPost
|
||||
//
|
||||
// @Router /v1/posts/:id/show [get]
|
||||
// @Bind id path
|
||||
func (a *ApiController) ShowPost(ctx fiber.Ctx, id int) error {
|
||||
if id <= 0 {
|
||||
return errorx.ErrInvalidParameter.WithMsg("invalid id")
|
||||
}
|
||||
return fiber.ErrNotFound
|
||||
}
|
||||
|
||||
// PlayPost
|
||||
//
|
||||
// @Router /v1/posts/:id/play [get]
|
||||
// @Bind id path
|
||||
func (a *ApiController) PlayPost(ctx fiber.Ctx, id int) error {
|
||||
if id <= 0 {
|
||||
return errorx.ErrInvalidParameter.WithMsg("invalid id")
|
||||
}
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "post play"})
|
||||
}
|
||||
|
||||
// MinePosts
|
||||
//
|
||||
// @Router /v1/posts/mine [get]
|
||||
// @Bind query query
|
||||
func (a *ApiController) MinePosts(ctx fiber.Ctx, query *PostsQuery) error {
|
||||
page := 1
|
||||
limit := 10
|
||||
if query != nil {
|
||||
if query.Page > 0 {
|
||||
page = query.Page
|
||||
}
|
||||
if query.Limit > 0 {
|
||||
limit = query.Limit
|
||||
}
|
||||
}
|
||||
return ctx.JSON(fiber.Map{
|
||||
"items": []any{},
|
||||
"total": 0,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
})
|
||||
}
|
||||
|
||||
// BuyPost
|
||||
//
|
||||
// @Router /v1/posts/:id/buy [post]
|
||||
// @Bind id path
|
||||
func (a *ApiController) BuyPost(ctx fiber.Ctx, id int) error {
|
||||
if id <= 0 {
|
||||
return errorx.ErrInvalidParameter.WithMsg("invalid id")
|
||||
}
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "post buy"})
|
||||
}
|
||||
|
||||
// UserProfile
|
||||
//
|
||||
// @Router /v1/users/profile [get]
|
||||
func (a *ApiController) UserProfile(ctx fiber.Ctx) error {
|
||||
tenantCode := ctx.Locals(tenancy.LocalTenantCode)
|
||||
return ctx.JSON(fiber.Map{
|
||||
"id": 0,
|
||||
"created_at": "1970-01-01T00:00:00Z",
|
||||
"username": "",
|
||||
"avatar": "",
|
||||
"balance": 0,
|
||||
"tenant": tenantCode,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateUsername
|
||||
//
|
||||
// @Router /v1/users/username [put]
|
||||
// @Bind req body
|
||||
func (a *ApiController) UpdateUsername(ctx fiber.Ctx, req *UpdateUsernameReq) error {
|
||||
if req == nil {
|
||||
return errorx.ErrInvalidJSON
|
||||
}
|
||||
name := strings.TrimSpace(req.Username)
|
||||
if name == "" {
|
||||
return errorx.ErrDataValidationFail.WithMsg("username required")
|
||||
}
|
||||
if len([]rune(name)) > 12 {
|
||||
return errorx.ErrDataValidationFail.WithMsg("username too long")
|
||||
}
|
||||
return ctx.JSON(fiber.Map{"ok": true})
|
||||
}
|
||||
|
||||
// WechatJSSDK
|
||||
//
|
||||
// @Router /v1/wechats/js-sdk [get]
|
||||
func (a *ApiController) WechatJSSDK(ctx fiber.Ctx) error {
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "wechat js-sdk"})
|
||||
}
|
||||
|
||||
// AdminAuth (stub)
|
||||
//
|
||||
// @Router /v1/admin/auth [post]
|
||||
func (a *ApiController) AdminAuth(ctx fiber.Ctx) error {
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "admin auth"})
|
||||
}
|
||||
|
||||
// AdminStatistics (stub)
|
||||
//
|
||||
// @Router /v1/admin/statistics [get]
|
||||
func (a *ApiController) AdminStatistics(ctx fiber.Ctx) error {
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "admin statistics"})
|
||||
}
|
||||
|
||||
// AdminOrders (stub)
|
||||
//
|
||||
// @Router /v1/admin/orders [get]
|
||||
func (a *ApiController) AdminOrders(ctx fiber.Ctx) error {
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "admin orders list"})
|
||||
}
|
||||
|
||||
// AdminOrderRefund (stub)
|
||||
//
|
||||
// @Router /v1/admin/orders/:id/refund [post]
|
||||
// @Bind id path
|
||||
func (a *ApiController) AdminOrderRefund(ctx fiber.Ctx, id int) error {
|
||||
if id <= 0 {
|
||||
return errorx.ErrInvalidParameter.WithMsg("invalid id")
|
||||
}
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "admin orders refund"})
|
||||
}
|
||||
|
||||
// AdminMedias (stub)
|
||||
//
|
||||
// @Router /v1/admin/medias [get]
|
||||
func (a *ApiController) AdminMedias(ctx fiber.Ctx) error {
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "admin medias list"})
|
||||
}
|
||||
|
||||
// AdminMediaShow (stub)
|
||||
//
|
||||
// @Router /v1/admin/medias/:id [get]
|
||||
func (a *ApiController) AdminMediaShow(ctx fiber.Ctx) error {
|
||||
_, err := strconv.ParseInt(ctx.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
return errorx.ErrInvalidParameter.WithMsg("invalid id")
|
||||
}
|
||||
return ctx.Status(fiber.StatusNotImplemented).JSON(fiber.Map{"ok": false, "todo": "admin medias show"})
|
||||
}
|
||||
33
backend/app/http/api/provider.gen.go
Executable file
33
backend/app/http/api/provider.gen.go
Executable file
@@ -0,0 +1,33 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func() (*ApiController, error) {
|
||||
obj := &ApiController{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
apiController *ApiController,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
apiController: apiController,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
114
backend/app/http/api/routes.gen.go
Normal file
114
backend/app/http/api/routes.gen.go
Normal file
@@ -0,0 +1,114 @@
|
||||
// Code generated by atomctl. DO NOT EDIT.
|
||||
|
||||
// Package api provides HTTP route definitions and registration
|
||||
// for the quyun/v2 application.
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
_ "go.ipao.vip/atom/contracts"
|
||||
. "go.ipao.vip/atom/fen"
|
||||
)
|
||||
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
// for all controllers in the api module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
// Controller instances
|
||||
apiController *ApiController
|
||||
}
|
||||
|
||||
// Prepare initializes the routes provider with logging configuration.
|
||||
func (r *Routes) Prepare() error {
|
||||
r.log = log.WithField("module", "routes.api")
|
||||
r.log.Info("Initializing routes module")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name returns the unique identifier for this routes provider.
|
||||
func (r *Routes) Name() string {
|
||||
return "api"
|
||||
}
|
||||
|
||||
// Register registers all HTTP routes with the provided fiber router.
|
||||
// Each route is registered with its corresponding controller action and parameter bindings.
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// Register routes for controller: ApiController
|
||||
r.log.Debugf("Registering route: Get /v1/admin/medias -> apiController.AdminMedias")
|
||||
router.Get("/v1/admin/medias", Func0(
|
||||
r.apiController.AdminMedias,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/admin/medias/:id -> apiController.AdminMediaShow")
|
||||
router.Get("/v1/admin/medias/:id", Func0(
|
||||
r.apiController.AdminMediaShow,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/admin/orders -> apiController.AdminOrders")
|
||||
router.Get("/v1/admin/orders", Func0(
|
||||
r.apiController.AdminOrders,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/admin/statistics -> apiController.AdminStatistics")
|
||||
router.Get("/v1/admin/statistics", Func0(
|
||||
r.apiController.AdminStatistics,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/auth/login -> apiController.WeChatOAuthCallback")
|
||||
router.Get("/v1/auth/login", Func0(
|
||||
r.apiController.WeChatOAuthCallback,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/auth/wechat -> apiController.WeChatOAuthStart")
|
||||
router.Get("/v1/auth/wechat", Func0(
|
||||
r.apiController.WeChatOAuthStart,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/posts -> apiController.ListPosts")
|
||||
router.Get("/v1/posts", Func1(
|
||||
r.apiController.ListPosts,
|
||||
Query[PostsQuery]("query"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/posts/:id/play -> apiController.PlayPost")
|
||||
router.Get("/v1/posts/:id/play", Func1(
|
||||
r.apiController.PlayPost,
|
||||
PathParam[int]("id"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/posts/:id/show -> apiController.ShowPost")
|
||||
router.Get("/v1/posts/:id/show", Func1(
|
||||
r.apiController.ShowPost,
|
||||
PathParam[int]("id"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/posts/mine -> apiController.MinePosts")
|
||||
router.Get("/v1/posts/mine", Func1(
|
||||
r.apiController.MinePosts,
|
||||
Query[PostsQuery]("query"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/users/profile -> apiController.UserProfile")
|
||||
router.Get("/v1/users/profile", Func0(
|
||||
r.apiController.UserProfile,
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/wechats/js-sdk -> apiController.WechatJSSDK")
|
||||
router.Get("/v1/wechats/js-sdk", Func0(
|
||||
r.apiController.WechatJSSDK,
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /v1/admin/auth -> apiController.AdminAuth")
|
||||
router.Post("/v1/admin/auth", Func0(
|
||||
r.apiController.AdminAuth,
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /v1/admin/orders/:id/refund -> apiController.AdminOrderRefund")
|
||||
router.Post("/v1/admin/orders/:id/refund", Func1(
|
||||
r.apiController.AdminOrderRefund,
|
||||
PathParam[int]("id"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /v1/posts/:id/buy -> apiController.BuyPost")
|
||||
router.Post("/v1/posts/:id/buy", Func1(
|
||||
r.apiController.BuyPost,
|
||||
PathParam[int]("id"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Put /v1/users/username -> apiController.UpdateUsername")
|
||||
router.Put("/v1/users/username", Func1(
|
||||
r.apiController.UpdateUsername,
|
||||
Body[UpdateUsernameReq]("req"),
|
||||
))
|
||||
|
||||
r.log.Info("Successfully registered all routes")
|
||||
}
|
||||
Reference in New Issue
Block a user