sftpgo/config/config.go

105 lines
2.8 KiB
Go
Raw Normal View History

2019-07-30 18:51:29 +00:00
// Package config manages the configuration.
// Configuration is loaded from sftpgo.conf file.
// If sftpgo.conf is not found or cannot be readed or decoded as json the default configuration is used.
// The default configuration an be found inside the source tree:
// https://github.com/drakkan/sftpgo/blob/master/sftpgo.conf
2019-07-20 10:26:52 +00:00
package config
import (
"encoding/json"
"os"
"strings"
2019-07-20 10:26:52 +00:00
"github.com/drakkan/sftpgo/api"
"github.com/drakkan/sftpgo/dataprovider"
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/sftpd"
)
const (
logSender = "config"
defaultBanner = "SFTPGo"
2019-07-20 10:26:52 +00:00
)
var (
globalConf globalConfig
)
type globalConfig struct {
SFTPD sftpd.Configuration `json:"sftpd"`
ProviderConf dataprovider.Config `json:"data_provider"`
HTTPDConfig api.HTTPDConf `json:"httpd"`
}
func init() {
// create a default configuration to use if no config file is provided
globalConf = globalConfig{
SFTPD: sftpd.Configuration{
Banner: defaultBanner,
BindPort: 2022,
BindAddress: "",
IdleTimeout: 15,
MaxAuthTries: 0,
Umask: "0022",
Actions: sftpd.Actions{
ExecuteOn: []string{},
Command: "",
HTTPNotificationURL: "",
},
2019-07-20 10:26:52 +00:00
},
ProviderConf: dataprovider.Config{
Driver: "sqlite",
Name: "sftpgo.db",
Host: "",
Port: 5432,
Username: "",
Password: "",
ConnectionString: "",
UsersTable: "users",
ManageUsers: 1,
SSLMode: 0,
TrackQuota: 1,
},
HTTPDConfig: api.HTTPDConf{
BindPort: 8080,
BindAddress: "127.0.0.1",
},
}
}
2019-07-30 18:51:29 +00:00
// GetSFTPDConfig returns the configuration for the SFTP server
2019-07-20 10:26:52 +00:00
func GetSFTPDConfig() sftpd.Configuration {
return globalConf.SFTPD
}
2019-07-30 18:51:29 +00:00
// GetHTTPDConfig returns the configuration for the HTTP server
2019-07-20 10:26:52 +00:00
func GetHTTPDConfig() api.HTTPDConf {
return globalConf.HTTPDConfig
}
2019-07-30 18:51:29 +00:00
//GetProviderConf returns the configuration for the data provider
2019-07-20 10:26:52 +00:00
func GetProviderConf() dataprovider.Config {
return globalConf.ProviderConf
}
2019-07-30 18:51:29 +00:00
// LoadConfig loads the configuration from sftpgo.conf or use the default configuration.
2019-07-20 10:26:52 +00:00
func LoadConfig(configPath string) error {
logger.Debug(logSender, "load config from path: %v", configPath)
file, err := os.Open(configPath)
if err != nil {
logger.Warn(logSender, "error loading configuration file: %v. Default configuration will be used: %+v", err, globalConf)
2019-07-20 10:26:52 +00:00
return err
}
defer file.Close()
err = json.NewDecoder(file).Decode(&globalConf)
if err != nil {
logger.Warn(logSender, "error parsing config file: %v. Default configuration will be used: %+v", err, globalConf)
2019-07-20 10:26:52 +00:00
return err
}
if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
globalConf.SFTPD.Banner = defaultBanner
}
2019-07-20 10:26:52 +00:00
logger.Debug(logSender, "config loaded: %+v", globalConf)
return err
}