Skip to content

Commit

Permalink
Merge pull request #34 from idoknow/version/1.0.0
Browse files Browse the repository at this point in the history
Release: 1.0.0
  • Loading branch information
RockChinQ authored Sep 2, 2024
2 parents 293ffc0 + 538d9e4 commit 9b45d38
Show file tree
Hide file tree
Showing 41 changed files with 1,432 additions and 205 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ pnpm-debug.log*
*.sln
*.sw?
frontend/package-lock.json
dist/
dist/
/data/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WORKDIR /app
COPY bin/campux /app/campux
COPY frontend/dist /app/frontend/dist

EXPOSE 8080
EXPOSE 8081

ENV GIN_MODE=release

Expand Down
38 changes: 24 additions & 14 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
type Config struct {
}

func SetDefault() {
// 设置初始值
// 仅在配置文件不存在时调用
func SetInitValue() {
viper.SetDefault("backend.host", "0.0.0.0")
viper.SetDefault("backend.port", "8080")
viper.SetDefault("backend.port", "8081")

// jwt
viper.SetDefault("auth.jwt.secret", uuid.New().String())
Expand All @@ -23,13 +25,23 @@ func SetDefault() {
viper.SetDefault("oauth2.server.ak_expire", 3600*24*14)

// 服务token
viper.SetDefault("service.token", "campux")
viper.SetDefault("service.token", "campux123456")
viper.SetDefault("service.bots", []int64{123456789})
viper.SetDefault("service.domain", "campux")

// 数据库
viper.SetDefault("database.use", "sqlite")

viper.SetDefault("database.sqlite.path", "./data/campux.db")

viper.SetDefault("database.mongo.uri", "mongodb://localhost:27017")
viper.SetDefault("database.mongo.db", "campux")

viper.SetDefault("oss.use", "local")

// local
viper.SetDefault("oss.local.dir", "./data/objects")

// minio
viper.SetDefault("oss.minio.endpoint", "localhost:9000")
viper.SetDefault("oss.minio.access_key", "minio")
Expand All @@ -38,17 +50,16 @@ func SetDefault() {
viper.SetDefault("oss.minio.use_ssl", false)

// redis
viper.SetDefault("mq.redis.addr", "localhost:6379")
viper.SetDefault("mq.redis.password", "")
viper.SetDefault("mq.redis.addr", "campux-redis:6379")
viper.SetDefault("mq.redis.password", "campux123456")
viper.SetDefault("mq.redis.db", 0)
viper.SetDefault("mq.redis.stream.publish_post", "campux_publish_post")
viper.SetDefault("mq.redis.stream.new_post", "campux_new_post")
viper.SetDefault("mq.redis.stream.post_cancel", "campux_post_cancel")
viper.SetDefault("mq.redis.hash.post_publish_status", "campux_post_publish_status")
viper.SetDefault("mq.redis.prefix.oauth2_code", "campux_oauth2_code")

}

func WriteConfig() error {
return viper.WriteConfigAs("./data/campux.yaml")
}

// 创建配置文件对象
// 返回值1:配置文件对象
// 返回值2:是否新建配置文件
Expand All @@ -59,13 +70,12 @@ func NewConfig() (*Config, bool, error) {
viper.SetConfigType("yaml")
viper.AddConfigPath("./data/")

// 设置默认配置
SetDefault()

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// 设置默认配置
SetInitValue()
// Config file not found; write default config
if err := viper.WriteConfigAs("./data/campux.yaml"); err != nil {
if err := WriteConfig(); err != nil {
return nil, false, err
}
return nil, true, nil
Expand Down
63 changes: 60 additions & 3 deletions backend/controller/admapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (

type AdminRouter struct {
APIRouter
AdminService service.AdminService
AdminService service.AdminService
AccountService service.AccountService
}

func NewAdminRouter(rg *gin.RouterGroup, as service.AdminService) *AdminRouter {
func NewAdminRouter(rg *gin.RouterGroup, as service.AdminService, acs service.AccountService) *AdminRouter {
ar := &AdminRouter{
AdminService: as,
AdminService: as,
AccountService: acs,
}

group := rg.Group("/admin")
Expand All @@ -22,6 +24,8 @@ func NewAdminRouter(rg *gin.RouterGroup, as service.AdminService) *AdminRouter {
group.POST("/add-oauth2-app", ar.AddOAuth2App)
group.GET("/get-oauth2-apps", ar.GetOAuth2AppList)
group.DELETE("/del-oauth2-app/:id", ar.DeleteOAuth2App)
group.GET("/init", ar.IsInit)
group.POST("/init", ar.Init)

return ar
}
Expand Down Expand Up @@ -117,3 +121,56 @@ func (ar *AdminRouter) DeleteOAuth2App(c *gin.Context) {

ar.Success(c, nil)
}

// 检查是否初始化
func (ar *AdminRouter) IsInit(c *gin.Context) {
// 检查是否初始化
init, err := ar.AdminService.IsInit()

if err != nil {
ar.Fail(c, 1, err.Error())
return
}

ar.Success(c, gin.H{
"initialized": init,
})
}

// 初始化
func (ar *AdminRouter) Init(c *gin.Context) {
// 检查是否初始化
init, err := ar.AdminService.IsInit()

if err != nil {
ar.Fail(c, 1, err.Error())
return
}

if init {
ar.Fail(c, 2, "系统已有管理员账户")
return
}

// 取body的json里的appname
var body InitBody

if err := c.ShouldBindJSON(&body); err != nil {
ar.Fail(c, 3, err.Error())
return
}

// 创建管理员账户
err = ar.AccountService.AddAccount(
body.AdminUin,
body.AdminPasswd,
database.USER_GROUP_ADMIN,
)

if err != nil {
ar.Fail(c, 4, err.Error())
return
}

ar.Success(c, nil)
}
4 changes: 3 additions & 1 deletion backend/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func NewApiController(
cors.Config{
AllowOrigins: []string{
"http://localhost:3000",
"http://localhost:3001",
"http://127.0.0.1:3000",
"http://127.0.0.1:3001",
},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization", "Cookie"},
Expand Down Expand Up @@ -65,7 +67,7 @@ func NewApiController(
NewAccountRouter(rg, as)
NewPostRouter(rg, ps, as)
NewMiscRouter(rg, ms)
NewAdminRouter(rg, ads)
NewAdminRouter(rg, ads, as)
NewOAuth2Router(rg, oas)

return &APIController{
Expand Down
21 changes: 21 additions & 0 deletions backend/controller/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,24 @@ type OAuth2GetAccessTokenBody struct {
// 授权码
Code string `json:"code" binding:"required"`
}

type SetMetadataBody struct {
// 键
Key string `json:"key" binding:"required"`

// 值
Value string `json:"value" binding:"required"`
}

type SaveMetadataBody struct {
// 元数据列表
MetadataList []database.Metadata `json:"list" binding:"required"`
}

type InitBody struct {
// 管理员uin
AdminUin int64 `json:"admin_uin" binding:"required"`

// 管理员密码
AdminPasswd string `json:"admin_passwd" binding:"required"`
}
103 changes: 103 additions & 0 deletions backend/controller/miscapi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"github.com/RockChinQ/Campux/backend/database"
"github.com/RockChinQ/Campux/backend/service"
"github.com/gin-gonic/gin"
)
Expand All @@ -19,6 +20,9 @@ func NewMiscRouter(rg *gin.RouterGroup, ms service.MiscService) *MiscRouter {

// bind routes
group.GET("/get-metadata", mr.GetMetadata)
group.PUT("/set-metadata", mr.SetMetadata)
group.GET("/get-metadata-list", mr.GetMetadataList)
group.PUT("/save-metadatas", mr.SaveMetadata)

return mr
}
Expand All @@ -43,3 +47,102 @@ func (mr *MiscRouter) GetMetadata(c *gin.Context) {
"value": value,
})
}

// 设置元数据
func (mr *MiscRouter) SetMetadata(c *gin.Context) {
uin, err := mr.Auth(c, Both)

if err != nil {
return
}

// 检查用户权限
if !mr.MiscService.CheckUserGroup(uin, []database.UserGroup{
database.USER_GROUP_ADMIN,
database.USER_GROUP_MEMBER,
}) {
mr.StatusCode(c, 401, "权限不足")
return
}

var body SetMetadataBody

if err := c.ShouldBindJSON(&body); err != nil {
mr.Fail(c, 1, err.Error())
return
}

key := body.Key
value := body.Value

err = mr.MiscService.SetMetadata(key, value)

if err != nil {
mr.Fail(c, 1, err.Error())
return
}

mr.Success(c, nil)
}

// 获取元数据列表
func (mr *MiscRouter) GetMetadataList(c *gin.Context) {
uin, err := mr.Auth(c, Both)

if err != nil {
return
}

// 检查用户权限
if !mr.MiscService.CheckUserGroup(uin, []database.UserGroup{
database.USER_GROUP_ADMIN,
database.USER_GROUP_MEMBER,
}) {
mr.StatusCode(c, 401, "权限不足")
return
}

list, err := mr.MiscService.GetMetadataList()

if err != nil {
mr.Fail(c, 1, err.Error())
return
}

mr.Success(c, gin.H{
"list": list,
})
}

// 保存元数据
func (mr *MiscRouter) SaveMetadata(c *gin.Context) {
uin, err := mr.Auth(c, Both)

if err != nil {
return
}

// 检查用户权限
if !mr.MiscService.CheckUserGroup(uin, []database.UserGroup{
database.USER_GROUP_ADMIN,
}) {
mr.StatusCode(c, 401, "权限不足")
return
}

var body SaveMetadataBody

if err := c.ShouldBindJSON(&body); err != nil {
mr.Fail(c, 1, err.Error())
return
}

err = mr.MiscService.SaveMetadata(body.MetadataList)

if err != nil {
mr.Fail(c, 1, err.Error())
return
}

mr.Success(c, nil)
}
Loading

0 comments on commit 9b45d38

Please sign in to comment.