Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support yaml config #15

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
prerelease:
type: boolean
required: false
default: yes
default: true

jobs:
build:
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ mac: prepare

package: all
for GOARCH in $(GOARCHS); do \
zip -q -r dist/$(PROJECT_NAME)-windows-$$GOARCH.zip dist/windows_$$GOARCH/; \
zip -q -r dist/$(PROJECT_NAME)-linux-$$GOARCH.zip dist/linux_$$GOARCH/; \
(cd dist/windows_$$GOARCH/ && zip -q -r ../../dist/$(PROJECT_NAME)-windows-$$GOARCH.zip .); \
(cd dist/linux_$$GOARCH/ && zip -q -r ../../dist/$(PROJECT_NAME)-linux-$$GOARCH.zip .); \
done

for GOARCH in $(GOARCHS_MAC); do \
zip -q -r dist/$(PROJECT_NAME)-mac-$$GOARCH.zip dist/mac_$$GOARCH/; \
(cd dist/mac_$$GOARCH/ && zip -q -r ../../dist/$(PROJECT_NAME)-mac-$$GOARCH.zip .); \
done

ARCH_RELEASE_DIRS=$$(find dist -type d -name "*_*"); \
Expand All @@ -64,4 +64,4 @@ run:
clean:
rm -rfd dist

.PHONY: all, default, clean
.PHONY: all, default, clean
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)
}
}
}