🔧 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. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
@ -26,35 +26,34 @@
package main package main
import ( import (
"github.com/BurntSushi/toml" "github.com/go-yaml/yaml"
"golang.org/x/crypto/bcrypt" "os"
"log"
"strings"
) )
type Config struct { type Config struct {
ListenAddress string ListenAddress string `yaml:"listenAddress"`
BaseDirectory string BaseDirectory string `yaml:"baseDirectory"`
Shares map[string]Share Shares map[string]Share `yaml:"shares"`
Logins map[string]Login
} }
type Share struct { type Share struct {
Directory string Directory string `yaml:"directory"`
Users map[string]ShareUser `yaml:"users"`
} }
type Login struct { type ShareUser struct {
Credentials map[string]Credential Role string `yaml:"role"`
} Logins map[string]string `yaml:"logins"`
type Credential struct {
Password string
Share string
} }
func LoadConfig(filename string) Config { func LoadConfig(filename string) Config {
c := 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) panic(err)
} }
@ -62,7 +61,7 @@ func LoadConfig(filename string) Config {
} }
func (c *Config) ValidateDAVUser(username, password string) (valid bool, directory string) { 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 { if len(parts) != 2 {
return false, "" return false, ""
} }
@ -91,5 +90,6 @@ func (c *Config) ValidateDAVUser(username, password string) (valid bool, directo
return false, "" return false, ""
} else { } else {
return true, share.Directory 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" "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 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.") var hashPassword = flag.String("hashpass", "", "If set, the given password will be hashed.")