diff --git a/frontend/src/page/home/home.css b/frontend/src/page/home/home.css index cd05775..90cbe1a 100644 --- a/frontend/src/page/home/home.css +++ b/frontend/src/page/home/home.css @@ -48,3 +48,14 @@ div.body-connections-search-dismiss{ justify-content: center; align-items: center; } + +div.body-connections-list-item { + height: 36px; + display: flex; + align-items: center; + font-size: 14px; + margin: 1px 0; +} +div.body-connections-list-item:first-child { + margin-top: 8px; +} diff --git a/frontend/src/page/home/home.tsx b/frontend/src/page/home/home.tsx index 1692641..40ee18c 100644 --- a/frontend/src/page/home/home.tsx +++ b/frontend/src/page/home/home.tsx @@ -26,6 +26,18 @@ function Home() { }) }, []); + async function handleConnect(item: Connection) { + console.log('[DEBUG] double clicked item =', item) + let res = await Dial("/api/connection/connect", {id: item.id}) + if (res.status === 200) { + connectionList.forEach((conn) => { + if (conn.id === item.id) { + conn.active = true + } + }) + } + } + return (
@@ -47,7 +59,18 @@ function Home() {
-
+
+ {connectionList.map(item => { + return
+ +
+ })} +
diff --git a/frontend/wailsjs/go/controller/App.d.ts b/frontend/wailsjs/go/controller/App.d.ts index dfdb059..f04e377 100755 --- a/frontend/wailsjs/go/controller/App.d.ts +++ b/frontend/wailsjs/go/controller/App.d.ts @@ -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; export function Invoke(arg1:string,arg2:string):Promise; diff --git a/frontend/wailsjs/go/controller/App.js b/frontend/wailsjs/go/controller/App.js index 3bb5d47..4358348 100755 --- a/frontend/wailsjs/go/controller/App.js +++ b/frontend/wailsjs/go/controller/App.js @@ -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); } diff --git a/go.mod b/go.mod index 2502815..060f8b7 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/loveuer/go-sqlite3 v1.0.2 github.com/loveuer/nf v0.2.11 github.com/ncruces/go-sqlite3/gormlite v0.18.4 + github.com/pkg/errors v0.9.1 github.com/psanford/httpreadat v0.1.0 github.com/wailsapp/wails/v2 v2.9.2 gorm.io/driver/mysql v1.5.7 @@ -58,7 +59,6 @@ require ( github.com/ncruces/go-sqlite3 v0.18.4 // indirect github.com/ncruces/julianday v1.0.0 // indirect 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 diff --git a/internal/api/api.go b/internal/api/api.go index d519d4f..44fec17 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -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 } diff --git a/internal/controller/app.go b/internal/controller/app.go index e53ebc4..ca77cad 100644 --- a/internal/controller/app.go +++ b/internal/controller/app.go @@ -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!!!") +} diff --git a/internal/handler/connection.go b/internal/handler/connection.go index 92ca3d9..90b0001 100644 --- a/internal/handler/connection.go +++ b/internal/handler/connection.go @@ -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") +} diff --git a/internal/model/init.go b/internal/model/init.go index 43cd790..b7cf07c 100644 --- a/internal/model/init.go +++ b/internal/model/init.go @@ -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 } diff --git a/internal/opt/var.go b/internal/opt/var.go index df1d844..6b17413 100644 --- a/internal/opt/var.go +++ b/internal/opt/var.go @@ -7,5 +7,5 @@ const ( ) var ( - Debug bool + Debug bool = false ) diff --git a/main.go b/main.go index 5d25765..14db991 100644 --- a/main.go +++ b/main.go @@ -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" @@ -15,18 +18,21 @@ import ( //go:embed all:frontend/dist 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,