wip: conn list

This commit is contained in:
zhaoyupeng
2024-09-30 09:16:11 +08:00
parent b2c13508f4
commit bea30130f1
11 changed files with 155 additions and 15 deletions

View File

@ -27,6 +27,8 @@ func Init(ctx context.Context) error {
register("/api/connection/test", handler.ConnectionTest)
register("/api/connection/create", handler.ConnectionCreate)
register("/api/connection/list", handler.ConnectionList)
register("/api/connection/connect", handler.ConnectionConnect)
register("/api/connection/buckets", handler.ConnectionBuckets)
return nil
}

View File

@ -21,12 +21,14 @@ func NewApp() *App {
}
}
func (a *App) Startup(ctx context.Context) {
log.Info("app startup!!!")
func (a *App) Init(ctx context.Context) {
log.Info("app init!!!")
a.ctx = ctx
tool.Must(db.Init(ctx, "sqlite::memory", db.OptSqliteByMem(nil)))
tool.Must(model.Init(db.Default.Session()))
tool.Must(api.Init(ctx))
}
func (a *App) Startup(ctx context.Context) {
log.Info("app startup!!!")
}

View File

@ -6,6 +6,8 @@ import (
"github.com/loveuer/nf-disk/internal/model"
"github.com/loveuer/nf-disk/internal/s3"
"github.com/loveuer/nf-disk/ndh"
"github.com/pkg/errors"
"gorm.io/gorm"
)
func ConnectionTest(c *ndh.Ctx) error {
@ -86,11 +88,20 @@ func ConnectionCreate(c *ndh.Ctx) error {
}
func ConnectionList(c *ndh.Ctx) error {
type Req struct {
Keyword string `json:"keyword"`
}
var (
err error
list = make([]*model.Connection, 0)
req = new(Req)
)
if err = c.ReqParse(req); err != nil {
return c.Send400(nil, "参数错误")
}
if err = db.Default.Session().Model(&model.Connection{}).
Find(&list).
Error; err != nil {
@ -99,3 +110,56 @@ func ConnectionList(c *ndh.Ctx) error {
return c.Send200(map[string]any{"list": list})
}
func ConnectionConnect(c *ndh.Ctx) error {
type Req struct {
Id uint64 `json:"id"`
}
var (
err error
req = new(Req)
conn = new(model.Connection)
client *s3.Client
)
if err = c.ReqParse(req); err != nil {
return c.Send400(nil, "参数错误")
}
if err = db.Default.Session().Take(conn, req.Id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.Send400(c, "不存在的连接")
}
return c.Send500(nil)
}
if client, err = s3.New(c.Context(), conn.Endpoint, conn.Access, conn.Key); err != nil {
return c.Send500(err.Error())
}
if err = manager.Register(conn, client); err != nil {
return c.Send500(err.Error())
}
return c.Send200(conn)
}
func ConnectionBuckets(c *ndh.Ctx) error {
type Req struct {
Id uint64 `json:"id"`
Keyword string `json:"keyword"`
}
var (
err error
req = new(Req)
)
if err = c.ReqParse(req); err != nil {
return c.Send400(nil, "参数错误")
}
panic("implement me: ConnectionBuckets")
}

View File

@ -1,9 +1,34 @@
package model
import "gorm.io/gorm"
import (
"github.com/loveuer/nf-disk/internal/opt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func Init(tx *gorm.DB) error {
return tx.AutoMigrate(
func Init(tx *gorm.DB) (err error) {
err = tx.AutoMigrate(
&Connection{},
)
if opt.Debug {
err = tx.Create([]*Connection{
{
Name: "dev-minio",
Endpoint: "http://10.220.10.15:9000",
Access: "8ALV3DUZI31YG4BDRJ0Z",
Key: "CRqwS1MsiUj27TbRK+3T2n+LpKWd07VvaDKuzU0H",
},
{
Name: "test",
Endpoint: "http://10.220.10.14:19000",
Access: "5VCR05L4BSGNCTCD8DXP",
Key: "FPTMYBEiHhWLJ05C3aGXW8bjFXXNmghc8Za3Fo2u",
},
}).Clauses(clause.OnConflict{
DoNothing: true,
}).Error
}
return
}

View File

@ -7,5 +7,5 @@ const (
)
var (
Debug bool
Debug bool = false
)