2017-10-04 19:35:59 -04:00
|
|
|
package config
|
2017-10-01 21:15:40 -04:00
|
|
|
|
|
|
|
import (
|
2017-10-02 20:55:26 -04:00
|
|
|
"errors"
|
2017-10-01 21:15:40 -04:00
|
|
|
"fmt"
|
|
|
|
"gopkg.in/gcfg.v1"
|
2017-10-02 20:55:26 -04:00
|
|
|
"strings"
|
2017-10-01 21:15:40 -04:00
|
|
|
)
|
|
|
|
|
2017-10-02 20:55:26 -04:00
|
|
|
type DbType uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
SQLite DbType = 1 + iota
|
|
|
|
MySQL
|
|
|
|
Postgres
|
|
|
|
)
|
|
|
|
|
|
|
|
var dbTypes = [...]string{"sqlite3", "mysql", "postgres"}
|
|
|
|
|
|
|
|
func (e DbType) Valid() bool {
|
|
|
|
// This check is mostly out of paranoia, ensuring e != 0 should be
|
|
|
|
// sufficient
|
|
|
|
return e >= SQLite && e <= Postgres
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e DbType) String() string {
|
|
|
|
if e.Valid() {
|
|
|
|
return dbTypes[e-1]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("invalid DbType (%d)", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DbType) FromString(in string) error {
|
|
|
|
value := strings.TrimSpace(in)
|
|
|
|
|
|
|
|
for i, s := range dbTypes {
|
|
|
|
if s == value {
|
|
|
|
*e = DbType(i + 1)
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-01 21:15:40 -04:00
|
|
|
}
|
2017-10-02 20:55:26 -04:00
|
|
|
*e = 0
|
|
|
|
return errors.New("Invalid DbType: \"" + in + "\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DbType) UnmarshalText(text []byte) error {
|
|
|
|
return e.FromString(string(text))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MoneyGo struct {
|
|
|
|
Fcgi bool // whether to serve FCGI (HTTP by default if false)
|
|
|
|
Port int // port to serve API/files on
|
|
|
|
Basedir string `gcfg:"base-directory"` // base directory for serving files out of
|
|
|
|
DBType DbType `gcfg:"db-type"` // Whether this is a sqlite/mysql/postgresql database
|
|
|
|
DSN string `gcfg:"db-dsn"` // 'Data Source Name' for database connection
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:56:57 -05:00
|
|
|
type Https struct {
|
|
|
|
CertFile string `gcfg:"cert-file"`
|
|
|
|
KeyFile string `gcfg:"key-file"`
|
|
|
|
GenerateCerts bool `gcfg:"generate-certs-if-absent"` // Generate certificates if missing
|
|
|
|
GenerateCertsHosts string `gcfg:"generate-certs-hosts"` // Hostnames to generate certificates for if missing and GenerateCerts==true
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:55:26 -04:00
|
|
|
type Config struct {
|
|
|
|
MoneyGo MoneyGo
|
2017-12-05 20:56:57 -05:00
|
|
|
Https Https
|
2017-10-01 21:15:40 -04:00
|
|
|
}
|
|
|
|
|
2017-10-04 19:35:59 -04:00
|
|
|
func ReadConfig(filename string) (*Config, error) {
|
2017-10-02 20:55:26 -04:00
|
|
|
cfg := Config{
|
|
|
|
MoneyGo: MoneyGo{
|
|
|
|
Fcgi: false,
|
|
|
|
Port: 80,
|
|
|
|
Basedir: "src/github.com/aclindsa/moneygo/",
|
|
|
|
DBType: SQLite,
|
|
|
|
DSN: "file:moneygo.sqlite?cache=shared&mode=rwc",
|
|
|
|
},
|
2017-12-05 20:56:57 -05:00
|
|
|
Https: Https{
|
|
|
|
CertFile: "./cert.pem",
|
|
|
|
KeyFile: "./key.pem",
|
|
|
|
GenerateCerts: false,
|
|
|
|
GenerateCertsHosts: "localhost",
|
|
|
|
},
|
2017-10-02 20:55:26 -04:00
|
|
|
}
|
|
|
|
|
2017-10-01 21:15:40 -04:00
|
|
|
err := gcfg.ReadFileInto(&cfg, filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse config file: %s", err)
|
|
|
|
}
|
|
|
|
return &cfg, nil
|
|
|
|
}
|