Skip to content

Commit

Permalink
feat: support yaml config (#15)
Browse files Browse the repository at this point in the history
* feat: improve package archive level

* feat: support yaml config
  • Loading branch information
pluveto authored Jan 16, 2024
1 parent 4196942 commit fa62c0d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cmd/flydav/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"github.com/pluveto/flydav/cmd/flydav/app"
"github.com/pluveto/flydav/cmd/flydav/conf"
"github.com/pluveto/flydav/pkg/logger"
"github.com/pluveto/flydav/pkg/misc"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
"golang.org/x/term"
"gopkg.in/yaml.v3"
)

// main Entry point of the application
Expand Down Expand Up @@ -107,10 +109,28 @@ func loadConfValid(path string, defaultConf conf.Conf, defaultConfPath string) c
if _, err := os.Stat(preferredPath); err == nil {
path = preferredPath
}
_, err := toml.DecodeFile(path, &defaultConf)
err := decode(path, &defaultConf)
if err != nil {
logger.Warn("failed to load config file: ", err, " using default config")
}
logger.WithField("conf", &defaultConf).Debug("configuration loaded")
return defaultConf
}

func decode(path string, conf *conf.Conf) (error) {
ext, err := misc.MustGetFileExt(path)
if err != nil {
return err
}

switch ext {
case ".toml":
_, err = toml.DecodeFile(path, conf)
case ".yaml", ".yml":
err = yaml.Unmarshal([]byte(path), conf)
default:
err = fmt.Errorf("unsupported config file extension: %s", ext)
}

return err
}
45 changes: 45 additions & 0 deletions conf/config.default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
server:
host: 0.0.0.0
port: 7086
path: /webdav
fs_dir: /tmp/flydav
ui:
enabled: false
path: /ui
source: /usr/share/flydav/ui
auth: {}
log:
level: Warning
file:
- format: json
path: /var/log/flydav.log
max_size: 1
max_age: 28
stdout:
- format: text
output: stdout
cors:
enabled: true
allowed_origins:
- '*'
allowed_methods:
- GET
- POST
- PUT
- DELETE
- PROPFIND
- PROPPATCH
- MKCOL
- COPY
- MOVE
- LOCK
- UNLOCK
- OPTIONS
- HEAD
- PATCH
allowed_headers:
- '*'
exposed_headers:
- '*'
allow_credentials: true
max_age: 86400
16 changes: 16 additions & 0 deletions pkg/misc/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package misc

import (
"fmt"
"path/filepath"
"strings"
)

func MustGetFileExt(path string) (string, error) {
ext := filepath.Ext(path)
if ext == "" || ext == "." {
return "", fmt.Errorf("invalid file path " + path)
}
return strings.TrimPrefix(ext, "."), nil
}

27 changes: 27 additions & 0 deletions pkg/misc/lib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package misc

import "testing"
func TestMustGetFileExt(t *testing.T) {
tests := []struct {
path string
expected string
err bool
}{
{"file.txt", "txt", false},
{"path/to/file.jpg", "jpg", false},
{"no_extension", "", true},
{"", "", true},
}

for _, test := range tests {
ext, err := MustGetFileExt(test.path)
if test.err && err == nil {
t.Errorf("Expected error for path %s, but got nil", test.path)
} else if !test.err && err != nil {
t.Errorf("Unexpected error for path %s: %v", test.path, err)
}
if ext != test.expected {
t.Errorf("Expected extension %s for path %s, but got %s", test.expected, test.path, ext)
}
}
}

0 comments on commit fa62c0d

Please sign in to comment.