moneygo/main.go

103 lines
2.6 KiB
Go
Raw Normal View History

2015-06-25 22:36:58 -04:00
package main
2016-02-13 17:13:58 -05:00
//go:generate make
2015-06-25 22:36:58 -04:00
import (
"flag"
"gopkg.in/gorp.v1"
2015-06-25 22:36:58 -04:00
"log"
"net"
"net/http"
"net/http/fcgi"
"os"
"path"
"strconv"
)
2017-10-01 21:15:40 -04:00
var configFile string
var config *Config
2015-06-25 22:36:58 -04:00
func init() {
2017-10-01 21:15:40 -04:00
var err error
flag.StringVar(&configFile, "config", "/etc/moneygo/config.ini", "Path to config file")
2015-06-25 22:36:58 -04:00
flag.Parse()
2017-10-01 21:15:40 -04:00
config, err = readConfig(configFile)
if err != nil {
log.Fatal(err)
}
static_path := path.Join(config.MoneyGo.Basedir, "static")
2015-06-25 22:36:58 -04:00
// Ensure base directory is valid
dir_err_str := "The base directory doesn't look like it contains the " +
2017-10-01 21:15:40 -04:00
"'static' directory. Check to make sure your config file contains the" +
2017-10-02 21:01:23 -04:00
"right path for 'base-directory'."
2015-06-25 22:36:58 -04:00
static_dir, err := os.Stat(static_path)
if err != nil {
log.Print(err)
log.Fatal(dir_err_str)
}
if !static_dir.IsDir() {
log.Fatal(dir_err_str)
}
// Setup the logging flags to be printed
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
2015-06-25 22:36:58 -04:00
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, "static/index.html"))
2015-06-25 22:36:58 -04:00
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, r.URL.Path))
2015-06-25 22:36:58 -04:00
}
// Create a closure over db, allowing the handlers to look like a
// http.HandlerFunc
type DB = gorp.DbMap
type DBHandler func(http.ResponseWriter, *http.Request, *DB)
func DBHandlerFunc(h DBHandler, db *DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h(w, r, db)
}
}
func GetHandler(db *DB) http.Handler {
2015-06-25 22:36:58 -04:00
servemux := http.NewServeMux()
servemux.HandleFunc("/", rootHandler)
servemux.HandleFunc("/static/", staticHandler)
servemux.HandleFunc("/session/", DBHandlerFunc(SessionHandler, db))
servemux.HandleFunc("/user/", DBHandlerFunc(UserHandler, db))
servemux.HandleFunc("/security/", DBHandlerFunc(SecurityHandler, db))
servemux.HandleFunc("/securitytemplate/", SecurityTemplateHandler)
servemux.HandleFunc("/account/", DBHandlerFunc(AccountHandler, db))
servemux.HandleFunc("/transaction/", DBHandlerFunc(TransactionHandler, db))
servemux.HandleFunc("/import/gnucash", DBHandlerFunc(GnucashImportHandler, db))
servemux.HandleFunc("/report/", DBHandlerFunc(ReportHandler, db))
return servemux
}
func main() {
database, err := initDB(config)
if err != nil {
log.Fatal(err)
}
handler := GetHandler(database)
2015-06-25 22:36:58 -04:00
2017-10-01 21:15:40 -04:00
listener, err := net.Listen("tcp", ":"+strconv.Itoa(config.MoneyGo.Port))
2015-06-25 22:36:58 -04:00
if err != nil {
log.Fatal(err)
}
log.Printf("Serving on port %d out of directory: %s", config.MoneyGo.Port, config.MoneyGo.Basedir)
2017-10-01 21:15:40 -04:00
if config.MoneyGo.Fcgi {
fcgi.Serve(listener, handler)
2015-06-25 22:36:58 -04:00
} else {
http.Serve(listener, handler)
2015-06-25 22:36:58 -04:00
}
}