共用配置文件导入

This commit is contained in:
2023-12-03 04:43:53 +08:00
parent 6590fcbb11
commit c7b892e2c0
10 changed files with 70 additions and 158 deletions

View File

@@ -11,22 +11,20 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
var connection *sqlx.DB func NewSchema(config Config) (graphql.Schema, error) {
func init() { // 打开数据库连接
var err error connection, err := sqlx.Connect("mysql", fmt.Sprintf(
user := models.Viper.Get("mysql.user").(string) "%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
password := models.Viper.Get("mysql.password").(string) config.Mysql.UserName,
host := models.Viper.Get("mysql.host").(string) config.Mysql.Password,
port := models.Viper.Get("mysql.port").(int) config.Mysql.Host,
database := models.Viper.Get("mysql.database").(string) config.Mysql.Port,
connection, err = sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database)) config.Mysql.Database,
))
if err != nil { if err != nil {
log.Fatalln("连接数据库失败", err) log.Fatalln("连接数据库失败", err)
} }
}
func NewSchema() (graphql.Schema, error) {
// 文章的可选字段 // 文章的可选字段
article := graphql.NewObject(graphql.ObjectConfig{ article := graphql.NewObject(graphql.ObjectConfig{
@@ -54,6 +52,9 @@ func NewSchema() (graphql.Schema, error) {
}, },
}) })
// 图像中的文字提取
// text := graphql.NewObject(graphql.ObjectConfig{})
// 图像的可选字段 // 图像的可选字段
image := graphql.NewObject(graphql.ObjectConfig{ image := graphql.NewObject(graphql.ObjectConfig{
Name: "Image", Name: "Image",
@@ -66,6 +67,7 @@ func NewSchema() (graphql.Schema, error) {
"description": &graphql.Field{Type: graphql.String}, "description": &graphql.Field{Type: graphql.String},
"tags": &graphql.Field{Type: graphql.String}, "tags": &graphql.Field{Type: graphql.String},
"rank": &graphql.Field{Type: graphql.String}, "rank": &graphql.Field{Type: graphql.String},
"text": &graphql.Field{Type: graphql.String},
"comment_num": &graphql.Field{Type: graphql.Int}, "comment_num": &graphql.Field{Type: graphql.Int},
"article_category_top_id": &graphql.Field{Type: graphql.Int}, "article_category_top_id": &graphql.Field{Type: graphql.Int},
"praise_count": &graphql.Field{Type: graphql.Int}, "praise_count": &graphql.Field{Type: graphql.Int},

View File

@@ -13,6 +13,7 @@ type Image struct {
Description string `json:"description" db:"description"` Description string `json:"description" db:"description"`
Tags string `json:"tags" db:"tags"` Tags string `json:"tags" db:"tags"`
Rank string `json:"rank" db:"rank"` Rank string `json:"rank" db:"rank"`
Text string `json:"text" db:"text"`
CommentNum int `json:"comment_num" db:"comment_num"` CommentNum int `json:"comment_num" db:"comment_num"`
ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"` ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"`
PraiseCount int `json:"praise_count" db:"praise_count"` PraiseCount int `json:"praise_count" db:"praise_count"`
@@ -41,3 +42,15 @@ type Article struct {
CreateTime time.Time `json:"create_time" db:"create_time"` CreateTime time.Time `json:"create_time" db:"create_time"`
UpdateTime time.Time `json:"update_time" db:"update_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
}

View File

@@ -174,6 +174,15 @@ func (image *Image) GetSimilarImagesIdList(collection_name string) (ids []int64)
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) 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() mysqlConnection.Init()
milvusConnection.Init() milvusConnection.Init()
err := milvusConnection.Client.LoadCollection(context.Background(), "default", false) err := milvusConnection.Client.LoadCollection(context.Background(), "default", false)
@@ -183,7 +192,15 @@ func main() {
} }
// Schema // 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 { if err != nil {
log.Fatalf("failed to create new schema, error: %v", err) log.Fatalf("failed to create new schema, error: %v", err)
} }

View File

@@ -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)
}

View File

@@ -1,44 +1,9 @@
package models package models
import ( import "github.com/spf13/viper"
"log"
"path/filepath"
"runtime"
"github.com/spf13/viper" var config *viper.Viper
)
var ( func InitConfig(cfg *viper.Viper) {
Root string config = cfg
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", "")
} }

View File

@@ -13,11 +13,9 @@ import (
func elasticsearch_init() (es *elasticsearch.Client) { func elasticsearch_init() (es *elasticsearch.Client) {
es, err := elasticsearch.NewClient(elasticsearch.Config{ es, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{ Addresses: []string{config.GetString("elasticsearch.host")},
Viper.Get("elasticsearch.host").(string), Username: config.GetString("elasticsearch.user"),
}, Password: config.GetString("elasticsearch.password"),
Username: Viper.Get("elasticsearch.user").(string),
Password: Viper.Get("elasticsearch.password").(string),
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}, },

View File

@@ -18,12 +18,11 @@ func (m *MilvusConnection) GetClient() client.Client {
func (m *MilvusConnection) Init() (err error) { func (m *MilvusConnection) Init() (err error) {
log.Println("Milvus connection init") log.Println("Milvus connection init")
host := Viper.Get("milvus.host").(string) m.Client, err = client.NewGrpcClient(context.Background(), fmt.Sprintf(
port := Viper.Get("milvus.port").(int) "%s:%d",
m.Client, err = client.NewGrpcClient( config.GetString("milvus.host"),
context.Background(), config.GetInt("milvus.port"),
fmt.Sprintf("%s:%d", host, port), ))
)
if err != nil { if err != nil {
log.Println("Milvus connection failed:", err) log.Println("Milvus connection failed:", err)
return return
@@ -31,16 +30,3 @@ func (m *MilvusConnection) Init() (err error) {
log.Println("Milvus connection success") log.Println("Milvus connection success")
return 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
}

View File

@@ -10,38 +10,24 @@ import (
"time" "time"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
) )
var connection *sqlx.DB
var connectionx *sql.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 { type MysqlConnection struct {
Database *sql.DB Database *sql.DB
} }
// 初始化数据库连接 // 初始化数据库连接
func (m *MysqlConnection) Init() (err error) { func (m *MysqlConnection) Init() (err error) {
user := Viper.Get("mysql.user").(string) m.Database, err = sql.Open("mysql", fmt.Sprintf(
password := Viper.Get("mysql.password").(string) "%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
host := Viper.Get("mysql.host").(string) config.GetString("mysql.user"),
port := Viper.Get("mysql.port").(int) config.GetString("mysql.password"),
database := Viper.Get("mysql.database").(string) config.GetString("mysql.host"),
sqlconf := user + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database + "?charset=utf8mb4&parseTime=True&loc=Local" config.GetInt("mysql.port"),
m.Database, err = sql.Open("mysql", sqlconf) // 连接数据库 config.GetString("mysql.database"),
))
if err != nil { if err != nil {
log.Println("连接数据库失败", err) log.Println("连接数据库失败", err)
return return

View File

@@ -8,12 +8,11 @@ import (
) )
func GetBucket(bucketName string) *oss.Bucket { func GetBucket(bucketName string) *oss.Bucket {
// 从config文件中读取配置 client, err := oss.New(
endpoint := Viper.Get("oss.endpoint").(string) config.GetString("oss.endpoint"),
accessID := Viper.Get("oss.accessID").(string) config.GetString("oss.accessID"),
accessKey := Viper.Get("oss.accessKey").(string) config.GetString("oss.accessKey"),
)
client, err := oss.New(endpoint, accessID, accessKey)
if err != nil { if err != nil {
HandleError(err) HandleError(err)
} }

View File

@@ -17,22 +17,3 @@ ssh $host "pm2 reload webp"
# 删除旧文件 # 删除旧文件
ssh $host "rm ~/webp/main_old" 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"