1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-26 15:42:27 -05:00

Move to using config file

This commit is contained in:
Aaron Lindsay 2017-10-01 21:15:40 -04:00
parent debd221796
commit f88fed966b
4 changed files with 55 additions and 30 deletions

View File

@ -65,12 +65,12 @@ the `go generate ...` command.
Assuming you're in the same directory you ran the above installation commands
from, running MoneyGo is then as easy as:
$ ./bin/moneygo \
-port 8080 \
-base src/github.com/aclindsa/moneygo/
$ ./bin/moneygo -config src/github.com/aclindsa/moneygo/example_config.ini
You should then be able to explore MoneyGo by visiting http://localhost:8080 in
your browser.
your browser. Editing the configuration file supplied will allow you to modify
several settings including the port used and whether to serve via FastCGI
instead of HTTP (the default).
## Missing Features

23
config.go Normal file
View File

@ -0,0 +1,23 @@
package main
import (
"fmt"
"gopkg.in/gcfg.v1"
)
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
}
}
func readConfig(filename string) (*Config, error) {
var cfg Config
err := gcfg.ReadFileInto(&cfg, filename)
if err != nil {
return nil, fmt.Errorf("Failed to parse config file: %s", err)
}
return &cfg, nil
}

11
example_config.ini Normal file
View File

@ -0,0 +1,11 @@
[moneygo]
# Whether to serve as FastCGI (default is false, for HTTP)
fcgi = false
# Port to serve FCGI or HTTP on
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/

43
main.go
View File

@ -14,34 +14,25 @@ import (
"strconv"
)
var serveFcgi bool
var baseDir string
var tmpDir string
var port int
var smtpServer string
var smtpPort int
var smtpUsername string
var smtpPassword string
var reminderEmail string
var configFile string
var config *Config
func init() {
flag.StringVar(&baseDir, "base", "./", "Base directory for server")
flag.StringVar(&tmpDir, "tmp", "/tmp", "Directory to create temporary files in")
flag.IntVar(&port, "port", 80, "Port to serve API/files on")
flag.StringVar(&smtpServer, "smtp.server", "smtp.example.com", "SMTP server to send reminder emails from.")
flag.IntVar(&smtpPort, "smtp.port", 587, "SMTP server port to connect to")
flag.StringVar(&smtpUsername, "smtp.username", "moneygo", "SMTP username")
flag.StringVar(&smtpPassword, "smtp.password", "password", "SMTP password")
flag.StringVar(&reminderEmail, "email", "moneygo@example.com", "Email address to send reminder emails as.")
flag.BoolVar(&serveFcgi, "fcgi", false, "Serve via fcgi rather than http.")
var err error
flag.StringVar(&configFile, "config", "/etc/moneygo/config.ini", "Path to config file")
flag.Parse()
static_path := path.Join(baseDir, "static")
config, err = readConfig(configFile)
if err != nil {
log.Fatal(err)
}
static_path := path.Join(config.MoneyGo.Base_directory, "static")
// Ensure base directory is valid
dir_err_str := "The base directory doesn't look like it contains the " +
"'static' directory. Check to make sure you're passing the right " +
"value to the -base argument."
"'static' directory. Check to make sure your config file contains the" +
"right path for 'base_directory'."
static_dir, err := os.Stat(static_path)
if err != nil {
log.Print(err)
@ -56,11 +47,11 @@ func init() {
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(baseDir, "static/index.html"))
http.ServeFile(w, r, path.Join(config.MoneyGo.Base_directory, "static/index.html"))
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(baseDir, r.URL.Path))
http.ServeFile(w, r, path.Join(config.MoneyGo.Base_directory, r.URL.Path))
}
func main() {
@ -76,13 +67,13 @@ func main() {
servemux.HandleFunc("/import/gnucash", GnucashImportHandler)
servemux.HandleFunc("/report/", ReportHandler)
listener, err := net.Listen("tcp", ":"+strconv.Itoa(port))
listener, err := net.Listen("tcp", ":"+strconv.Itoa(config.MoneyGo.Port))
if err != nil {
log.Fatal(err)
}
log.Printf("Serving on port %d out of directory: %s", port, baseDir)
if serveFcgi {
log.Printf("Serving on port %d out of directory: %s", config.MoneyGo.Port, config.MoneyGo.Base_directory)
if config.MoneyGo.Fcgi {
fcgi.Serve(listener, context.ClearHandler(servemux))
} else {
http.Serve(listener, context.ClearHandler(servemux))