wip: list
This commit is contained in:
@ -27,6 +27,7 @@ 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)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"github.com/loveuer/nf-disk/internal/api"
|
||||
"github.com/loveuer/nf-disk/internal/db"
|
||||
"github.com/loveuer/nf-disk/internal/manager"
|
||||
"github.com/loveuer/nf-disk/internal/model"
|
||||
"github.com/loveuer/nf-disk/internal/tool"
|
||||
"github.com/loveuer/nf-disk/ndh"
|
||||
@ -21,12 +22,17 @@ 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(manager.Init(ctx))
|
||||
tool.Must(api.Init(ctx))
|
||||
}
|
||||
|
||||
func (a *App) Startup(ctx context.Context) {
|
||||
log.Info("app startup!!!")
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/loveuer/nf-disk/internal/db"
|
||||
"github.com/loveuer/nf-disk/internal/manager"
|
||||
"github.com/loveuer/nf-disk/internal/model"
|
||||
"github.com/loveuer/nf-disk/internal/s3"
|
||||
"github.com/loveuer/nf-disk/ndh"
|
||||
"github.com/samber/lo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func ConnectionTest(c *ndh.Ctx) error {
|
||||
@ -78,7 +81,7 @@ func ConnectionCreate(c *ndh.Ctx) error {
|
||||
return c.Send500(err.Error(), "创建连接失败(1)")
|
||||
}
|
||||
|
||||
if err = manager.Register(connection, client); err != nil {
|
||||
if err = manager.Manager.Register(connection, client); err != nil {
|
||||
return c.Send500(err.Error(), "创建连接失败(2)")
|
||||
}
|
||||
|
||||
@ -97,5 +100,52 @@ func ConnectionList(c *ndh.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
listMap := lo.SliceToMap(list, func(item *model.Connection) (uint64, *model.Connection) {
|
||||
return item.Id, item
|
||||
})
|
||||
|
||||
manager.Manager.Map(func(c *model.Connection, s *s3.Client) error {
|
||||
if item, ok := listMap[c.Id]; ok {
|
||||
item.Active = true
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
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(req)
|
||||
}
|
||||
|
||||
if err = db.Default.Session().Take(conn, req.Id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return c.Send400(err.Error(), "连接不存在")
|
||||
}
|
||||
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
if client, err = s3.New(c.Context(), conn.Endpoint, conn.Access, conn.Key); err != nil {
|
||||
return c.Send500(err.Error(), "连接失败")
|
||||
}
|
||||
|
||||
if err = manager.Manager.Register(conn, client); err != nil {
|
||||
return c.Send500(err.Error(), "连接失败")
|
||||
}
|
||||
|
||||
return c.Send200(conn, "连接成功")
|
||||
}
|
||||
|
7
internal/manager/error.go
Normal file
7
internal/manager/error.go
Normal file
@ -0,0 +1,7 @@
|
||||
package manager
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
)
|
@ -5,14 +5,71 @@ import (
|
||||
"github.com/loveuer/nf-disk/internal/model"
|
||||
"github.com/loveuer/nf-disk/internal/s3"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
conn *model.Connection
|
||||
client *s3.Client
|
||||
}
|
||||
|
||||
type manager struct {
|
||||
sync.Mutex
|
||||
clients map[uint64]*client
|
||||
}
|
||||
|
||||
var (
|
||||
Manager *manager
|
||||
)
|
||||
|
||||
func Init(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Register(m *model.Connection, c *s3.Client) error {
|
||||
log.Debug("manager: register connection-client: id = %d, name = %s", m.Id, m.Name)
|
||||
Manager = &manager{
|
||||
clients: make(map[uint64]*client),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *manager) Register(c *model.Connection, s *s3.Client) error {
|
||||
log.Debug("manager: register connection-client: id = %d, name = %s", c.Id, c.Name)
|
||||
|
||||
Manager.Lock()
|
||||
defer Manager.Unlock()
|
||||
Manager.clients[c.Id] = &client{conn: c, client: s}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *manager) UnRegister(id uint64) error {
|
||||
Manager.Lock()
|
||||
defer Manager.Unlock()
|
||||
c, ok := m.clients[id]
|
||||
if !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
log.Debug("manager: register connection-client: id = %d, name = %s", c.conn, c.conn.Name)
|
||||
|
||||
delete(m.clients, id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *manager) Map(fn func(*model.Connection, *s3.Client) error) error {
|
||||
for _, item := range m.clients {
|
||||
if err := fn(item.conn, item.client); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *manager) Use(id uint64) (*model.Connection, *s3.Client, error) {
|
||||
c, ok := m.clients[id]
|
||||
if !ok {
|
||||
return nil, nil, ErrNotFound
|
||||
}
|
||||
|
||||
return c.conn, c.client, nil
|
||||
}
|
||||
|
@ -1,9 +1,35 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
"github.com/loveuer/nf-disk/internal/opt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Init(tx *gorm.DB) error {
|
||||
return tx.AutoMigrate(
|
||||
func Init(tx *gorm.DB) (err error) {
|
||||
if err = tx.AutoMigrate(
|
||||
&Connection{},
|
||||
)
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if opt.Debug {
|
||||
if 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: "",
|
||||
Key: "",
|
||||
},
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ type Connection struct {
|
||||
Endpoint string `json:"endpoint" gorm:"column:endpoint"`
|
||||
Access string `json:"access" gorm:"column:access"`
|
||||
Key string `json:"key" gorm:"column:key"`
|
||||
|
||||
Active bool `json:"active" gorm:"-"`
|
||||
}
|
||||
|
||||
func (c *Connection) Create(tx *gorm.DB) error {
|
||||
|
Reference in New Issue
Block a user