-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
103 additions
and
3 deletions.
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 @@ | ||
config.yml |
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,18 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/bonfy/go-mega-code/model" | ||
_ "github.com/jinzhu/gorm/dialects/mysql" | ||
) | ||
|
||
func main() { | ||
log.Println("DB Init ...") | ||
db := model.ConnectToDB() | ||
defer db.Close() | ||
model.SetDB(db) | ||
|
||
db.DropTableIfExists(model.User{}, model.Post{}) | ||
db.CreateTable(model.User{}, model.Post{}) | ||
} |
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,6 @@ | ||
mysql: | ||
charset: utf8 | ||
db: dbname | ||
host: localhost | ||
password: password | ||
user: root |
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,36 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
func init() { | ||
projectName := "go-mega" | ||
getConfig(projectName) | ||
} | ||
|
||
func getConfig(projectName string) { | ||
viper.SetConfigName("config") // name of config file (without extension) | ||
|
||
viper.AddConfigPath(".") // optionally look for config in the working directory | ||
viper.AddConfigPath(fmt.Sprintf("$HOME/.%s", projectName)) // call multiple times to add many search paths | ||
viper.AddConfigPath(fmt.Sprintf("/data/docker/config/%s", projectName)) // path to look for the config file in | ||
|
||
err := viper.ReadInConfig() // Find and read the config file | ||
if err != nil { // Handle errors reading the config file | ||
panic(fmt.Errorf("Fatal error config file: %s", err)) | ||
} | ||
} | ||
|
||
// GetMysqlConnectingString func | ||
func GetMysqlConnectingString() string { | ||
usr := viper.GetString("mysql.user") | ||
pwd := viper.GetString("mysql.password") | ||
host := viper.GetString("mysql.host") | ||
db := viper.GetString("mysql.db") | ||
charset := viper.GetString("mysql.charset") | ||
|
||
return fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=%s&parseTime=true", usr, pwd, host, db, charset) | ||
} |
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,27 @@ | ||
package model | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/bonfy/go-mega-code/config" | ||
"github.com/jinzhu/gorm" | ||
) | ||
|
||
var db *gorm.DB | ||
|
||
// SetDB func | ||
func SetDB(database *gorm.DB) { | ||
db = database | ||
} | ||
|
||
// ConnectToDB func | ||
func ConnectToDB() *gorm.DB { | ||
connectingStr := config.GetMysqlConnectingString() | ||
log.Println("Connet to db...") | ||
db, err := gorm.Open("mysql", connectingStr) | ||
if err != nil { | ||
panic("Failed to connect database") | ||
} | ||
db.SingularTable(true) | ||
return db | ||
} |
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 |
---|---|---|
@@ -1,7 +1,14 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Post struct | ||
type Post struct { | ||
User | ||
Body string | ||
ID int `gorm:"primary_key"` | ||
UserID int | ||
User User | ||
Body string `gorm:"varchar(180)"` | ||
Timestamp *time.Time `sql:"DEFAULT:current_timestamp"` | ||
} |
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
2306009
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mysql8.0 无法创建数据库。
create DATABASE go-mega;
中 go-mega 可以改为使用 下划线go_mega