diff --git a/cmd/vasedb.go b/cmd/vasedb.go index e94ba86..b752522 100644 --- a/cmd/vasedb.go +++ b/cmd/vasedb.go @@ -52,7 +52,7 @@ func init() { fl := parseFlags() // 根据命令行传入的配置文件地址,覆盖掉默认的配置 - if fl.config != conf.DefaultConfig.FilePath { + if conf.IsDefault(fl.config) { if err := conf.Load(fl.config, conf.Settings); err != nil { clog.Failed(err) } @@ -99,7 +99,7 @@ type flags struct { func parseFlags() (fl *flags) { fl = new(flags) flag.StringVar(&fl.auth, "auth", conf.DefaultConfig.Password, "--auth specify the server authentication password.") - flag.StringVar(&fl.config, "config", conf.DefaultConfig.FilePath, "--config specify the configuration file path.") + flag.StringVar(&fl.config, "config", "", "--config specify the configuration file path.") flag.StringVar(&fl.path, "path", conf.DefaultConfig.Path, "--path specify the data storage directory.") flag.IntVar(&fl.port, "port", conf.DefaultConfig.Port, "--port specify the HTTP server port.") flag.BoolVar(&fl.debug, "debug", conf.DefaultConfig.Debug, "--debug whether to enable debug mode.") diff --git a/conf/config.go b/conf/config.go index 416055c..793cb69 100644 --- a/conf/config.go +++ b/conf/config.go @@ -3,17 +3,21 @@ package conf import ( "encoding/json" "io/fs" + "io/ioutil" "path/filepath" - "strings" "github.com/auula/vasedb/clog" "github.com/spf13/viper" + "gopkg.in/yaml.v2" ) const ( cfSuffix = "yaml" defaultFileName = "config" - defaultFilePath = "./config.yaml" + defaultFilePath = "" + + // 设置默认文件系统权限 + Permissions = fs.FileMode(0755) // DefaultConfigJSON configure json string DefaultConfigJSON = ` @@ -54,24 +58,21 @@ func init() { // 先读内置默认配置,设置为全局的配置 if err := DefaultConfig.Unmarshal([]byte(DefaultConfigJSON)); err != nil { - // 读取失败直接退出进程 + // 读取失败直接退出进程,打印对应堆栈信息 clog.Failed(err) } - // 设置默认的配置文件路径 - DefaultConfig.FilePath = defaultFilePath - - // 设置默认文件系统权限 - DefaultConfig.Permissions = fs.FileMode(0755) - // 当初始化完成之后应该使用此 Settings 配置 if err := Settings.Unmarshal([]byte(DefaultConfigJSON)); err != nil { - // 读取失败直接退出进程 clog.Failed(err) } } +func IsDefault(flag string) bool { + return flag != defaultFilePath +} + // Load through a configuration file func Load(file string, opt *ServerConfig) error { @@ -110,28 +111,16 @@ func ReloadConfig() (*ServerConfig, error) { } // Saved Settings.Path 存储到磁盘中 -func (opt *ServerConfig) Saved() error { +func (opt *ServerConfig) Saved(path string) error { - v := viper.New() - - jsonData, err := opt.Marshal() + // 将配置对象转换为 YAML 格式的字节数组 + yamlData, err := yaml.Marshal(&opt) if err != nil { return err } - // 读取 JSON 数据到配置对象 - err = v.ReadConfig(strings.NewReader(string(jsonData))) - if err != nil { - return err - } - - // path := filepath.Join(opt.Path, Dirs[0], defaultFileName+"."+cfSuffix) - - // 创建 config.yaml 文件 - // file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, opt.Permissions) - - // 将配置对象写入 YAML 文件 - return v.WriteConfigAs(filepath.Join(opt.Path, Dirs[0], defaultFileName+"."+cfSuffix)) + // // 将 YAML 数据写入文件 + return ioutil.WriteFile(filepath.Join(path, defaultFileName+"."+cfSuffix), yamlData, Permissions) } func (opt *ServerConfig) Unmarshal(data []byte) error { @@ -143,9 +132,7 @@ func (opt *ServerConfig) Marshal() ([]byte, error) { } type ServerConfig struct { - VaseDB `json:"vasedb"` - FilePath string - Permissions fs.FileMode + VaseDB `json:"vasedb"` } type VaseDB struct { diff --git a/conf/config_test.go b/conf/config_test.go index 75178aa..56d901f 100644 --- a/conf/config_test.go +++ b/conf/config_test.go @@ -106,34 +106,36 @@ func TestReloadConfig(t *testing.T) { func TestSavedConfig(t *testing.T) { + // 创建一个临时目录用于测试 + tmpDir := t.TempDir() + // 创建一个 ServerConfig 实例 config := &ServerConfig{ VaseDB: VaseDB{ - Port: 8080, - Path: "./_temp", - Debug: true, + Port: 8080, + Path: tmpDir, + Debug: true, + Mode: "mmap", + FileSize: 10248080, + Logging: "/tmp/vasedb/out.log", + Password: "password@123", + Encoder: Encoder{ + Enable: true, + Secret: "/tmp/vasedb/etc/encrypt.wasm", + }, + Compressor: Compressor{ + Enable: true, + Mode: "cycle", + Hotspot: 1000, + Second: 15000, + }, }, } // 调用 Saved 函数 - err := config.Saved() + err := config.Saved(tmpDir) + if err != nil { t.Fatalf("Error saving config: %v", err) } } - -func TestMain(m *testing.M) { - // 执行一些初始化操作 - dir := "./_temp/etc" - - os.MkdirAll(dir, 0600) - - // 运行测试,并获取返回的退出代码 - exitCode := m.Run() - - // 执行一些清理操作 - os.RemoveAll(dir) - - // 退出测试程序 - os.Exit(exitCode) -} diff --git a/config.yaml b/config.yaml index 8375eb0..a3a210e 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,5 @@ vasedb: - port: 2468 # 服务 HTTP 协议端口 + port: 2068 # 服务 HTTP 协议端口 mode: mmap # 默认为 Direct/IO,另外可以设置 mmap 模式 filesize: 102400 # 默认个数据文件大小,单位 KB path: /tmp/vasedb # 数据库文件存储目录 diff --git a/go.mod b/go.mod index 9aa2323..ad17c3f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/fatih/color v1.13.0 github.com/gorilla/mux v1.8.0 github.com/spf13/viper v1.16.0 + gopkg.in/yaml.v2 v2.4.0 ) require ( diff --git a/go.sum b/go.sum index 77651bb..913f1bd 100644 --- a/go.sum +++ b/go.sum @@ -479,6 +479,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/server/router/api.go b/server/api/api.go similarity index 98% rename from server/router/api.go rename to server/api/api.go index c8a7d88..68618f7 100644 --- a/server/router/api.go +++ b/server/api/api.go @@ -1,4 +1,4 @@ -package router +package api import ( "net/http" diff --git a/server/router/handler.go b/server/api/handler.go similarity index 99% rename from server/router/handler.go rename to server/api/handler.go index 60b165d..6bfdcb4 100644 --- a/server/router/handler.go +++ b/server/api/handler.go @@ -1,4 +1,4 @@ -package router +package api import ( "encoding/json" diff --git a/server/server.go b/server/server.go index 07b7364..bed8465 100644 --- a/server/server.go +++ b/server/server.go @@ -11,7 +11,7 @@ import ( "github.com/auula/vasedb/clog" "github.com/auula/vasedb/conf" - "github.com/auula/vasedb/server/router" + "github.com/auula/vasedb/server/api" ) var ( @@ -33,7 +33,7 @@ func New(opt *conf.ServerConfig) *HttpServer { hs := HttpServer{ s: &http.Server{ - Handler: router.Root, + Handler: api.Root, Addr: net.JoinHostPort(IPv4, strconv.Itoa(opt.Port)), WriteTimeout: 3 * time.Second, ReadTimeout: 3 * time.Second, diff --git a/vfs/filesys.go b/vfs/filesys.go index f1c23a1..878e430 100644 --- a/vfs/filesys.go +++ b/vfs/filesys.go @@ -19,7 +19,7 @@ func InitFS(path string) error { clog.Info(fmt.Sprintf("Initial %s checked successful", dir)) } else { // 不存在创建对应的目录 - if err := os.MkdirAll(filepath.Join(path, dir), conf.Settings.Permissions); err != nil { + if err := os.MkdirAll(filepath.Join(path, dir), conf.Permissions); err != nil { return err } }