-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
27 changed files
with
3,215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"ByteRhythm/app/comment/dao" | ||
"ByteRhythm/app/comment/service" | ||
"ByteRhythm/config" | ||
"ByteRhythm/idl/comment/commentPb" | ||
"github.com/go-micro/plugins/v4/registry/etcd" | ||
|
||
"fmt" | ||
|
||
"go-micro.dev/v4" | ||
"go-micro.dev/v4/registry" | ||
) | ||
|
||
func main() { | ||
config.Init() | ||
dao.InitMySQL() | ||
dao.InitRedis() | ||
|
||
// etcd注册件 | ||
etcdReg := etcd.NewRegistry( | ||
registry.Addrs(fmt.Sprintf("%s:%s", config.EtcdHost, config.EtcdPort)), | ||
) | ||
|
||
// 得到一个微服务实例 | ||
microService := micro.NewService( | ||
micro.Name("CommentService"), // 微服务名字 | ||
micro.Address(config.CommentServiceAddress), | ||
micro.Registry(etcdReg), // etcd注册件 | ||
) | ||
|
||
// 结构命令行参数,初始化 | ||
microService.Init() | ||
// 服务注册 | ||
_ = commentPb.RegisterCommentServiceHandler(microService.Server(), service.GetCommentSrv()) | ||
// 启动微服务 | ||
_ = microService.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package dao | ||
|
||
import ( | ||
"ByteRhythm/model" | ||
"ByteRhythm/util" | ||
"context" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type CommentDao struct { | ||
*gorm.DB | ||
} | ||
|
||
func NewCommentDao(ctx context.Context) *CommentDao { | ||
if ctx == nil { | ||
ctx = context.Background() | ||
} | ||
return &CommentDao{NewDBClient(ctx)} | ||
} | ||
|
||
func (c *CommentDao) CreateComment(comment *model.Comment) (err error) { | ||
err = c.Model(&model.Comment{}).Create(&comment).Error | ||
if err != nil { | ||
return | ||
} | ||
return nil | ||
} | ||
|
||
func (c *CommentDao) DeleteComment(comment *model.Comment) (err error) { | ||
err = c.Model(&model.Comment{}).Where(&comment).Delete(&comment).Error | ||
if err != nil { | ||
return | ||
} | ||
return nil | ||
} | ||
|
||
func (c *CommentDao) GetCommentListByVideoId(vid int64) (comments []*model.Comment, err error) { | ||
err = c.Model(&model.Comment{}).Where("video_id = ?", vid).Order("id desc").Find(&comments).Error | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (c *CommentDao) GetUsernameByUid(uid int64) (username string, err error) { | ||
err = c.Model(&model.User{}).Where("id = ?", uid).Select("username").Find(&username).Error | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (c *CommentDao) GetAvatarByUid(uid int64) (avatar string, err error) { | ||
err = c.Model(&model.User{}).Where("id = ?", uid).Select("avatar").Find(&avatar).Error | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (v *CommentDao) GetFollowCount(uid int) (count int64, err error) { | ||
err = v.Model(&model.Follow{}).Where("followed_user_id = ?", uid).Count(&count).Error | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (v *CommentDao) GetFollowerCount(uid int) (count int64, err error) { | ||
err = v.Model(&model.Follow{}).Where("user_id = ?", uid).Count(&count).Error | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (v *CommentDao) GetWorkCount(uid int) (count int64, err error) { | ||
err = v.Model(&model.Video{}).Where("author_id = ?", uid).Count(&count).Error | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (v *CommentDao) GetUserFavoriteCount(uid int) (count int64, err error) { | ||
err = v.Model(&model.Favorite{}).Where("user_id = ?", uid).Count(&count).Error | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (v *CommentDao) GetFavoriteCount(vid int) (count int64, err error) { | ||
err = v.Model(&model.Favorite{}).Where("video_id = ?", vid).Count(&count).Error | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (v *CommentDao) GetTotalFavorited(uid int) (count int64, err error) { | ||
var videos []*model.Video | ||
err = v.Model(&model.Video{}).Where("author_id = ?", uid).Find(&videos).Error | ||
if err != nil { | ||
return | ||
} | ||
for _, video := range videos { | ||
var favoriteCount int64 | ||
err = v.Model(&model.Favorite{}).Where("video_id = ?", video.ID).Count(&favoriteCount).Error | ||
if err != nil { | ||
return | ||
} | ||
count += favoriteCount | ||
} | ||
return | ||
} | ||
|
||
func (v *CommentDao) GetIsFollowed(uid int, token string) (isFollowed bool, err error) { | ||
|
||
baseID, err := util.GetUserIdFromToken(token) | ||
if err != nil { | ||
return | ||
} | ||
var follow model.Follow | ||
err = v.Model(&model.Follow{}).Where("user_id = ?", baseID).Where("followed_user_id = ?", uid).Limit(1).Find(&follow).Error | ||
if err != nil { | ||
return | ||
} | ||
if follow.ID != 0 { | ||
isFollowed = true | ||
} else { | ||
isFollowed = false | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package dao | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/go-redis/redis/v8" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"gorm.io/gorm/schema" | ||
|
||
"ByteRhythm/config" | ||
) | ||
|
||
var db *gorm.DB | ||
|
||
var RedisNo2Client *redis.Client | ||
var RedisNo1Client *redis.Client | ||
|
||
func InitRedis() { | ||
// 初始化 Redis 客户端 | ||
host := config.RedisHost | ||
port := config.RedisPort | ||
RedisNo2Client = redis.NewClient(&redis.Options{ | ||
Addr: host + ":" + port, // Redis 服务器地址 | ||
Password: "", // Redis 访问密码(如果有的话) | ||
DB: 2, // Redis 数据库索引 | ||
}) | ||
|
||
RedisNo1Client = redis.NewClient(&redis.Options{ | ||
Addr: host + ":" + port, // Redis 服务器地址 | ||
Password: "", // Redis 访问密码(如果有的话) | ||
DB: 1, // Redis 数据库索引 | ||
}) | ||
} | ||
|
||
func InitMySQL() { | ||
host := config.DBHost | ||
port := config.DBPort | ||
database := config.DBName | ||
username := config.DBUser | ||
password := config.DBPassWord | ||
charset := config.Charset | ||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local", username, password, host, port, database, charset) | ||
err := Database(dsn) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func Database(connString string) error { | ||
var ormLogger logger.Interface | ||
if gin.Mode() == "debug" { | ||
ormLogger = logger.Default.LogMode(logger.Info) | ||
} else { | ||
ormLogger = logger.Default | ||
} | ||
DB, err := gorm.Open(mysql.New(mysql.Config{ | ||
DSN: connString, // DSN data source name | ||
DefaultStringSize: 256, // string 类型字段的默认长度 | ||
DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持 | ||
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引 | ||
DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 | ||
SkipInitializeWithVersion: false, // 根据版本自动配置 | ||
}), &gorm.Config{ | ||
Logger: ormLogger, | ||
NamingStrategy: schema.NamingStrategy{ | ||
SingularTable: true, | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
sqlDB, _ := DB.DB() | ||
sqlDB.SetMaxIdleConns(20) | ||
sqlDB.SetMaxOpenConns(100) | ||
sqlDB.SetConnMaxLifetime(time.Second * 30) | ||
db = DB | ||
migration() | ||
return err | ||
} | ||
|
||
func NewDBClient(ctx context.Context) *gorm.DB { | ||
DB := db | ||
return DB.WithContext(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dao | ||
|
||
import ( | ||
"ByteRhythm/model" | ||
"log" | ||
) | ||
|
||
func migration() { | ||
err := db.Set(`gorm:table_options`, "charset=utf8mb4"). | ||
AutoMigrate(&model.User{}, &model.Follow{}, &model.Video{}, &model.Favorite{}, &model.Comment{}, &model.Message{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.