Skip to content

Commit

Permalink
Merge pull request #1 from 8acebuzzy/PXJ
Browse files Browse the repository at this point in the history
migrate、gormGen model
  • Loading branch information
8acebuzzy authored Feb 16, 2023
2 parents a2e9974 + f4c3bfa commit 29544ff
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 11 deletions.
34 changes: 34 additions & 0 deletions cmd/gormGen/gen.go
Original file line number Diff line number Diff line change
@@ -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()
}
37 changes: 37 additions & 0 deletions cmd/migrateSchema/migrate.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions dal/initdb.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 13 additions & 0 deletions dal/model/comment.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
18 changes: 9 additions & 9 deletions idl/commentmicro.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 19 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 29544ff

Please sign in to comment.