🎨 Optimize configuration service
This commit is contained in:
@@ -190,11 +190,6 @@ export enum LanguageType {
|
|||||||
* PathsConfig 路径配置集合
|
* PathsConfig 路径配置集合
|
||||||
*/
|
*/
|
||||||
export class PathsConfig {
|
export class PathsConfig {
|
||||||
/**
|
|
||||||
* 配置文件路径
|
|
||||||
*/
|
|
||||||
"configPath": string;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日志文件路径
|
* 日志文件路径
|
||||||
*/
|
*/
|
||||||
@@ -207,9 +202,6 @@ export class PathsConfig {
|
|||||||
|
|
||||||
/** Creates a new PathsConfig instance. */
|
/** Creates a new PathsConfig instance. */
|
||||||
constructor($$source: Partial<PathsConfig> = {}) {
|
constructor($$source: Partial<PathsConfig> = {}) {
|
||||||
if (!("configPath" in $$source)) {
|
|
||||||
this["configPath"] = "";
|
|
||||||
}
|
|
||||||
if (!("logPath" in $$source)) {
|
if (!("logPath" in $$source)) {
|
||||||
this["logPath"] = "";
|
this["logPath"] = "";
|
||||||
}
|
}
|
||||||
|
@@ -12,13 +12,13 @@ import {Call as $Call, Create as $Create} from "@wailsio/runtime";
|
|||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore: Unused imports
|
// @ts-ignore: Unused imports
|
||||||
import * as models$0 from "../../models/models.js";
|
import * as models$0 from "../models/models.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GetConfig 获取完整应用配置
|
* GetConfig 获取完整应用配置
|
||||||
*/
|
*/
|
||||||
export function GetConfig(): Promise<models$0.AppConfig | null> & { cancel(): void } {
|
export function GetConfig(): Promise<models$0.AppConfig | null> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(3332910023) as any;
|
let $resultPromise = $Call.ByID(1013336538) as any;
|
||||||
let $typingPromise = $resultPromise.then(($result: any) => {
|
let $typingPromise = $resultPromise.then(($result: any) => {
|
||||||
return $$createType1($result);
|
return $$createType1($result);
|
||||||
}) as any;
|
}) as any;
|
||||||
@@ -26,19 +26,11 @@ export function GetConfig(): Promise<models$0.AppConfig | null> & { cancel(): vo
|
|||||||
return $typingPromise;
|
return $typingPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* GetConfigPath 获取当前配置文件路径
|
|
||||||
*/
|
|
||||||
export function GetConfigPath(): Promise<string> & { cancel(): void } {
|
|
||||||
let $resultPromise = $Call.ByID(2145796624) as any;
|
|
||||||
return $resultPromise;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GetEditorConfig 获取编辑器配置
|
* GetEditorConfig 获取编辑器配置
|
||||||
*/
|
*/
|
||||||
export function GetEditorConfig(): Promise<models$0.EditorConfig> & { cancel(): void } {
|
export function GetEditorConfig(): Promise<models$0.EditorConfig> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(3738882774) as any;
|
let $resultPromise = $Call.ByID(3648153351) as any;
|
||||||
let $typingPromise = $resultPromise.then(($result: any) => {
|
let $typingPromise = $resultPromise.then(($result: any) => {
|
||||||
return $$createType2($result);
|
return $$createType2($result);
|
||||||
}) as any;
|
}) as any;
|
||||||
@@ -50,7 +42,7 @@ export function GetEditorConfig(): Promise<models$0.EditorConfig> & { cancel():
|
|||||||
* GetLanguage 获取当前语言设置
|
* GetLanguage 获取当前语言设置
|
||||||
*/
|
*/
|
||||||
export function GetLanguage(): Promise<models$0.LanguageType> & { cancel(): void } {
|
export function GetLanguage(): Promise<models$0.LanguageType> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(1762264443) as any;
|
let $resultPromise = $Call.ByID(3409375894) as any;
|
||||||
return $resultPromise;
|
return $resultPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +50,7 @@ export function GetLanguage(): Promise<models$0.LanguageType> & { cancel(): void
|
|||||||
* GetMetadata 获取配置元数据
|
* GetMetadata 获取配置元数据
|
||||||
*/
|
*/
|
||||||
export function GetMetadata(): Promise<models$0.ConfigMetadata> & { cancel(): void } {
|
export function GetMetadata(): Promise<models$0.ConfigMetadata> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(356327488) as any;
|
let $resultPromise = $Call.ByID(3276720617) as any;
|
||||||
let $typingPromise = $resultPromise.then(($result: any) => {
|
let $typingPromise = $resultPromise.then(($result: any) => {
|
||||||
return $$createType3($result);
|
return $$createType3($result);
|
||||||
}) as any;
|
}) as any;
|
||||||
@@ -70,7 +62,7 @@ export function GetMetadata(): Promise<models$0.ConfigMetadata> & { cancel(): vo
|
|||||||
* GetPaths 获取路径配置
|
* GetPaths 获取路径配置
|
||||||
*/
|
*/
|
||||||
export function GetPaths(): Promise<models$0.PathsConfig> & { cancel(): void } {
|
export function GetPaths(): Promise<models$0.PathsConfig> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(1115810463) as any;
|
let $resultPromise = $Call.ByID(1096257096) as any;
|
||||||
let $typingPromise = $resultPromise.then(($result: any) => {
|
let $typingPromise = $resultPromise.then(($result: any) => {
|
||||||
return $$createType4($result);
|
return $$createType4($result);
|
||||||
}) as any;
|
}) as any;
|
||||||
@@ -82,7 +74,7 @@ export function GetPaths(): Promise<models$0.PathsConfig> & { cancel(): void } {
|
|||||||
* ResetConfig 重置为默认配置
|
* ResetConfig 重置为默认配置
|
||||||
*/
|
*/
|
||||||
export function ResetConfig(): Promise<void> & { cancel(): void } {
|
export function ResetConfig(): Promise<void> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(2265390548) as any;
|
let $resultPromise = $Call.ByID(3593047389) as any;
|
||||||
return $resultPromise;
|
return $resultPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +82,7 @@ export function ResetConfig(): Promise<void> & { cancel(): void } {
|
|||||||
* SaveConfig 保存完整应用配置
|
* SaveConfig 保存完整应用配置
|
||||||
*/
|
*/
|
||||||
export function SaveConfig(config: models$0.AppConfig | null): Promise<void> & { cancel(): void } {
|
export function SaveConfig(config: models$0.AppConfig | null): Promise<void> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(2710445504, config) as any;
|
let $resultPromise = $Call.ByID(616684383, config) as any;
|
||||||
return $resultPromise;
|
return $resultPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,15 +90,7 @@ export function SaveConfig(config: models$0.AppConfig | null): Promise<void> & {
|
|||||||
* SetLanguage 设置语言
|
* SetLanguage 设置语言
|
||||||
*/
|
*/
|
||||||
export function SetLanguage(language: models$0.LanguageType): Promise<void> & { cancel(): void } {
|
export function SetLanguage(language: models$0.LanguageType): Promise<void> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(2553541807, language) as any;
|
let $resultPromise = $Call.ByID(814725002, language) as any;
|
||||||
return $resultPromise;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* UpdateConfigPath 更新配置文件路径
|
|
||||||
*/
|
|
||||||
export function UpdateConfigPath(newPath: string): Promise<void> & { cancel(): void } {
|
|
||||||
let $resultPromise = $Call.ByID(251712339, newPath) as any;
|
|
||||||
return $resultPromise;
|
return $resultPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +98,7 @@ export function UpdateConfigPath(newPath: string): Promise<void> & { cancel(): v
|
|||||||
* UpdateEditorConfig 更新编辑器配置
|
* UpdateEditorConfig 更新编辑器配置
|
||||||
*/
|
*/
|
||||||
export function UpdateEditorConfig(editorConfig: models$0.EditorConfig): Promise<void> & { cancel(): void } {
|
export function UpdateEditorConfig(editorConfig: models$0.EditorConfig): Promise<void> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(420530601, editorConfig) as any;
|
let $resultPromise = $Call.ByID(1237949666, editorConfig) as any;
|
||||||
return $resultPromise;
|
return $resultPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +106,7 @@ export function UpdateEditorConfig(editorConfig: models$0.EditorConfig): Promise
|
|||||||
* UpdateMetadata 更新配置元数据
|
* UpdateMetadata 更新配置元数据
|
||||||
*/
|
*/
|
||||||
export function UpdateMetadata(metadata: models$0.ConfigMetadata): Promise<void> & { cancel(): void } {
|
export function UpdateMetadata(metadata: models$0.ConfigMetadata): Promise<void> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(1687760751, metadata) as any;
|
let $resultPromise = $Call.ByID(3353893284, metadata) as any;
|
||||||
return $resultPromise;
|
return $resultPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +114,7 @@ export function UpdateMetadata(metadata: models$0.ConfigMetadata): Promise<void>
|
|||||||
* UpdatePaths 更新路径配置
|
* UpdatePaths 更新路径配置
|
||||||
*/
|
*/
|
||||||
export function UpdatePaths(paths: models$0.PathsConfig): Promise<void> & { cancel(): void } {
|
export function UpdatePaths(paths: models$0.PathsConfig): Promise<void> & { cancel(): void } {
|
||||||
let $resultPromise = $Call.ByID(3466187518, paths) as any;
|
let $resultPromise = $Call.ByID(3650847675, paths) as any;
|
||||||
return $resultPromise;
|
return $resultPromise;
|
||||||
}
|
}
|
||||||
|
|
@@ -1,6 +1,6 @@
|
|||||||
import {createI18n} from 'vue-i18n';
|
import {createI18n} from 'vue-i18n';
|
||||||
import messages from './locales';
|
import messages from './locales';
|
||||||
import { ConfigService } from '@/../bindings/voidraft/internal/services/config';
|
import { ConfigService } from '@/../bindings/voidraft/internal/services';
|
||||||
import { LanguageType } from '@/../bindings/voidraft/internal/models';
|
import { LanguageType } from '@/../bindings/voidraft/internal/models';
|
||||||
|
|
||||||
// 定义支持的语言类型
|
// 定义支持的语言类型
|
||||||
|
@@ -3,7 +3,7 @@ import {ref, watch} from 'vue';
|
|||||||
import {useDebounceFn} from '@vueuse/core';
|
import {useDebounceFn} from '@vueuse/core';
|
||||||
import {
|
import {
|
||||||
ConfigService
|
ConfigService
|
||||||
} from '@/../bindings/voidraft/internal/services/config';
|
} from '@/../bindings/voidraft/internal/services';
|
||||||
import {EditorConfig, TabType} from '@/../bindings/voidraft/internal/models/models';
|
import {EditorConfig, TabType} from '@/../bindings/voidraft/internal/models/models';
|
||||||
import {useLogStore} from './logStore';
|
import {useLogStore} from './logStore';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
|
@@ -38,7 +38,6 @@ const (
|
|||||||
|
|
||||||
// PathsConfig 路径配置集合
|
// PathsConfig 路径配置集合
|
||||||
type PathsConfig struct {
|
type PathsConfig struct {
|
||||||
ConfigPath string `json:"configPath"` // 配置文件路径
|
|
||||||
LogPath string `json:"logPath"` // 日志文件路径
|
LogPath string `json:"logPath"` // 日志文件路径
|
||||||
DataPath string `json:"dataPath"` // 数据存储路径
|
DataPath string `json:"dataPath"` // 数据存储路径
|
||||||
}
|
}
|
||||||
@@ -77,7 +76,6 @@ func NewDefaultAppConfig() *AppConfig {
|
|||||||
AlwaysOnTop: false,
|
AlwaysOnTop: false,
|
||||||
},
|
},
|
||||||
Paths: PathsConfig{
|
Paths: PathsConfig{
|
||||||
ConfigPath: filepath.Join(rootDir, "config", "config.json"),
|
|
||||||
LogPath: filepath.Join(rootDir, "logs"),
|
LogPath: filepath.Join(rootDir, "logs"),
|
||||||
DataPath: filepath.Join(rootDir, "data"),
|
DataPath: filepath.Join(rootDir, "data"),
|
||||||
},
|
},
|
||||||
|
@@ -1,93 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v3/pkg/services/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ConfigLocator 配置定位器接口
|
|
||||||
type ConfigLocator interface {
|
|
||||||
// GetConfigPath 获取配置文件路径
|
|
||||||
GetConfigPath() string
|
|
||||||
|
|
||||||
// SetConfigPath 设置配置文件路径
|
|
||||||
SetConfigPath(string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// FileConfigLocator 基于文件的配置定位器
|
|
||||||
type FileConfigLocator struct {
|
|
||||||
locationFile string
|
|
||||||
defaultPath string
|
|
||||||
logger *log.LoggerService
|
|
||||||
mu sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFileConfigLocator 创建文件配置定位器
|
|
||||||
func NewFileConfigLocator(locationFile, defaultPath string, logger *log.LoggerService) *FileConfigLocator {
|
|
||||||
if logger == nil {
|
|
||||||
logger = log.New()
|
|
||||||
}
|
|
||||||
|
|
||||||
return &FileConfigLocator{
|
|
||||||
locationFile: locationFile,
|
|
||||||
defaultPath: defaultPath,
|
|
||||||
logger: logger,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDefaultConfigPath 获取默认配置路径
|
|
||||||
func GetDefaultConfigPath() string {
|
|
||||||
homePath, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return filepath.Join(".voidraft", "config", "config.json")
|
|
||||||
}
|
|
||||||
return filepath.Join(homePath, ".voidraft", "config", "config.json")
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetConfigPath 获取配置文件路径
|
|
||||||
func (fcl *FileConfigLocator) GetConfigPath() string {
|
|
||||||
fcl.mu.RLock()
|
|
||||||
defer fcl.mu.RUnlock()
|
|
||||||
|
|
||||||
// 尝试从位置文件读取
|
|
||||||
if _, err := os.Stat(fcl.locationFile); err == nil {
|
|
||||||
if data, err := os.ReadFile(fcl.locationFile); err == nil && len(data) > 0 {
|
|
||||||
path := string(data)
|
|
||||||
|
|
||||||
// 验证路径目录是否存在
|
|
||||||
if _, err := os.Stat(filepath.Dir(path)); err == nil {
|
|
||||||
fcl.logger.Info("ConfigLocator: Using stored path", "path", path)
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
|
|
||||||
fcl.logger.Error("ConfigLocator: Stored path invalid, using default", "path", path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回默认路径
|
|
||||||
fcl.logger.Info("ConfigLocator: Using default path", "path", fcl.defaultPath)
|
|
||||||
return fcl.defaultPath
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetConfigPath 设置配置文件路径
|
|
||||||
func (fcl *FileConfigLocator) SetConfigPath(path string) error {
|
|
||||||
fcl.mu.Lock()
|
|
||||||
defer fcl.mu.Unlock()
|
|
||||||
|
|
||||||
// 确保位置文件目录存在
|
|
||||||
if err := os.MkdirAll(filepath.Dir(fcl.locationFile), 0755); err != nil {
|
|
||||||
return fmt.Errorf("failed to create location directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入位置文件
|
|
||||||
if err := os.WriteFile(fcl.locationFile, []byte(path), 0644); err != nil {
|
|
||||||
return fmt.Errorf("failed to write location file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fcl.logger.Info("ConfigLocator: Updated config path", "path", path)
|
|
||||||
return nil
|
|
||||||
}
|
|
@@ -1,117 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"sync"
|
|
||||||
"voidraft/internal/models"
|
|
||||||
"voidraft/internal/services/store"
|
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v3/pkg/services/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ConfigStorage 配置存储接口
|
|
||||||
type ConfigStorage interface {
|
|
||||||
// Load 加载配置
|
|
||||||
Load() (models.AppConfig, error)
|
|
||||||
|
|
||||||
// Save 保存配置
|
|
||||||
Save(models.AppConfig) error
|
|
||||||
|
|
||||||
// GetPath 获取存储路径
|
|
||||||
GetPath() string
|
|
||||||
|
|
||||||
// MoveTo 移动到新路径
|
|
||||||
MoveTo(string, models.AppConfig) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// FileConfigStorage 基于文件的配置存储
|
|
||||||
type FileConfigStorage struct {
|
|
||||||
store *store.Store[models.AppConfig]
|
|
||||||
currentPath string
|
|
||||||
logger *log.LoggerService
|
|
||||||
mu sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFileConfigStorage 创建文件配置存储
|
|
||||||
func NewFileConfigStorage(path string, logger *log.LoggerService) *FileConfigStorage {
|
|
||||||
if logger == nil {
|
|
||||||
logger = log.New()
|
|
||||||
}
|
|
||||||
|
|
||||||
return &FileConfigStorage{
|
|
||||||
store: store.NewStore[models.AppConfig](store.StoreOption{
|
|
||||||
FilePath: path,
|
|
||||||
AutoSave: true,
|
|
||||||
Logger: logger,
|
|
||||||
}),
|
|
||||||
currentPath: path,
|
|
||||||
logger: logger,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load 加载配置
|
|
||||||
func (fcs *FileConfigStorage) Load() (models.AppConfig, error) {
|
|
||||||
fcs.mu.RLock()
|
|
||||||
defer fcs.mu.RUnlock()
|
|
||||||
|
|
||||||
config := fcs.store.Get()
|
|
||||||
|
|
||||||
// 检查配置是否为空
|
|
||||||
if isEmptyConfig(config) {
|
|
||||||
return models.AppConfig{}, fmt.Errorf("empty config detected")
|
|
||||||
}
|
|
||||||
|
|
||||||
return config, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save 保存配置
|
|
||||||
func (fcs *FileConfigStorage) Save(config models.AppConfig) error {
|
|
||||||
fcs.mu.Lock()
|
|
||||||
defer fcs.mu.Unlock()
|
|
||||||
|
|
||||||
return fcs.store.Set(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetPath 获取存储路径
|
|
||||||
func (fcs *FileConfigStorage) GetPath() string {
|
|
||||||
fcs.mu.RLock()
|
|
||||||
defer fcs.mu.RUnlock()
|
|
||||||
|
|
||||||
return fcs.currentPath
|
|
||||||
}
|
|
||||||
|
|
||||||
// MoveTo 移动到新路径
|
|
||||||
func (fcs *FileConfigStorage) MoveTo(newPath string, config models.AppConfig) error {
|
|
||||||
fcs.mu.Lock()
|
|
||||||
defer fcs.mu.Unlock()
|
|
||||||
|
|
||||||
// 创建目录
|
|
||||||
if err := os.MkdirAll(filepath.Dir(newPath), 0755); err != nil {
|
|
||||||
return fmt.Errorf("failed to create directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建新存储
|
|
||||||
newStore := store.NewStore[models.AppConfig](store.StoreOption{
|
|
||||||
FilePath: newPath,
|
|
||||||
AutoSave: true,
|
|
||||||
Logger: fcs.logger,
|
|
||||||
})
|
|
||||||
|
|
||||||
// 保存到新位置
|
|
||||||
if err := newStore.Set(config); err != nil {
|
|
||||||
return fmt.Errorf("failed to save config to new path: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新状态
|
|
||||||
fcs.store = newStore
|
|
||||||
fcs.currentPath = newPath
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// isEmptyConfig 检查配置是否为空
|
|
||||||
func isEmptyConfig(config models.AppConfig) bool {
|
|
||||||
return config.Editor.FontSize == 0
|
|
||||||
}
|
|
@@ -1,27 +1,24 @@
|
|||||||
package config
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/wailsapp/wails/v3/pkg/services/log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"voidraft/internal/models"
|
"voidraft/internal/models"
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v3/pkg/services/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigService 提供配置管理功能
|
// ConfigService 提供配置管理功能
|
||||||
type ConfigService struct {
|
type ConfigService struct {
|
||||||
storage ConfigStorage // 配置存储接口
|
store *Store[models.AppConfig] // 配置存储
|
||||||
locator ConfigLocator // 配置定位器接口
|
path string // 配置文件路径
|
||||||
logger *log.LoggerService
|
logger *log.LoggerService
|
||||||
cache *models.AppConfig // 配置缓存
|
cache *models.AppConfig // 配置缓存
|
||||||
cacheMu sync.RWMutex // 缓存锁
|
cacheMu sync.RWMutex // 缓存锁
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service struct{}
|
|
||||||
|
|
||||||
// NewConfigService 创建新的配置服务实例
|
// NewConfigService 创建新的配置服务实例
|
||||||
func NewConfigService() *ConfigService {
|
func NewConfigService() *ConfigService {
|
||||||
// 初始化日志服务
|
// 初始化日志服务
|
||||||
@@ -34,24 +31,21 @@ func NewConfigService() *ConfigService {
|
|||||||
homePath = "."
|
homePath = "."
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取默认配置路径
|
// 固定配置路径
|
||||||
defaultPath := GetDefaultConfigPath()
|
configPath := filepath.Join(homePath, ".voidraft", "config", "config.json")
|
||||||
|
|
||||||
// 创建配置定位器
|
|
||||||
locationFile := filepath.Join(homePath, ".voidraft", "config.location")
|
|
||||||
locator := NewFileConfigLocator(locationFile, defaultPath, logger)
|
|
||||||
|
|
||||||
// 获取实际配置路径
|
|
||||||
configPath := locator.GetConfigPath()
|
|
||||||
logger.Info("Config: Using config path", "path", configPath)
|
logger.Info("Config: Using config path", "path", configPath)
|
||||||
|
|
||||||
// 创建配置存储
|
// 创建存储
|
||||||
storage := NewFileConfigStorage(configPath, logger)
|
store := NewStore[models.AppConfig](StoreOption{
|
||||||
|
FilePath: configPath,
|
||||||
|
AutoSave: true,
|
||||||
|
Logger: logger,
|
||||||
|
})
|
||||||
|
|
||||||
// 构造配置服务实例
|
// 构造配置服务实例
|
||||||
service := &ConfigService{
|
service := &ConfigService{
|
||||||
storage: storage,
|
store: store,
|
||||||
locator: locator,
|
path: configPath,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,14 +58,13 @@ func NewConfigService() *ConfigService {
|
|||||||
// loadInitialConfig 加载初始配置
|
// loadInitialConfig 加载初始配置
|
||||||
func (cs *ConfigService) loadInitialConfig() {
|
func (cs *ConfigService) loadInitialConfig() {
|
||||||
// 尝试加载配置
|
// 尝试加载配置
|
||||||
config, err := cs.storage.Load()
|
config, err := cs.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 如果加载失败,使用默认配置
|
// 如果加载失败,使用默认配置
|
||||||
defaultConfig := models.NewDefaultAppConfig()
|
defaultConfig := models.NewDefaultAppConfig()
|
||||||
defaultConfig.Paths.ConfigPath = cs.storage.GetPath()
|
|
||||||
|
|
||||||
// 保存默认配置
|
// 保存默认配置
|
||||||
if err := cs.storage.Save(*defaultConfig); err != nil {
|
if err := cs.save(*defaultConfig); err != nil {
|
||||||
cs.logger.Error("Config: Failed to save default config", "error", err)
|
cs.logger.Error("Config: Failed to save default config", "error", err)
|
||||||
} else {
|
} else {
|
||||||
// 更新缓存
|
// 更新缓存
|
||||||
@@ -80,14 +73,6 @@ func (cs *ConfigService) loadInitialConfig() {
|
|||||||
cs.cacheMu.Unlock()
|
cs.cacheMu.Unlock()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 确保配置中的路径与实际使用的路径一致
|
|
||||||
if config.Paths.ConfigPath != cs.storage.GetPath() {
|
|
||||||
config.Paths.ConfigPath = cs.storage.GetPath()
|
|
||||||
if err := cs.storage.Save(config); err != nil {
|
|
||||||
cs.logger.Error("Config: Failed to sync config path", "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新缓存
|
// 更新缓存
|
||||||
cs.cacheMu.Lock()
|
cs.cacheMu.Lock()
|
||||||
cs.cache = &config
|
cs.cache = &config
|
||||||
@@ -95,6 +80,28 @@ func (cs *ConfigService) loadInitialConfig() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load 加载配置
|
||||||
|
func (cs *ConfigService) load() (models.AppConfig, error) {
|
||||||
|
config := cs.store.Get()
|
||||||
|
|
||||||
|
// 检查配置是否为空
|
||||||
|
if isEmptyConfig(config) {
|
||||||
|
return models.AppConfig{}, fmt.Errorf("empty config detected")
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// save 保存配置
|
||||||
|
func (cs *ConfigService) save(config models.AppConfig) error {
|
||||||
|
return cs.store.Set(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// isEmptyConfig 检查配置是否为空
|
||||||
|
func isEmptyConfig(config models.AppConfig) bool {
|
||||||
|
return config.Editor.FontSize == 0
|
||||||
|
}
|
||||||
|
|
||||||
// GetConfig 获取完整应用配置
|
// GetConfig 获取完整应用配置
|
||||||
func (cs *ConfigService) GetConfig() (*models.AppConfig, error) {
|
func (cs *ConfigService) GetConfig() (*models.AppConfig, error) {
|
||||||
// 优先使用缓存
|
// 优先使用缓存
|
||||||
@@ -107,14 +114,13 @@ func (cs *ConfigService) GetConfig() (*models.AppConfig, error) {
|
|||||||
cs.cacheMu.RUnlock()
|
cs.cacheMu.RUnlock()
|
||||||
|
|
||||||
// 缓存不存在,从存储加载
|
// 缓存不存在,从存储加载
|
||||||
config, err := cs.storage.Load()
|
config, err := cs.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 加载失败,使用默认配置
|
// 加载失败,使用默认配置
|
||||||
defaultConfig := models.NewDefaultAppConfig()
|
defaultConfig := models.NewDefaultAppConfig()
|
||||||
defaultConfig.Paths.ConfigPath = cs.storage.GetPath()
|
|
||||||
|
|
||||||
// 保存默认配置
|
// 保存默认配置
|
||||||
if saveErr := cs.storage.Save(*defaultConfig); saveErr != nil {
|
if saveErr := cs.save(*defaultConfig); saveErr != nil {
|
||||||
cs.logger.Error("Config: Failed to save default config", "error", saveErr)
|
cs.logger.Error("Config: Failed to save default config", "error", saveErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,48 +145,13 @@ func (cs *ConfigService) SaveConfig(config *models.AppConfig) error {
|
|||||||
// 更新配置元数据
|
// 更新配置元数据
|
||||||
config.Metadata.LastUpdated = time.Now()
|
config.Metadata.LastUpdated = time.Now()
|
||||||
|
|
||||||
// 确保ConfigPath与当前路径一致
|
|
||||||
config.Paths.ConfigPath = cs.storage.GetPath()
|
|
||||||
|
|
||||||
// 更新缓存
|
// 更新缓存
|
||||||
cs.cacheMu.Lock()
|
cs.cacheMu.Lock()
|
||||||
cs.cache = config
|
cs.cache = config
|
||||||
cs.cacheMu.Unlock()
|
cs.cacheMu.Unlock()
|
||||||
|
|
||||||
// 保存到存储
|
// 保存到存储
|
||||||
return cs.storage.Save(*config)
|
return cs.save(*config)
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateConfigPath 更新配置文件路径
|
|
||||||
func (cs *ConfigService) UpdateConfigPath(newPath string) error {
|
|
||||||
// 如果路径相同,无需更改
|
|
||||||
if newPath == cs.storage.GetPath() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取当前配置(优先使用缓存)
|
|
||||||
config, err := cs.GetConfig()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get current config: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新配置中的路径
|
|
||||||
config.Paths.ConfigPath = newPath
|
|
||||||
|
|
||||||
// 移动到新路径
|
|
||||||
if err := cs.storage.MoveTo(newPath, *config); err != nil {
|
|
||||||
return fmt.Errorf("failed to move config to new path: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新定位器
|
|
||||||
if err := cs.locator.SetConfigPath(newPath); err != nil {
|
|
||||||
cs.logger.Error("Config: Failed to update location file", "error", err)
|
|
||||||
// 继续执行,这不是致命错误
|
|
||||||
}
|
|
||||||
|
|
||||||
cs.logger.Info("Config: Config path updated", "path", newPath)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdatePaths 更新路径配置
|
// UpdatePaths 更新路径配置
|
||||||
@@ -190,35 +161,16 @@ func (cs *ConfigService) UpdatePaths(paths models.PathsConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查配置文件路径是否变更
|
// 更新路径配置
|
||||||
if paths.ConfigPath != "" && paths.ConfigPath != cs.storage.GetPath() {
|
|
||||||
// 如果配置路径有变化,使用专门的方法处理
|
|
||||||
if err := cs.UpdateConfigPath(paths.ConfigPath); err != nil {
|
|
||||||
return fmt.Errorf("failed to update config path: %w", err)
|
|
||||||
}
|
|
||||||
// 更新后重新加载配置
|
|
||||||
config, err = cs.GetConfig()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新其他路径,但保持ConfigPath不变
|
|
||||||
config.Paths.LogPath = paths.LogPath
|
config.Paths.LogPath = paths.LogPath
|
||||||
config.Paths.DataPath = paths.DataPath
|
config.Paths.DataPath = paths.DataPath
|
||||||
|
|
||||||
// 确保ConfigPath与当前一致
|
|
||||||
config.Paths.ConfigPath = cs.storage.GetPath()
|
|
||||||
|
|
||||||
return cs.SaveConfig(config)
|
return cs.SaveConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetConfig 重置为默认配置
|
// ResetConfig 重置为默认配置
|
||||||
func (cs *ConfigService) ResetConfig() error {
|
func (cs *ConfigService) ResetConfig() error {
|
||||||
defaultConfig := models.NewDefaultAppConfig()
|
defaultConfig := models.NewDefaultAppConfig()
|
||||||
// 保留当前配置路径
|
|
||||||
defaultConfig.Paths.ConfigPath = cs.storage.GetPath()
|
|
||||||
|
|
||||||
return cs.SaveConfig(defaultConfig)
|
return cs.SaveConfig(defaultConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,7 +207,7 @@ func (cs *ConfigService) GetLanguage() (models.LanguageType, error) {
|
|||||||
func (cs *ConfigService) SetLanguage(language models.LanguageType) error {
|
func (cs *ConfigService) SetLanguage(language models.LanguageType) error {
|
||||||
// 验证语言类型有效
|
// 验证语言类型有效
|
||||||
if language != models.LangZhCN && language != models.LangEnUS {
|
if language != models.LangZhCN && language != models.LangEnUS {
|
||||||
return fmt.Errorf("unsupported language: %s", language)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
config, err := cs.GetConfig()
|
config, err := cs.GetConfig()
|
||||||
@@ -295,8 +247,3 @@ func (cs *ConfigService) UpdateMetadata(metadata models.ConfigMetadata) error {
|
|||||||
config.Metadata = metadata
|
config.Metadata = metadata
|
||||||
return cs.SaveConfig(config)
|
return cs.SaveConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConfigPath 获取当前配置文件路径
|
|
||||||
func (cs *ConfigService) GetConfigPath() string {
|
|
||||||
return cs.storage.GetPath()
|
|
||||||
}
|
|
@@ -2,18 +2,17 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/wailsapp/wails/v3/pkg/application"
|
"github.com/wailsapp/wails/v3/pkg/application"
|
||||||
"voidraft/internal/services/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ServiceManager 服务管理器,负责协调各个服务
|
// ServiceManager 服务管理器,负责协调各个服务
|
||||||
type ServiceManager struct {
|
type ServiceManager struct {
|
||||||
configService *config.ConfigService
|
configService *ConfigService
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServiceManager 创建新的服务管理器实例
|
// NewServiceManager 创建新的服务管理器实例
|
||||||
func NewServiceManager() *ServiceManager {
|
func NewServiceManager() *ServiceManager {
|
||||||
// 初始化配置服务
|
// 初始化配置服务
|
||||||
configService := config.NewConfigService()
|
configService := NewConfigService()
|
||||||
|
|
||||||
return &ServiceManager{
|
return &ServiceManager{
|
||||||
configService: configService,
|
configService: configService,
|
||||||
@@ -28,6 +27,6 @@ func (sm *ServiceManager) GetServices() []application.Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetConfigService 获取配置服务实例
|
// GetConfigService 获取配置服务实例
|
||||||
func (sm *ServiceManager) GetConfigService() *config.ConfigService {
|
func (sm *ServiceManager) GetConfigService() *ConfigService {
|
||||||
return sm.configService
|
return sm.configService
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
package store
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
Reference in New Issue
Block a user