Skip to content

Commit

Permalink
fix: server config saved bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Sep 13, 2023
1 parent b484af2 commit a1267d6
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 58 deletions.
4 changes: 2 additions & 2 deletions cmd/vasedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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.")
Expand Down
47 changes: 17 additions & 30 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
42 changes: 22 additions & 20 deletions conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
vasedb:
port: 2468 # 服务 HTTP 协议端口
port: 2068 # 服务 HTTP 协议端口
mode: mmap # 默认为 Direct/IO,另外可以设置 mmap 模式
filesize: 102400 # 默认个数据文件大小,单位 KB
path: /tmp/vasedb # 数据库文件存储目录
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion server/router/api.go → server/api/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package router
package api

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion server/router/handler.go → server/api/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package router
package api

import (
"encoding/json"
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion vfs/filesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit a1267d6

Please sign in to comment.