refactor: config package
This commit is contained in:
@@ -3,6 +3,7 @@ TODO
|
||||
- [x] 剪辑板传输
|
||||
- [x] 文件夹传输
|
||||
- [x] 多样化图标
|
||||
- [ ] 加密传输
|
||||
- [x] 取消传输
|
||||
- [x] 多文件发送
|
||||
- [ ] 加密传输
|
||||
- [ ] 设置页面:默认保存路径
|
||||
@@ -10,8 +10,6 @@ import {
|
||||
NSpace,
|
||||
NText,
|
||||
NEmpty,
|
||||
NGrid,
|
||||
NGi,
|
||||
NMenu,
|
||||
NBadge,
|
||||
NButton,
|
||||
@@ -176,15 +174,15 @@ const handleMenuUpdate = (key: string) => {
|
||||
<n-layout-content class="content">
|
||||
<div class="content-container">
|
||||
<!-- 发现页视图 -->
|
||||
<div v-if="activeKey === 'discover'">
|
||||
<div v-show="activeKey === 'discover'">
|
||||
<n-space vertical size="large" v-if="peers.length > 0">
|
||||
<n-grid x-gap="16" y-gap="16" cols="1 500:2 700:3">
|
||||
<n-gi v-for="peer in peers" :key="peer.id">
|
||||
<div class="peer-grid">
|
||||
<div v-for="peer in peers" :key="peer.id">
|
||||
<PeerCard
|
||||
:peer="peer"
|
||||
@transferStarted="activeKey = 'transfers'" />
|
||||
</n-gi>
|
||||
</n-grid>
|
||||
</div>
|
||||
</div>
|
||||
</n-space>
|
||||
|
||||
<div v-else class="empty-state">
|
||||
@@ -199,7 +197,7 @@ const handleMenuUpdate = (key: string) => {
|
||||
</div>
|
||||
|
||||
<!-- 传输列表视图 -->
|
||||
<div v-else-if="activeKey === 'transfers'">
|
||||
<div v-show="activeKey === 'transfers'">
|
||||
<div v-if="transferList.length > 0">
|
||||
<TransferItem
|
||||
v-for="transfer in transferList"
|
||||
@@ -260,4 +258,22 @@ const handleMenuUpdate = (key: string) => {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
.peer-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(1, minmax(0, 1fr));
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
@media (min-width: 500px) {
|
||||
.peer-grid {
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: 700px) {
|
||||
.peer-grid {
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -315,7 +315,9 @@ const handleSendFiles = () => {
|
||||
</template>
|
||||
</n-card>
|
||||
|
||||
<!-- 文件发送 Modal -->
|
||||
<n-modal
|
||||
:mask-closable="false"
|
||||
v-model:show="showFileModal"
|
||||
preset="card"
|
||||
title="Send Files"
|
||||
@@ -375,6 +377,7 @@ const handleSendFiles = () => {
|
||||
|
||||
<!-- 文本发送 Modal -->
|
||||
<n-modal
|
||||
:mask-closable="false"
|
||||
v-model:show="showTextModal"
|
||||
preset="card"
|
||||
title="Send Text"
|
||||
|
||||
10
go.mod
10
go.mod
@@ -22,6 +22,7 @@ require (
|
||||
github.com/cyphar/filepath-securejoin v0.6.1 // indirect
|
||||
github.com/ebitengine/purego v0.9.1 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/sse v1.1.0 // indirect
|
||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
||||
@@ -31,6 +32,7 @@ require (
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.27.0 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/goccy/go-yaml v1.18.0 // indirect
|
||||
github.com/godbus/dbus/v5 v5.2.2 // indirect
|
||||
@@ -54,14 +56,22 @@ require (
|
||||
github.com/quic-go/qpack v0.5.1 // indirect
|
||||
github.com/quic-go/quic-go v0.54.0 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/samber/lo v1.52.0 // indirect
|
||||
github.com/sergi/go-diff v1.4.0 // indirect
|
||||
github.com/skeema/knownhosts v1.3.2 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/spf13/viper v1.21.0 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.3.0 // indirect
|
||||
github.com/wailsapp/go-webview2 v1.0.23 // indirect
|
||||
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
||||
go.uber.org/mock v0.5.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||
golang.org/x/arch v0.20.0 // indirect
|
||||
golang.org/x/crypto v0.47.0 // indirect
|
||||
golang.org/x/mod v0.32.0 // indirect
|
||||
|
||||
20
go.sum
20
go.sum
@@ -34,6 +34,8 @@ github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o
|
||||
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
|
||||
@@ -62,6 +64,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
|
||||
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||
@@ -132,6 +136,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
|
||||
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
|
||||
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
|
||||
@@ -139,6 +145,16 @@ github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepq
|
||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/skeema/knownhosts v1.3.2 h1:EDL9mgf4NzwMXCTfaxSD/o/a5fxDw/xL9nkU28JjdBg=
|
||||
github.com/skeema/knownhosts v1.3.2/go.mod h1:bEg3iQAuw+jyiw+484wwFJoKSLwcfd7fqRy+N0QTiow=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
|
||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
||||
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
||||
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
|
||||
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
@@ -150,6 +166,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
|
||||
@@ -162,6 +180,8 @@ github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM
|
||||
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
|
||||
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
||||
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
|
||||
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
|
||||
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
|
||||
116
internal/config/config.go
Normal file
116
internal/config/config.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// WindowState 定义窗口状态
|
||||
type WindowState struct {
|
||||
Width int `mapstructure:"width"`
|
||||
Height int `mapstructure:"height"`
|
||||
X int `mapstructure:"x"`
|
||||
Y int `mapstructure:"y"`
|
||||
Maximised bool `mapstructure:"maximised"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
mu sync.RWMutex
|
||||
WindowState WindowState `mapstructure:"window_state"`
|
||||
SavePath string `mapstructure:"save_path"`
|
||||
}
|
||||
|
||||
// 默认窗口配置
|
||||
var defaultWindowState = WindowState{
|
||||
Width: 1024,
|
||||
Height: 768,
|
||||
X: -1,
|
||||
Y: -1,
|
||||
}
|
||||
|
||||
func getConfigDir() string {
|
||||
configPath, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
configPath = "/tmp"
|
||||
}
|
||||
return filepath.Join(configPath, "mesh-drop")
|
||||
}
|
||||
|
||||
func getUserHomeDir() string {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "/tmp"
|
||||
}
|
||||
return home
|
||||
}
|
||||
|
||||
// New 读取配置
|
||||
func Load() *Config {
|
||||
configDir := getConfigDir()
|
||||
err := os.MkdirAll(configDir, 0755)
|
||||
if err != nil {
|
||||
slog.Error("Failed to create config directory", "error", err)
|
||||
}
|
||||
configFile := filepath.Join(configDir, "config.json")
|
||||
|
||||
// 设置默认值
|
||||
defaultSavePath := filepath.Join(getUserHomeDir(), "Downloads")
|
||||
viper.SetDefault("window_state", defaultWindowState)
|
||||
viper.SetDefault("save_path", defaultSavePath)
|
||||
|
||||
viper.SetConfigFile(configFile)
|
||||
viper.SetConfigType("json")
|
||||
|
||||
// 尝试读取配置
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
slog.Info("Config file not found, using defaults")
|
||||
} else {
|
||||
slog.Warn("Failed to read config file, using defaults", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
var config Config
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
slog.Error("Failed to unmarshal config", "error", err)
|
||||
}
|
||||
|
||||
return &config
|
||||
}
|
||||
|
||||
// Save 保存配置到磁盘
|
||||
func (c *Config) Save() error {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
configDir := getConfigDir()
|
||||
if err := os.MkdirAll(configDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
slog.Error("Failed to write config", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetSavePath 修改配置
|
||||
func (c *Config) SetSavePath(savePath string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.SavePath = savePath
|
||||
viper.Set("save_path", savePath)
|
||||
}
|
||||
|
||||
func (c *Config) GetSavePath() string {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.SavePath
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// WindowState 定义窗口状态
|
||||
type WindowState struct {
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
Maximised bool `json:"maximised"`
|
||||
}
|
||||
|
||||
// 默认窗口配置
|
||||
var DefaultWindowState = WindowState{
|
||||
Width: 1024,
|
||||
Height: 768,
|
||||
X: -1, // -1 表示让系统自动决定位置
|
||||
Y: -1,
|
||||
}
|
||||
|
||||
// GetConfigPath 获取配置文件路径
|
||||
func GetConfigPath() string {
|
||||
configDir, _ := os.UserConfigDir()
|
||||
appDir := filepath.Join(configDir, "mesh-drop")
|
||||
_ = os.MkdirAll(appDir, 0755)
|
||||
return filepath.Join(appDir, "window.json")
|
||||
}
|
||||
|
||||
// LoadWindowState 读取配置
|
||||
func LoadWindowState() WindowState {
|
||||
path := GetConfigPath()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return DefaultWindowState
|
||||
}
|
||||
|
||||
var state WindowState
|
||||
if err := json.Unmarshal(data, &state); err != nil {
|
||||
return DefaultWindowState
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
// SaveWindowState 保存配置
|
||||
func SaveWindowState(state WindowState) error {
|
||||
path := GetConfigPath()
|
||||
data, _ := json.MarshalIndent(state, "", " ")
|
||||
return os.WriteFile(path, data, 0644)
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func (s *Service) handleUpload(c *gin.Context) {
|
||||
|
||||
savePath := task.SavePath
|
||||
if savePath == "" {
|
||||
savePath = s.savePath
|
||||
savePath = s.config.GetSavePath()
|
||||
}
|
||||
|
||||
ctxReader := &ContextReader{
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"mesh-drop/internal/config"
|
||||
"mesh-drop/internal/discovery"
|
||||
"sync"
|
||||
|
||||
@@ -12,9 +13,9 @@ import (
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
app *application.App
|
||||
port int
|
||||
savePath string // 默认下载目录
|
||||
config *config.Config
|
||||
app *application.App
|
||||
port int
|
||||
|
||||
// pendingRequests 存储等待用户确认的通道
|
||||
// Key: TransferID, Value: *Transfer
|
||||
@@ -27,14 +28,14 @@ type Service struct {
|
||||
cancelMap sync.Map
|
||||
}
|
||||
|
||||
func NewService(app *application.App, port int, defaultSavePath string, discoveryService *discovery.Service) *Service {
|
||||
func NewService(config *config.Config, app *application.App, port int, discoveryService *discovery.Service) *Service {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
return &Service{
|
||||
app: app,
|
||||
port: port,
|
||||
savePath: defaultSavePath,
|
||||
discoveryService: discoveryService,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
44
main.go
44
main.go
@@ -7,7 +7,6 @@ import (
|
||||
"mesh-drop/internal/discovery"
|
||||
"mesh-drop/internal/transfer"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"github.com/wailsapp/wails/v3/pkg/events"
|
||||
@@ -22,7 +21,7 @@ type FilesDroppedEvent struct {
|
||||
}
|
||||
|
||||
func main() {
|
||||
state := config.LoadWindowState()
|
||||
conf := config.Load()
|
||||
|
||||
app := application.New(application.Options{
|
||||
Name: "mesh-drop",
|
||||
@@ -31,19 +30,10 @@ func main() {
|
||||
},
|
||||
})
|
||||
|
||||
// 获取用户目录
|
||||
userHomePath, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
userHomePath = "/tmp/mesh-drop"
|
||||
}
|
||||
|
||||
// 设置默认保存路径
|
||||
defaultSavePath := filepath.Join(userHomePath, "Downloads")
|
||||
|
||||
// 创建保存路径
|
||||
err = os.MkdirAll(defaultSavePath, 0755)
|
||||
err := os.MkdirAll(conf.SavePath, 0755)
|
||||
if err != nil {
|
||||
slog.Error("Failed to create save path", "path", defaultSavePath, "error", err)
|
||||
slog.Error("Failed to create save path", "path", conf.SavePath, "error", err)
|
||||
}
|
||||
|
||||
// 文件传输端口
|
||||
@@ -55,24 +45,25 @@ func main() {
|
||||
discoveryService.Start()
|
||||
|
||||
// 初始化传输服务
|
||||
transferService := transfer.NewService(app, port, defaultSavePath, discoveryService)
|
||||
transferService := transfer.NewService(conf, app, port, discoveryService)
|
||||
transferService.Start()
|
||||
|
||||
slog.Info("Backend Service Started", "discovery_port", discovery.DiscoveryPort, "transfer_port", port)
|
||||
|
||||
app.RegisterService(application.NewService(discoveryService))
|
||||
app.RegisterService(application.NewService(transferService))
|
||||
app.RegisterService(application.NewService(conf))
|
||||
|
||||
windows := app.Window.NewWithOptions(application.WebviewWindowOptions{
|
||||
window := app.Window.NewWithOptions(application.WebviewWindowOptions{
|
||||
Title: "mesh drop",
|
||||
Width: state.Width,
|
||||
Height: state.Height,
|
||||
X: state.X,
|
||||
Y: state.Y,
|
||||
Width: conf.WindowState.Width,
|
||||
Height: conf.WindowState.Height,
|
||||
X: conf.WindowState.X,
|
||||
Y: conf.WindowState.Y,
|
||||
EnableFileDrop: true,
|
||||
})
|
||||
|
||||
windows.OnWindowEvent(events.Common.WindowFilesDropped, func(event *application.WindowEvent) {
|
||||
window.OnWindowEvent(events.Common.WindowFilesDropped, func(event *application.WindowEvent) {
|
||||
files := event.Context().DroppedFiles()
|
||||
details := event.Context().DropTargetDetails()
|
||||
app.Event.Emit("files-dropped", FilesDroppedEvent{
|
||||
@@ -81,9 +72,20 @@ func main() {
|
||||
})
|
||||
})
|
||||
|
||||
window.OnWindowEvent(events.Common.WindowClosing, func(event *application.WindowEvent) {
|
||||
x, y := window.Position()
|
||||
width, height := window.Size()
|
||||
conf.WindowState = config.WindowState{
|
||||
X: x,
|
||||
Y: y,
|
||||
Width: width,
|
||||
Height: height,
|
||||
}
|
||||
_ = conf.Save()
|
||||
})
|
||||
|
||||
application.RegisterEvent[FilesDroppedEvent]("files-dropped")
|
||||
|
||||
// Initialize structured logging
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelDebug,
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user