51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
|
|
"yh_web/server/config"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// GetOfficialSite 获取官网站点 ID
|
|
func GetOfficialSite(c *gin.Context) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
siteID := getOfficialSiteID(ctx)
|
|
c.JSON(http.StatusOK, gin.H{"site_id": siteID})
|
|
}
|
|
|
|
// SetOfficialSiteInput 设置官网站点
|
|
type SetOfficialSiteInput struct {
|
|
SiteID string `json:"site_id" binding:"required"`
|
|
}
|
|
|
|
// SetOfficialSite 设置官网站点
|
|
func SetOfficialSite(c *gin.Context) {
|
|
var input SetOfficialSiteInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "请提供 site_id"})
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
coll := config.GetDB(config.DBName).Collection("system_config")
|
|
doc := officialSiteDoc{ID: officialSiteConfigID, SiteID: input.SiteID}
|
|
opts := options.UpdateOne().SetUpsert(true)
|
|
_, err := coll.UpdateOne(ctx, bson.M{"_id": officialSiteConfigID}, bson.M{"$set": doc}, opts)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "已设为官网站点", "site_id": input.SiteID})
|
|
}
|