diff --git a/api/graphql.go b/api/graphql.go index 849f632..ddd91e3 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -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}, diff --git a/api/struct.go b/api/struct.go index cef1422..bcb1d0a 100644 --- a/api/struct.go +++ b/api/struct.go @@ -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 +} diff --git a/bin/main.go b/bin/main.go index 128e64f..e4149da 100644 --- a/bin/main.go +++ b/bin/main.go @@ -174,6 +174,15 @@ func (image *Image) GetSimilarImagesIdList(collection_name string) (ids []int64) func main() { runtime.GOMAXPROCS(runtime.NumCPU()) + + viper.SetConfigFile("./data/config.yaml") + if err := viper.ReadInConfig(); err != nil { + log.Println("读取配置文件失败", err) + } + config := viper.GetViper() + + models.InitConfig(config) + mysqlConnection.Init() milvusConnection.Init() err := milvusConnection.Client.LoadCollection(context.Background(), "default", false) @@ -183,7 +192,15 @@ func main() { } // Schema - schema, err := api.NewSchema() + schema, err := api.NewSchema(api.Config{ + Mysql: api.ConfigMysql{ + Host: config.GetString("mysql.host"), + Port: config.GetInt("mysql.port"), + Database: config.GetString("mysql.database"), + UserName: config.GetString("mysql.user"), + Password: config.GetString("mysql.password"), + }, + }) if err != nil { log.Fatalf("failed to create new schema, error: %v", err) } diff --git a/demo/main.go b/demo/main.go deleted file mode 100644 index 3fd336b..0000000 --- a/demo/main.go +++ /dev/null @@ -1,35 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "log" - "net/http" - "runtime" - - "git.satori.love/gameui/webp/api" - "github.com/graphql-go/graphql" -) - -func main() { - runtime.GOMAXPROCS(runtime.NumCPU()) - - schema, err := api.NewSchema() - if err != nil { - log.Fatalln("创建Schema失败", err) - } - http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) { - query := r.URL.Query().Get("query") - params := graphql.Params{Schema: schema, RequestString: query} - result := graphql.Do(params) - if len(result.Errors) > 0 { - fmt.Printf("failed to execute graphql operation, errors: %+v", result.Errors) - http.Error(w, result.Errors[0].Error(), 500) - return - } - rJSON, _ := json.MarshalIndent(result.Data, "", " ") - w.Write(rJSON) - }) - fmt.Println("Now server is running on port 6001") - http.ListenAndServe(":6001", nil) -} diff --git a/models/config.go b/models/config.go index a7d5335..91c788a 100644 --- a/models/config.go +++ b/models/config.go @@ -1,44 +1,9 @@ package models -import ( - "log" - "path/filepath" - "runtime" +import "github.com/spf13/viper" - "github.com/spf13/viper" -) +var config *viper.Viper -var ( - Root string - Viper *viper.Viper -) - -func init() { - _, b, _, _ := runtime.Caller(0) - Root = filepath.Join(filepath.Dir(b), "..") - config_file := filepath.Join(Root, "data", "config.yaml") - viper.SetConfigFile(config_file) - if err := viper.ReadInConfig(); err != nil { - log.Println("读取配置文件失败", err) - 生成配置文件() - } - Viper = viper.GetViper() -} - -func 生成配置文件() { - viper.Set("mysql.host", "") - viper.Set("mysql.port", 3306) - viper.Set("mysql.user", "") - viper.Set("mysql.password", "") - viper.Set("mysql.dbname", "") - viper.Set("mysql.charset", "utf8mb4") - viper.Set("mysql.maxOpenConns", 100) - - viper.Set("oss.endpoint", "") - viper.Set("oss.accessID", "") - viper.Set("oss.accessKey", "") - - viper.Set("video.endpoint", "") - viper.Set("video.accessKeyID", "") - viper.Set("video.accessKey", "") +func InitConfig(cfg *viper.Viper) { + config = cfg } diff --git a/models/elasticsearch.go b/models/elasticsearch.go index 5d7ff77..2f1166b 100644 --- a/models/elasticsearch.go +++ b/models/elasticsearch.go @@ -13,11 +13,9 @@ import ( func elasticsearch_init() (es *elasticsearch.Client) { es, err := elasticsearch.NewClient(elasticsearch.Config{ - Addresses: []string{ - Viper.Get("elasticsearch.host").(string), - }, - Username: Viper.Get("elasticsearch.user").(string), - Password: Viper.Get("elasticsearch.password").(string), + Addresses: []string{config.GetString("elasticsearch.host")}, + Username: config.GetString("elasticsearch.user"), + Password: config.GetString("elasticsearch.password"), Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }, diff --git a/models/milvus.go b/models/milvus.go index 4f3a782..11802b0 100644 --- a/models/milvus.go +++ b/models/milvus.go @@ -18,12 +18,11 @@ func (m *MilvusConnection) GetClient() client.Client { func (m *MilvusConnection) Init() (err error) { log.Println("Milvus connection init") - host := Viper.Get("milvus.host").(string) - port := Viper.Get("milvus.port").(int) - m.Client, err = client.NewGrpcClient( - context.Background(), - fmt.Sprintf("%s:%d", host, port), - ) + m.Client, err = client.NewGrpcClient(context.Background(), fmt.Sprintf( + "%s:%d", + config.GetString("milvus.host"), + config.GetInt("milvus.port"), + )) if err != nil { log.Println("Milvus connection failed:", err) return @@ -31,16 +30,3 @@ func (m *MilvusConnection) Init() (err error) { log.Println("Milvus connection success") return } - -func (m *MilvusConnection) GetCollection(collection_name string) (collection *client.Client, err error) { - if m.Client == nil { - m.Init() - } - err = m.Client.LoadCollection(context.Background(), collection_name, false) - if err != nil { - log.Println("Milvus load collection failed:", err) - return - } - collection = &m.Client - return -} diff --git a/models/mysql.go b/models/mysql.go index 524092c..cd2751d 100644 --- a/models/mysql.go +++ b/models/mysql.go @@ -10,38 +10,24 @@ import ( "time" _ "github.com/go-sql-driver/mysql" - "github.com/jmoiron/sqlx" ) -var connection *sqlx.DB var connectionx *sql.DB -func init() { - var err error - user := Viper.Get("mysql.user").(string) - password := Viper.Get("mysql.password").(string) - host := Viper.Get("mysql.host").(string) - port := Viper.Get("mysql.port").(int) - database := 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)) - if err != nil { - log.Fatalln("连接数据库失败", err) - } -} - type MysqlConnection struct { Database *sql.DB } // 初始化数据库连接 func (m *MysqlConnection) Init() (err error) { - user := Viper.Get("mysql.user").(string) - password := Viper.Get("mysql.password").(string) - host := Viper.Get("mysql.host").(string) - port := Viper.Get("mysql.port").(int) - database := Viper.Get("mysql.database").(string) - sqlconf := user + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database + "?charset=utf8mb4&parseTime=True&loc=Local" - m.Database, err = sql.Open("mysql", sqlconf) // 连接数据库 + m.Database, err = sql.Open("mysql", fmt.Sprintf( + "%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", + config.GetString("mysql.user"), + config.GetString("mysql.password"), + config.GetString("mysql.host"), + config.GetInt("mysql.port"), + config.GetString("mysql.database"), + )) if err != nil { log.Println("连接数据库失败", err) return diff --git a/models/oss.go b/models/oss.go index e831706..14da54f 100644 --- a/models/oss.go +++ b/models/oss.go @@ -8,12 +8,11 @@ import ( ) func GetBucket(bucketName string) *oss.Bucket { - // 从config文件中读取配置 - endpoint := Viper.Get("oss.endpoint").(string) - accessID := Viper.Get("oss.accessID").(string) - accessKey := Viper.Get("oss.accessKey").(string) - - client, err := oss.New(endpoint, accessID, accessKey) + client, err := oss.New( + config.GetString("oss.endpoint"), + config.GetString("oss.accessID"), + config.GetString("oss.accessKey"), + ) if err != nil { HandleError(err) } diff --git a/update.sh b/update.sh index 8bcc962..053f743 100755 --- a/update.sh +++ b/update.sh @@ -17,22 +17,3 @@ ssh $host "pm2 reload webp" # 删除旧文件 ssh $host "rm ~/webp/main_old" - - -# pm2 start /root/webp/main --name webp --watch - -# 查看 main_ 的進程, 然後使用 kill -9 强制关闭 -# ssh $host "ps -ef | grep -v grep | grep ./main" -# ssh $host "kill -9 \`ps -ef | grep -v grep | grep ./main | awk '{print \$2}'\`" - -# 重启服务, 保持服务不被关闭 (pkill ./main; 無效, 還需防止 nohup 不退出) -#ssh $host "rm ./main; mv ./main_new ./main;" -#ssh $host "nohup ./main test >> nohup.out 2>&1 &" -#ssh $host "nohup ./main server >> nohup.out 2>&1 &" -#ssh $host "ps -ef | grep -v grep | grep ./main" - -# 查看日志 -# ssh root@47.103.40.152 "tail -fn 50 nohup.out" - -# 查看所有進程, 排除grep, 只看本用戶, 排除進程名帶方括號的, 排除lib, 排除bash, 排除sshd, 按照進程名排序, 對 main 高亮爲紅色 -#ssh root@47.103.40.152 "ps -ef | grep -v grep | grep \$USER | grep -v '\[' | grep -v /lib | grep -v bash | grep -v sshd | grep -v /sbin | sort -k 8"