wip: list

This commit is contained in:
loveuer 2024-09-29 22:15:28 +08:00
parent b2c13508f4
commit 5e0885f22d
13 changed files with 241 additions and 19 deletions

View File

@ -37,7 +37,8 @@ input.body-connections-search-input {
outline: none;
text-indent: 5px;
}
div.body-connections-search-dismiss{
div.body-connections-search-dismiss {
border: none;
background: none;
outline: none;
@ -48,3 +49,35 @@ div.body-connections-search-dismiss{
justify-content: center;
align-items: center;
}
div.body-connections-list-item {
height: 36px;
}
div.body-connections-list-item.active {
background: aquamarine;
}
div.body-connections-list-item:first-child {
margin-top: 2px;
}
button.body-connections-list-item-button {
background: none;
border: none;
height: 100%;
width: 100%;
padding: 0 6px;
display: flex;
justify-content: left;
align-items: center;
cursor: pointer;
font-size: 14px;
}
button.body-connections-list-item-button:hover {
background-color: var(--colorBrandBackground);
color: rgba(245, 245, 245, 1);
}

View File

@ -26,6 +26,23 @@ function Home() {
})
}, []);
async function handleConnect(item: Connection) {
console.log('[DEBUG] db click item =', item)
for (const c of connectionList) {
if(c.active) {
return
}
}
let res = await Dial("/api/connection/connect", {id: item.id})
if (res.status === 200) {
dispatchMessage("连接成功", "success")
for (const c of connectionList) {
c.active = c.id === item.id;
}
}
}
return (
<div className="container">
<div className="header">
@ -47,7 +64,14 @@ function Home() {
<DismissRegular/>
</div>
</div>
<div className="body-connections-list"></div>
<div className="body-connections-list">
{connectionList.map(item => {
return <div key={item.id} className={item.active?'body-connections-list-item active':'body-connections-list-item'}>
<button onDoubleClick={() => handleConnect(item)}
className='body-connections-list-item-button'>{item.name}</button>
</div>
})}
</div>
</div>
<div className="body-content"></div>
</div>

View File

@ -1,4 +1,7 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {context} from '../models';
export function Init(arg1:context.Context):Promise<void>;
export function Invoke(arg1:string,arg2:string):Promise<string>;

View File

@ -2,6 +2,10 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function Init(arg1) {
return window['go']['controller']['App']['Init'](arg1);
}
export function Invoke(arg1, arg2) {
return window['go']['controller']['App']['Invoke'](arg1, arg2);
}

2
go.mod
View File

@ -14,6 +14,7 @@ require (
github.com/loveuer/nf v0.2.11
github.com/ncruces/go-sqlite3/gormlite v0.18.4
github.com/psanford/httpreadat v0.1.0
github.com/samber/lo v1.38.1
github.com/wailsapp/wails/v2 v2.9.2
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9
@ -60,7 +61,6 @@ require (
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/tetratelabs/wazero v1.8.0 // indirect
github.com/tkrajina/go-reflector v0.5.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect

View File

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

View File

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

View File

@ -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, "连接成功")
}

View File

@ -0,0 +1,7 @@
package manager
import "errors"
var (
ErrNotFound = errors.New("not found")
)

View File

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

View File

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

View File

@ -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 {

17
main.go
View File

@ -1,10 +1,13 @@
package main
import (
"context"
"embed"
"flag"
"github.com/loveuer/nf-disk/internal/controller"
"github.com/loveuer/nf/nft/nfctl/opt"
"github.com/loveuer/nf-disk/internal/opt"
"os/signal"
"syscall"
"github.com/loveuer/nf/nft/log"
"github.com/wailsapp/wails/v2"
@ -16,17 +19,23 @@ import (
var assets embed.FS
func init() {
flag.BoolVar(&opt.Debug, "debug", false, "debug mode")
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
flag.BoolVar(&opt.Debug, "debug", true, "debug mode")
flag.Parse()
if opt.Debug {
log.SetLogLevel(log.LogLevelDebug)
}
}
func main() {
app := controller.NewApp()
app.Init(ctx)
if err := wails.Run(&options.App{
Title: "nf-disk",
Width: 1024,