Skip to content

Commit

Permalink
Merge pull request #33 from idoknow/feat/domain
Browse files Browse the repository at this point in the history
feat: 引入 domain 概念
  • Loading branch information
RockChinQ authored Aug 30, 2024
2 parents 1dc7556 + bcaf289 commit 538d9e4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 54 deletions.
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
14 changes: 5 additions & 9 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Config struct {
// 仅在配置文件不存在时调用
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 @@ -25,8 +25,9 @@ func SetInitValue() {
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")
Expand All @@ -49,14 +50,9 @@ func SetInitValue() {
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")

}

Expand Down
35 changes: 21 additions & 14 deletions backend/mq/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type RedisStreamMQ struct {
PublishPostStream string
NewPostStream string
PostCancelStream string

PublishStatusHash string
Oauth2CodeHash string
}

func NewRedisStreamMQ() *RedisStreamMQ {
Expand All @@ -23,17 +26,21 @@ func NewRedisStreamMQ() *RedisStreamMQ {
DB: viper.GetInt("mq.redis.db"),
})

// 检查流是否存在
client.XGroupCreateMkStream(context.Background(), viper.GetString("mq.redis.stream.publish_post"), "campux", "0")
client.XGroupCreateMkStream(context.Background(), viper.GetString("mq.redis.stream.new_post"), "campux", "0")
client.XGroupCreateMkStream(context.Background(), viper.GetString("mq.redis.stream.post_cancel"), "campux", "0")

return &RedisStreamMQ{
redis := &RedisStreamMQ{
Client: client,
PublishPostStream: viper.GetString("mq.redis.stream.publish_post"),
NewPostStream: viper.GetString("mq.redis.stream.new_post"),
PostCancelStream: viper.GetString("mq.redis.stream.post_cancel"),
PublishPostStream: viper.GetString("service.domain") + ".publish_post",
NewPostStream: viper.GetString("service.domain") + ".new_post",
PostCancelStream: viper.GetString("service.domain") + ".post_cancel",
PublishStatusHash: viper.GetString("service.domain") + ".post_publish_status",
Oauth2CodeHash: viper.GetString("service.domain") + ".oauth2_code",
}

// 检查流是否存在
client.XGroupCreateMkStream(context.Background(), redis.PublishPostStream, "campux", "0")
client.XGroupCreateMkStream(context.Background(), redis.NewPostStream, "campux", "0")
client.XGroupCreateMkStream(context.Background(), redis.PostCancelStream, "campux", "0")

return redis
}

func (r *RedisStreamMQ) PublishPost(postID int) error {
Expand All @@ -58,7 +65,7 @@ func (r *RedisStreamMQ) PublishPost(postID int) error {
}

for _, bot := range bots {
err = r.Client.HSet(context.Background(), viper.GetString("mq.redis.hash.post_publish_status")+strconv.Itoa(postID), "campuxbot_"+strconv.FormatInt(bot, 10), 0).Err()
err = r.Client.HSet(context.Background(), r.PublishStatusHash+":"+strconv.Itoa(postID), "campuxbot_"+strconv.FormatInt(bot, 10), 0).Err()

if err != nil {
return err
Expand Down Expand Up @@ -90,7 +97,7 @@ func (r *RedisStreamMQ) PostCancel(postID int) error {

func (r *RedisStreamMQ) CheckPostPublishStatus(postID int) (bool, error) {
// HGETALL {{ viper.GetString("mq.redis.hash.post_publish_status") }}77
status, err := r.Client.HGetAll(context.Background(), viper.GetString("mq.redis.hash.post_publish_status")+strconv.Itoa(postID)).Result()
status, err := r.Client.HGetAll(context.Background(), r.PublishStatusHash+":"+strconv.Itoa(postID)).Result()

if err != nil {
return false, err
Expand All @@ -107,16 +114,16 @@ func (r *RedisStreamMQ) CheckPostPublishStatus(postID int) (bool, error) {

// 删除稿件发布跟踪hash表
func (r *RedisStreamMQ) DeletePostPublishStatus(postID int) error {
_, err := r.Client.Del(context.Background(), viper.GetString("mq.redis.hash.post_publish_status")+strconv.Itoa(postID)).Result()
_, err := r.Client.Del(context.Background(), r.PublishStatusHash+":"+strconv.Itoa(postID)).Result()
return err
}

// 存储oauth2_code和uin对应关系 十分钟过期
func (r *RedisStreamMQ) SetOauth2Code(code string, uin int64) error {
return r.Client.Set(context.Background(), viper.GetString("mq.redis.prefix.oauth2_code")+code, uin, 60*10*time.Second).Err()
return r.Client.Set(context.Background(), r.Oauth2CodeHash+code, uin, 60*10*time.Second).Err()
}

// 获取oauth2_code对应的uin
func (r *RedisStreamMQ) GetOauth2Uin(code string) (int64, error) {
return r.Client.Get(context.Background(), viper.GetString("mq.redis.prefix.oauth2_code")+code).Int64()
return r.Client.Get(context.Background(), r.Oauth2CodeHash+code).Int64()
}
53 changes: 53 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
version: "3"
services:
redis:
image: redis:6-alpine
restart: always
container_name: campux-redis
volumes:
# Mount the redis data directory to the container.
- ./volumes/redis/data:/data
# Set the redis password when startup redis server.
command: redis-server --requirepass campux123456
healthcheck:
test: [ "CMD", "redis-cli", "ping" ]
networks:
- campux-network

campux:
image: rockchin/campux:latest
container_name: campux
ports:
- 8081:8081
volumes:
- ./volumes/campux/data:/app/data
networks:
- campux-network
environment:
- GIN_MODE=release

campuxbot:
image: rockchin/campuxbot:latest
container_name: campux-bot
restart: always
ports:
- 8282:8282
volumes:
- ./volumes/campuxbot/data:/app/data
environment:
- TZ=Asia/Shanghai
networks:
- campux-network

campuxutility:
image: rockchin/campuxutility:latest
container_name: campux-utility
restart: always
volumes:
- ./volumes/campuxutility/data:/app/data
networks:
- campux-network

networks:
campux-network:
external: true
13 changes: 0 additions & 13 deletions docker/minio/docker-compose.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions docker/mongodb/docker-compose.yaml

This file was deleted.

8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func main() {
}

// 配置文件
_, created, err := config.NewConfig()
_, _, err = config.NewConfig()

if err != nil {
panic(err)
}

if created {
panic("请修改配置文件后重启")
}
// if created {
// panic("请修改配置文件后重启")
// }

err = migrate.DoMigration()

Expand Down

0 comments on commit 538d9e4

Please sign in to comment.