diff --git a/backend/controller/api.go b/backend/controller/api.go index b9ef717..7197bdb 100644 --- a/backend/controller/api.go +++ b/backend/controller/api.go @@ -18,6 +18,7 @@ type APIController struct { func NewApiController( as service.AccountService, ps service.PostService, + ms service.MiscService, ) *APIController { r := gin.Default() @@ -45,7 +46,7 @@ func NewApiController( return } // 没有文件都返回/ - if util.IsFileExist("./frontend/dist"+c.Request.URL.Path) == false { + if !util.IsFileExist("./frontend/dist" + c.Request.URL.Path) { http.ServeFile(c.Writer, c.Request, "./frontend/dist/index.html") } else { http.ServeFile(c.Writer, c.Request, "./frontend/dist"+c.Request.URL.Path) @@ -57,6 +58,7 @@ func NewApiController( // bind routes NewAccountRouter(rg, as) NewPostRouter(rg, ps) + NewMiscRouter(rg, ms) return &APIController{ R: r, diff --git a/backend/controller/miscapi.go b/backend/controller/miscapi.go new file mode 100644 index 0000000..c6f8769 --- /dev/null +++ b/backend/controller/miscapi.go @@ -0,0 +1,45 @@ +package controller + +import ( + "github.com/RockChinQ/Campux/backend/service" + "github.com/gin-gonic/gin" +) + +type MiscRouter struct { + APIRouter + MiscService service.MiscService +} + +func NewMiscRouter(rg *gin.RouterGroup, ms service.MiscService) *MiscRouter { + mr := &MiscRouter{ + MiscService: ms, + } + + group := rg.Group("/misc") + + // bind routes + group.GET("/get-metadata", mr.GetMetadata) + + return mr +} + +// 获取元数据 +func (mr *MiscRouter) GetMetadata(c *gin.Context) { + key := c.Query("key") + + if key == "" { + mr.Fail(c, 1, "key is required") + return + } + + value, err := mr.MiscService.GetMetadata(key) + + if err != nil { + mr.Fail(c, 1, err.Error()) + return + } + + mr.Success(c, gin.H{ + "value": value, + }) +} diff --git a/backend/core/app.go b/backend/core/app.go index 89f7d0e..d489ce9 100644 --- a/backend/core/app.go +++ b/backend/core/app.go @@ -19,8 +19,10 @@ func NewApplication() *Application { fs := oss.NewMinioClient() ps := service.NewPostService(*db, *fs) + ms := service.NewMiscService(*db) + return &Application{ - API: controller.NewApiController(*as, *ps), + API: controller.NewApiController(*as, *ps, *ms), } } diff --git a/backend/database/mongo.go b/backend/database/mongo.go index 8ae713e..10c04d0 100644 --- a/backend/database/mongo.go +++ b/backend/database/mongo.go @@ -13,6 +13,7 @@ const ( ACCOUNT_COLLECTION = "account" POST_COLLECTION = "post" POST_LOG_COLLECTION = "post_log" + METADATA_COLLECTION = "metadata" ) type MongoDBManager struct { @@ -172,3 +173,17 @@ func (m *MongoDBManager) UpdatePostStatus(id int, status PostStatus) error { ) return err } + +func (m *MongoDBManager) GetMetadata(key string) (string, error) { + var meta struct { + Value string `bson:"value"` + } + err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(METADATA_COLLECTION).FindOne( + context.TODO(), + bson.M{"key": key}, + ).Decode(&meta) + if err != nil { + return "", err + } + return meta.Value, nil +} diff --git a/backend/service/misc.go b/backend/service/misc.go new file mode 100644 index 0000000..7a4221f --- /dev/null +++ b/backend/service/misc.go @@ -0,0 +1,17 @@ +package service + +import "github.com/RockChinQ/Campux/backend/database" + +type MiscService struct { + DB database.MongoDBManager +} + +func NewMiscService(db database.MongoDBManager) *MiscService { + return &MiscService{ + DB: db, + } +} + +func (ms *MiscService) GetMetadata(key string) (string, error) { + return ms.DB.GetMetadata(key) +}