59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
var defaultCfg = Config{
|
|
ServerName: "My Fileserver",
|
|
Port: 8080,
|
|
Directories: []Directory{
|
|
{
|
|
Id: "example",
|
|
DisplayName: "Example Directory",
|
|
Path: "/path/to/directory",
|
|
},
|
|
},
|
|
}
|
|
|
|
// Directory represents a configuration for a directory to be served.
|
|
type Directory struct {
|
|
Id string `yaml:"id"`
|
|
DisplayName string `yaml:"display_name"`
|
|
Description string `yaml:"description"`
|
|
Path string `yaml:"path"`
|
|
}
|
|
|
|
// Config represents a configuration for the fileserver.
|
|
type Config struct {
|
|
ServerName string `yaml:"server_name"`
|
|
Port int `yaml:"port"`
|
|
Directories []Directory `yaml:"directories"`
|
|
}
|
|
|
|
// LoadConfig loads configuration from a YAML file.
|
|
func LoadConfig(path string) (*Config, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var cfg Config
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|
|
|
|
// WriteExampleConfig writes an example configuration to a YAML file.
|
|
func WriteExampleConfig(filepath string) error {
|
|
data, err := yaml.Marshal(&defaultCfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return os.WriteFile(filepath, data, 0644)
|
|
}
|