112 lines
2.4 KiB
Go
112 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/schema"
|
|
"log/slog"
|
|
"platform/pkg/env"
|
|
"platform/pkg/logs"
|
|
client2 "platform/web/domains/client"
|
|
proxy2 "platform/web/domains/proxy"
|
|
m "platform/web/models"
|
|
q "platform/web/queries"
|
|
)
|
|
|
|
func main() {
|
|
|
|
env.Init()
|
|
logs.Init()
|
|
|
|
// 初始化数据库连接
|
|
dsn := fmt.Sprintf(
|
|
"host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
|
|
env.DbHost, env.DbUserName, env.DbPassword, env.DbName, env.DbPort,
|
|
)
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
NamingStrategy: schema.NamingStrategy{
|
|
SingularTable: true,
|
|
},
|
|
})
|
|
if err != nil {
|
|
slog.Error("gorm 初始化数据库失败:", slog.Any("err", err))
|
|
panic(err)
|
|
}
|
|
|
|
q.SetDefault(db)
|
|
|
|
// 填充数据
|
|
err = q.Q.Transaction(func(tx *q.Query) (err error) {
|
|
|
|
// 代理
|
|
err = q.Proxy.
|
|
Select(q.Proxy.Version, q.Proxy.Name, q.Proxy.Host, q.Proxy.Type, q.Proxy.Secret).
|
|
Create(&m.Proxy{
|
|
Version: 1,
|
|
Name: "7a17e8b4-cdc3-4500-bf16-4a665991a7f6",
|
|
Host: "110.40.82.248",
|
|
Type: int32(proxy2.TypeSelfHosted),
|
|
Secret: "api:123456",
|
|
}, &m.Proxy{
|
|
Version: 1,
|
|
Name: "58e03f38-4cef-429c-8bb8-530142d0a745",
|
|
Host: "123.6.147.241",
|
|
Type: int32(proxy2.TypeThirdParty),
|
|
Secret: "api:123456",
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 客户端
|
|
testSecret, err := bcrypt.GenerateFromPassword([]byte("test"), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tasksSecret, err := bcrypt.GenerateFromPassword([]byte("tasks"), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = q.Client.
|
|
Select(
|
|
q.Client.ClientID,
|
|
q.Client.ClientSecret,
|
|
q.Client.GrantClient,
|
|
q.Client.GrantRefresh,
|
|
q.Client.GrantPassword,
|
|
q.Client.Spec,
|
|
q.Client.Name,
|
|
).
|
|
Create(&m.Client{
|
|
ClientID: "test",
|
|
ClientSecret: string(testSecret),
|
|
GrantCode: true,
|
|
GrantClient: true,
|
|
GrantRefresh: true,
|
|
GrantPassword: true,
|
|
Spec: int32(client2.SpecTrusted),
|
|
Name: "默认客户端",
|
|
}, &m.Client{
|
|
ClientID: "tasks",
|
|
ClientSecret: string(tasksSecret),
|
|
GrantClient: true,
|
|
Spec: int32(client2.SpecTrusted),
|
|
Name: "异步任务处理服务",
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
slog.Info("✔ Data inserted successfully")
|
|
}
|