init repo

This commit is contained in:
VaalaCat
2024-08-28 00:02:28 +08:00
committed by vaalacat
commit 13148b95e3
97 changed files with 10214 additions and 0 deletions

29
biz/handler.go Normal file
View File

@@ -0,0 +1,29 @@
package biz
import (
"github.com/nose7en/ToyBoomServer/biz/user"
"github.com/nose7en/ToyBoomServer/common"
"github.com/nose7en/ToyBoomServer/config"
"github.com/nose7en/ToyBoomServer/middleware"
"github.com/gin-gonic/gin"
)
func Router() *gin.Engine {
r := gin.Default()
api := r.Group("/api")
v1 := api.Group("/v1")
userRouter := v1.Group("/user")
{
userRouter.POST("/create", middleware.ValidateAppleAppToken(), common.Wrapper(user.CreateUser))
userRouter.GET("/info", middleware.ValidateAppleAppToken(), common.Wrapper(user.GetUserInfo))
}
if config.IsDebug() {
// for debug
v1.GET("/ping", middleware.ValidateAppleAppToken(), func(ctx *gin.Context) { ctx.JSON(200, gin.H{"message": "pong"}) })
}
return r
}

53
biz/static.go Normal file
View File

@@ -0,0 +1,53 @@
package biz
import (
"embed"
"io/fs"
"net/http"
"strings"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
type embedFileSystem struct {
http.FileSystem
}
func (e embedFileSystem) Exists(prefix string, path string) bool {
_, err := e.Open(path)
return err == nil
}
func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
fsys, err := fs.Sub(fsEmbed, targetPath)
if err != nil {
panic(err)
}
return embedFileSystem{
FileSystem: http.FS(fsys),
}
}
func HandleStaticFile(router *gin.Engine, f embed.FS) {
root := EmbedFolder(f, "out")
router.Use(static.Serve("/", root))
staticServer := static.Serve("/", root)
router.NoRoute(func(c *gin.Context) {
if c.Request.Method == http.MethodGet &&
!strings.ContainsRune(c.Request.URL.Path, '.') &&
!strings.HasPrefix(c.Request.URL.Path, "/v1/") {
if strings.HasSuffix(c.Request.URL.Path, "/") {
c.Request.URL.Path += "index.html"
staticServer(c)
return
}
if !strings.HasSuffix(c.Request.URL.Path, ".html") {
c.Request.URL.Path += ".html"
staticServer(c)
return
}
}
})
}

24
biz/user/create.go Normal file
View File

@@ -0,0 +1,24 @@
package user
import (
"context"
"github.com/nose7en/ToyBoomServer/common"
"github.com/nose7en/ToyBoomServer/dao"
"github.com/nose7en/ToyBoomServer/defs"
"github.com/nose7en/ToyBoomServer/models"
)
func CreateUser(c context.Context, req *defs.CommonRequest) (*defs.CommonResponse, error) {
userInfo := common.GetUser(c)
newUser := &models.User{}
newUser.FillWithUserInfo(userInfo)
if err := dao.NewMutation().CreateUser(newUser); err != nil {
return nil, err
}
return &defs.CommonResponse{
Status: &defs.Status{Code: defs.RespCode_SUCCESS, Message: defs.RespMessage_SUCCESS},
}, nil
}

16
biz/user/get.go Normal file
View File

@@ -0,0 +1,16 @@
package user
import (
"context"
"github.com/nose7en/ToyBoomServer/common"
"github.com/nose7en/ToyBoomServer/defs"
"github.com/samber/lo"
)
func GetUserInfo(c context.Context, req *defs.CommonRequest) (resp *defs.GetUserInfoResponse, err error) {
userInfo := common.GetUser(c)
return &defs.GetUserInfoResponse{
User: lo.ToPtr(userInfo.ToUser()),
}, nil
}