🔧 Replaces toml with yaml

 Improved config structure
This commit is contained in:
Andreas Schneider 2019-08-03 18:02:43 +02:00
parent 1111852adf
commit 9f2eefcb34
4 changed files with 39 additions and 38 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) 2018, Andreas Schneider
// Copyright (c) 2019, Andreas Schneider
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
@ -26,35 +26,34 @@
package main
import (
"github.com/BurntSushi/toml"
"golang.org/x/crypto/bcrypt"
"log"
"strings"
"github.com/go-yaml/yaml"
"os"
)
type Config struct {
ListenAddress string
BaseDirectory string
Shares map[string]Share
Logins map[string]Login
ListenAddress string `yaml:"listenAddress"`
BaseDirectory string `yaml:"baseDirectory"`
Shares map[string]Share `yaml:"shares"`
}
type Share struct {
Directory string
Directory string `yaml:"directory"`
Users map[string]ShareUser `yaml:"users"`
}
type Login struct {
Credentials map[string]Credential
}
type Credential struct {
Password string
Share string
type ShareUser struct {
Role string `yaml:"role"`
Logins map[string]string `yaml:"logins"`
}
func LoadConfig(filename string) Config {
c := Config{}
if _, err := toml.DecodeFile(filename, &c); err != nil {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(&c); err != nil {
panic(err)
}
@ -62,7 +61,7 @@ func LoadConfig(filename string) Config {
}
func (c *Config) ValidateDAVUser(username, password string) (valid bool, directory string) {
parts := strings.SplitN(username, "@", 2)
/*parts := strings.SplitN(username, "@", 2)
if len(parts) != 2 {
return false, ""
}
@ -91,5 +90,6 @@ func (c *Config) ValidateDAVUser(username, password string) (valid bool, directo
return false, ""
} else {
return true, share.Directory
}
}*/
return false, ""
}

View File

@ -1,17 +0,0 @@
ListenAddress = ":3000"
BaseDirectory = "data"
[Shares]
[Shares.Test1]
Directory = "share1"
[Shares.Test2]
Directory = "share2"
[Logins]
[Logins.User1]
[Logins.User1.Credentials.Test1]
Password = "$2a$10$5AuehKad7TxDqW2HXdYaZ.ipFajhl7ULyTR3DLCquTA3B/dxHujIq" #test
Share = "Test1"

18
example.yaml Normal file
View File

@ -0,0 +1,18 @@
listenAddress: :3000
baseDirectory: data
shares:
test1:
directory: share1
users:
User1:
role: admin
logins:
dev1: $2a$10$5AuehKad7TxDqW2HXdYaZ.ipFajhl7ULyTR3DLCquTA3B/dxHujIq #test
test2:
directory: share2
users:
User1:
role: reader
users:
User1:

View File

@ -37,7 +37,7 @@ import (
"time"
)
var configFile = flag.String("config", "sharedav.toml", "Config file to be used.")
var configFile = flag.String("config", "sharedav.yaml", "Config file to be used.")
var genPassword = flag.Bool("genpass", false, "If set, a password will be generated and hashed.")
var hashPassword = flag.String("hashpass", "", "If set, the given password will be hashed.")