1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-10-31 16:00:05 -04:00

Merge pull request #18 from aclindsa/database_config

Move database configuration into config file
This commit is contained in:
Aaron Lindsay 2017-10-02 20:58:40 -04:00 committed by GitHub
commit c1f6919245
4 changed files with 87 additions and 14 deletions

View File

@ -1,20 +1,75 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"gopkg.in/gcfg.v1" "gopkg.in/gcfg.v1"
"strings"
) )
type Config struct { type DbType uint
MoneyGo struct {
Fcgi bool // whether to serve FCGI (HTTP by default if false) const (
Base_directory string // base directory for serving files out of SQLite DbType = 1 + iota
Port int // port to serve API/files on 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) { 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) err := gcfg.ReadFileInto(&cfg, filename)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to parse config file: %s", err) return nil, fmt.Errorf("Failed to parse config file: %s", err)

10
db.go
View File

@ -2,15 +2,17 @@ package main
import ( import (
"database/sql" "database/sql"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"gopkg.in/gorp.v1" "gopkg.in/gorp.v1"
"log" "log"
) )
var DB *gorp.DbMap = initDB() var DB *gorp.DbMap
func initDB() *gorp.DbMap { func initDB(cfg *Config) {
db, err := sql.Open("sqlite3", "file:moneygo.sqlite?cache=shared&mode=rwc") db, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -30,5 +32,5 @@ func initDB() *gorp.DbMap {
log.Fatal(err) 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 # Base directory for serving files out of. This should point to the root of the
# moneygo source directory # moneygo source directory
base-directory = src/github.com/aclindsa/moneygo/ 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) 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 // Ensure base directory is valid
dir_err_str := "The base directory doesn't look like it contains the " + 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 // Setup the logging flags to be printed
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
initDB(config)
} }
func rootHandler(w http.ResponseWriter, r *http.Request) { 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) { 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() { func main() {
@ -72,7 +74,7 @@ func main() {
log.Fatal(err) 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 { if config.MoneyGo.Fcgi {
fcgi.Serve(listener, context.ClearHandler(servemux)) fcgi.Serve(listener, context.ClearHandler(servemux))
} else { } else {