更新若干功能
This commit is contained in:
51
sync.go
51
sync.go
@@ -22,33 +22,48 @@ import (
|
||||
|
||||
const syncTimeout = 10 * time.Second
|
||||
|
||||
// 内置默认连接:本地 MySQL(root/root)的 code_count 库。
|
||||
// 用户零配置即可登录同步;meta 中存过自定义配置时仍以 meta 为准。
|
||||
const (
|
||||
defaultSyncHost = "127.0.0.1"
|
||||
defaultSyncUser = "root"
|
||||
defaultSyncPassword = "root"
|
||||
defaultSyncDatabase = "code_count"
|
||||
)
|
||||
|
||||
// ---------- 配置与状态 ----------
|
||||
|
||||
// syncConfig 解析同步服务器连接。
|
||||
// 启动时会把打包配置写入本地 SQLite meta(见 applyPackagedSyncConfig);
|
||||
// 运行期以 meta 为准,meta 为空时回退到打包嵌入配置。
|
||||
func (a *App) syncConfig() SyncConfig {
|
||||
port, _ := strconv.Atoi(a.store.Meta("sync_port"))
|
||||
if port <= 0 || port > 65535 {
|
||||
port = 3306
|
||||
}
|
||||
c := SyncConfig{
|
||||
Host: a.store.Meta("sync_host"),
|
||||
Host: strings.TrimSpace(a.store.Meta("sync_host")),
|
||||
Port: port,
|
||||
User: a.store.Meta("sync_user"),
|
||||
User: strings.TrimSpace(a.store.Meta("sync_user")),
|
||||
Password: a.store.Meta("sync_password"),
|
||||
Database: a.store.Meta("sync_database"),
|
||||
Database: strings.TrimSpace(a.store.Meta("sync_database")),
|
||||
}
|
||||
if c.Host == "" && c.User == "" && c.Database == "" {
|
||||
c.Host, c.User, c.Password, c.Database = defaultSyncHost, defaultSyncUser, defaultSyncPassword, defaultSyncDatabase
|
||||
if syncConfigComplete(c) {
|
||||
if c.Port <= 0 || c.Port > 65535 {
|
||||
c.Port = 3306
|
||||
}
|
||||
return c
|
||||
}
|
||||
return c
|
||||
pkg := packagedSyncDefaults()
|
||||
if syncConfigComplete(pkg) {
|
||||
return pkg
|
||||
}
|
||||
return SyncConfig{Host: "127.0.0.1", Port: 3306, User: "root", Password: "root", Database: "code_count"}
|
||||
}
|
||||
|
||||
// applyPackagedSyncConfig 启动时用打包配置覆盖本地 SQLite 中的同步连接,
|
||||
// 避免旧安装残留的 127.0.0.1 等 meta 继续生效。
|
||||
func (a *App) applyPackagedSyncConfig() {
|
||||
if a.store == nil {
|
||||
return
|
||||
}
|
||||
pkg := packagedSyncDefaults()
|
||||
if !syncConfigComplete(pkg) {
|
||||
return
|
||||
}
|
||||
_ = a.store.SetMeta("sync_host", strings.TrimSpace(pkg.Host))
|
||||
_ = a.store.SetMeta("sync_port", strconv.Itoa(pkg.Port))
|
||||
_ = a.store.SetMeta("sync_user", strings.TrimSpace(pkg.User))
|
||||
_ = a.store.SetMeta("sync_password", pkg.Password)
|
||||
_ = a.store.SetMeta("sync_database", strings.TrimSpace(pkg.Database))
|
||||
}
|
||||
|
||||
func syncConfigComplete(c SyncConfig) bool {
|
||||
|
||||
Reference in New Issue
Block a user