1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2025-07-03 04:38:38 -04:00

Move database configuration into config file

Also support mysql and postgres (at least in name, they haven't been
fully tested yet)
This commit is contained in:
2017-10-02 20:55:26 -04:00
parent 319b0b147d
commit 8ce3ef1bf5
4 changed files with 87 additions and 14 deletions

View File

@ -1,20 +1,75 @@
package main
import (
"errors"
"fmt"
"gopkg.in/gcfg.v1"
"strings"
)
type Config struct {
MoneyGo struct {
Fcgi bool // whether to serve FCGI (HTTP by default if false)
Base_directory string // base directory for serving files out of
Port int // port to serve API/files on
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
}
}
*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
}
type Config struct {
MoneyGo MoneyGo
}
func readConfig(filename string) (*Config, error) {
var cfg Config
cfg := Config{
MoneyGo: MoneyGo{
Fcgi: false,
Port: 80,
Basedir: "src/github.com/aclindsa/moneygo/",
DBType: SQLite,
DSN: "file:moneygo.sqlite?cache=shared&mode=rwc",
},
}
err := gcfg.ReadFileInto(&cfg, filename)
if err != nil {
return nil, fmt.Errorf("Failed to parse config file: %s", err)