共用配置文件导入
This commit is contained in:
		@@ -11,22 +11,20 @@ import (
 | 
			
		||||
	"github.com/jmoiron/sqlx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var connection *sqlx.DB
 | 
			
		||||
func NewSchema(config Config) (graphql.Schema, error) {
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	var err error
 | 
			
		||||
	user := models.Viper.Get("mysql.user").(string)
 | 
			
		||||
	password := models.Viper.Get("mysql.password").(string)
 | 
			
		||||
	host := models.Viper.Get("mysql.host").(string)
 | 
			
		||||
	port := models.Viper.Get("mysql.port").(int)
 | 
			
		||||
	database := models.Viper.Get("mysql.database").(string)
 | 
			
		||||
	connection, err = sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database))
 | 
			
		||||
	// 打开数据库连接
 | 
			
		||||
	connection, err := sqlx.Connect("mysql", fmt.Sprintf(
 | 
			
		||||
		"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
 | 
			
		||||
		config.Mysql.UserName,
 | 
			
		||||
		config.Mysql.Password,
 | 
			
		||||
		config.Mysql.Host,
 | 
			
		||||
		config.Mysql.Port,
 | 
			
		||||
		config.Mysql.Database,
 | 
			
		||||
	))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln("连接数据库失败", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewSchema() (graphql.Schema, error) {
 | 
			
		||||
 | 
			
		||||
	// 文章的可选字段
 | 
			
		||||
	article := graphql.NewObject(graphql.ObjectConfig{
 | 
			
		||||
@@ -54,6 +52,9 @@ func NewSchema() (graphql.Schema, error) {
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	// 图像中的文字提取
 | 
			
		||||
	// text := graphql.NewObject(graphql.ObjectConfig{})
 | 
			
		||||
 | 
			
		||||
	// 图像的可选字段
 | 
			
		||||
	image := graphql.NewObject(graphql.ObjectConfig{
 | 
			
		||||
		Name: "Image",
 | 
			
		||||
@@ -66,6 +67,7 @@ func NewSchema() (graphql.Schema, error) {
 | 
			
		||||
			"description":             &graphql.Field{Type: graphql.String},
 | 
			
		||||
			"tags":                    &graphql.Field{Type: graphql.String},
 | 
			
		||||
			"rank":                    &graphql.Field{Type: graphql.String},
 | 
			
		||||
			"text":                    &graphql.Field{Type: graphql.String},
 | 
			
		||||
			"comment_num":             &graphql.Field{Type: graphql.Int},
 | 
			
		||||
			"article_category_top_id": &graphql.Field{Type: graphql.Int},
 | 
			
		||||
			"praise_count":            &graphql.Field{Type: graphql.Int},
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,7 @@ type Image struct {
 | 
			
		||||
	Description          string    `json:"description"             db:"description"`
 | 
			
		||||
	Tags                 string    `json:"tags"                    db:"tags"`
 | 
			
		||||
	Rank                 string    `json:"rank"                    db:"rank"`
 | 
			
		||||
	Text                 string    `json:"text"                    db:"text"`
 | 
			
		||||
	CommentNum           int       `json:"comment_num"             db:"comment_num"`
 | 
			
		||||
	ArticleCategoryTopId int       `json:"article_category_top_id" db:"article_category_top_id"`
 | 
			
		||||
	PraiseCount          int       `json:"praise_count"            db:"praise_count"`
 | 
			
		||||
@@ -41,3 +42,15 @@ type Article struct {
 | 
			
		||||
	CreateTime time.Time `json:"create_time" db:"create_time"`
 | 
			
		||||
	UpdateTime time.Time `json:"update_time" db:"update_time"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 输入配置
 | 
			
		||||
type ConfigMysql struct {
 | 
			
		||||
	Host     string
 | 
			
		||||
	Port     int
 | 
			
		||||
	Database string
 | 
			
		||||
	UserName string
 | 
			
		||||
	Password string
 | 
			
		||||
}
 | 
			
		||||
type Config struct {
 | 
			
		||||
	Mysql ConfigMysql
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user