From f4c3bfa75dc83702f4b6c039cc1d82e270be4cce Mon Sep 17 00:00:00 2001 From: "1730530019@qq.com" <8acebuzzy> Date: Thu, 16 Feb 2023 07:40:00 -0800 Subject: [PATCH] =?UTF-8?q?migrate=E3=80=81gormGen=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/gormGen/gen.go | 34 +++++++++++++++++++++++++++++++++ cmd/migrateSchema/migrate.go | 37 ++++++++++++++++++++++++++++++++++++ dal/initdb.go | 21 ++++++++++++++++++++ dal/model/comment.go | 13 +++++++++++++ go.sum | 12 ++++++++++++ idl/commentmicro.thrift | 18 +++++++++--------- main.go | 21 ++++++++++++++++++-- 7 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 cmd/gormGen/gen.go create mode 100644 cmd/migrateSchema/migrate.go create mode 100644 dal/initdb.go create mode 100644 dal/model/comment.go create mode 100644 go.sum diff --git a/cmd/gormGen/gen.go b/cmd/gormGen/gen.go new file mode 100644 index 0000000..0634389 --- /dev/null +++ b/cmd/gormGen/gen.go @@ -0,0 +1,34 @@ +package main + +import ( + "log" + + "github.com/ClubWeGo/commentmicro/dal/model" + + "gorm.io/driver/mysql" + "gorm.io/gen" + "gorm.io/gorm" +) + +func main() { + + g := gen.NewGenerator(gen.Config{ + OutPath: "../../dal/query", + Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, + }) + + dsn := "tk:123456@tcp(127.0.0.1:3306)/simpletk?charset=utf8&parseTime=True&loc=Local" + db, err := gorm.Open(mysql.Open(dsn)) + if err != nil { + log.Fatal(err) + return + } + + g.UseDB(db) + + g.ApplyBasic(model.User{}) + + // g.ApplyInterface(func(model.UserMethod) {}, model.User{}) + + g.Execute() +} diff --git a/cmd/migrateSchema/migrate.go b/cmd/migrateSchema/migrate.go new file mode 100644 index 0000000..627d375 --- /dev/null +++ b/cmd/migrateSchema/migrate.go @@ -0,0 +1,37 @@ +package main + +import ( + "log" + + "github.com/ClubWeGo/commentmicro/dal/model" + + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +func InitComment(db *gorm.DB) { + err := db.AutoMigrate(&model.comment{}) + if err != nil { + log.Fatal(err) + } +} + +func main() { + var datetimePrecision = 2 + dsn := "tk:123456@tcp(127.0.0.1:3306)/simpletk?charset=utf8&parseTime=True&loc=Local" + db, err := gorm.Open(mysql.New(mysql.Config{ + DSN: dsn, // data source name, refer https://github.com/go-sql-driver/mysql#dsn-data-source-name + DefaultStringSize: 256, // add default size for string fields, by default, will use db type `longtext` for fields without size, not a primary key, no index defined and don't have default values + DisableDatetimePrecision: true, // disable datetime precision support, which not supported before MySQL 5.6 + DefaultDatetimePrecision: &datetimePrecision, // default datetime precision + DontSupportRenameIndex: true, // drop & create index when rename index, rename index not supported before MySQL 5.7, MariaDB + DontSupportRenameColumn: true, // use change when rename column, rename rename not supported before MySQL 8, MariaDB + SkipInitializeWithVersion: false, // smart configure based on used version + }), &gorm.Config{}) + if err != nil { + log.Fatal(err) + return + } + + InitComment(db) +} \ No newline at end of file diff --git a/dal/initdb.go b/dal/initdb.go new file mode 100644 index 0000000..63fb821 --- /dev/null +++ b/dal/initdb.go @@ -0,0 +1,21 @@ +package dal + +import ( + "log" + + "github.com/ClubWeGo/commentmicro/dal/query" + + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func InitDB(dsn string) { + database, err := gorm.Open(mysql.Open(dsn)) + if err != nil { + log.Fatal(err) + } + DB = database + query.SetDefault(DB) +} diff --git a/dal/model/comment.go b/dal/model/comment.go new file mode 100644 index 0000000..b87315c --- /dev/null +++ b/dal/model/comment.go @@ -0,0 +1,13 @@ +package model +import( + "gorm.io/gorm" +) + +type comment struct{ + gorm.Model + videoid int64 `gorm:"type:varchar(128);not null;index"` \\视频id + videouserid int64 `gorm:"type:varchar(128);not null;index"` \\视频主id + commentuserid int64 `gorm:"not null"` + content string `gorm:"type:varchar(256)"` + is_follow bool +} \ No newline at end of file diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..253f0d1 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI= +github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= +github.com/bytedance/gopkg v0.0.0-20220531084716-665b4f21126f h1:2YCF3cgO6XCub0HIsLrA8ZGhmAPGZfOeSaGjT6Kx4Mc= +github.com/bytedance/gopkg v0.0.0-20220531084716-665b4f21126f/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q= +github.com/chenzhuoyu/iasm v0.0.0-20220818063314-28c361dae733 h1:Hx6Jxqln+bHRrtjUdgrehhF3gtWVJ2S7bjO/YTNn8Fg= +github.com/chenzhuoyu/iasm v0.0.0-20220818063314-28c361dae733/go.mod h1:wOQ0nsbeOLa2awv8bUYFW/EHXbjQMlZ10fAlXDB2sz8= +github.com/choleraehyq/pid v0.0.15 h1:PejhUZowqxxssjwyaw4OZURRFjnvftZfeEWK9UoWPXU= +github.com/choleraehyq/pid v0.0.15/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= +github.com/cloudwego/fastpb v0.0.3 h1:GZE0WzlnjQFE3+vkYFZd964PGT9AXOuvir+JGzuBSPM= +github.com/cloudwego/fastpb v0.0.3/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0= +github.com/cloudwego/frugal v0.1.3 h1:tw3+hh4YMmtHFHRue3OGYjAnkxnZRHqeAyG18+7z5aI= +github.com/cloudwego/frugal v0.1.3/go.mod h1:b981ViPYdhI56aFYsoMjl9kv6yeqYSO+iEz2jrhkCgI= diff --git a/idl/commentmicro.thrift b/idl/commentmicro.thrift index a6d6427..8b44da5 100644 --- a/idl/commentmicro.thrift +++ b/idl/commentmicro.thrift @@ -4,23 +4,23 @@ namespace go comment struct User { 1: required i64 id; 2: required string name; - 3: optional i64 follow_count; - 4: optional i64 follower_count; - 5: required bool is_follow; + 3: optional i64 follow_count;//关注总数 + 4: optional i64 follower_count;//粉丝总数 + 5: required bool is_follow;//true 已关注 false未关注该视频主 } struct Comment { - 1: required i64 id; - 2: required User user; - 3: required string content; - 4: required string create_date; + 1: required i64 id; //视频评论id + 2: required User user;//评论用户的id + 3: required string content; //评论内容 + 4: required string create_date;//评论发布日期时间 mm-dd } # commentList struct CommentListReq { - 1: required string token; - 2: required i64 video_id; + 1: required string token;//用户鉴权token + 2: required i64 video_id;//视频id } struct CommentListResp { diff --git a/main.go b/main.go index b042f9e..89c12c0 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,29 @@ package main import ( comment "github.com/ClubWeGo/commentmicro/kitex_gen/comment/commentservice" "log" + "net" + etcd "github.com/kitex-contrib/registry-etcd" + "github.com/ClubWeGo/commentmicro/dal" + "github.com/cloudwego/kitex/pkg/rpcinfo" + "github.com/cloudwego/kitex/server" ) func main() { - svr := comment.NewServer(new(CommentServiceImpl)) + dsn := "tk:123456@tcp(127.0.0.1:3306)/simpletk?charset=utf8&parseTime=True&loc=Local" + dal.InitDB(dsn) - err := svr.Run() + r, err := etcd.NewEtcdRegistry([]string{"0.0.0.0:2379"}) + if err != nil { + log.Fatal(err) + } + + addr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:10010") + svr := comment.NewServer(new(CommentServiceImpl), + server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "commentservice"}), + server.WithRegistry(r), + server.WithServiceAddr(addr)) + + err = svr.Run() if err != nil { log.Println(err.Error())