1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-26 15:42:27 -05: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:
Aaron Lindsay 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)

10
db.go
View File

@ -2,15 +2,17 @@ package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/gorp.v1"
"log"
)
var DB *gorp.DbMap = initDB()
var DB *gorp.DbMap
func initDB() *gorp.DbMap {
db, err := sql.Open("sqlite3", "file:moneygo.sqlite?cache=shared&mode=rwc")
func initDB(cfg *Config) {
db, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN)
if err != nil {
log.Fatal(err)
}
@ -30,5 +32,5 @@ func initDB() *gorp.DbMap {
log.Fatal(err)
}
return dbmap
DB = dbmap
}

View File

@ -9,3 +9,17 @@ port = 8080
# Base directory for serving files out of. This should point to the root of the
# moneygo source directory
base-directory = src/github.com/aclindsa/moneygo/
# Type of database being used (sqlite3, mysql, postgres)
db-type = sqlite3
# 'Data Source Name' for the database being used. This is driver-specific. See
# the following examples and external resources for more information about
# configuring this for your particular database configuration:
#
# Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc"
# MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name
# example DSN: "user:password@localhost/dbname"
# Postgres documentation: https://godoc.org/github.com/lib/pq
# example DSN: "postgres://user:password@localhost/dbname"
db-dsn = file:moneygo.sqlite?cache=shared&mode=rwc

10
main.go
View File

@ -27,7 +27,7 @@ func init() {
log.Fatal(err)
}
static_path := path.Join(config.MoneyGo.Base_directory, "static")
static_path := path.Join(config.MoneyGo.Basedir, "static")
// Ensure base directory is valid
dir_err_str := "The base directory doesn't look like it contains the " +
@ -44,14 +44,16 @@ func init() {
// Setup the logging flags to be printed
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
initDB(config)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(config.MoneyGo.Base_directory, "static/index.html"))
http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, "static/index.html"))
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(config.MoneyGo.Base_directory, r.URL.Path))
http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, r.URL.Path))
}
func main() {
@ -72,7 +74,7 @@ func main() {
log.Fatal(err)
}
log.Printf("Serving on port %d out of directory: %s", config.MoneyGo.Port, config.MoneyGo.Base_directory)
log.Printf("Serving on port %d out of directory: %s", config.MoneyGo.Port, config.MoneyGo.Basedir)
if config.MoneyGo.Fcgi {
fcgi.Serve(listener, context.ClearHandler(servemux))
} else {