moneygo/main.go

83 lines
2.0 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"
"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)
initDB(config)
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
}
func main() {
servemux := http.NewServeMux()
servemux.HandleFunc("/", rootHandler)
servemux.HandleFunc("/static/", staticHandler)
servemux.HandleFunc("/session/", SessionHandler)
servemux.HandleFunc("/user/", UserHandler)
2015-06-27 08:31:38 -04:00
servemux.HandleFunc("/security/", SecurityHandler)
servemux.HandleFunc("/securitytemplate/", SecurityTemplateHandler)
2015-06-27 17:46:06 -04:00
servemux.HandleFunc("/account/", AccountHandler)
servemux.HandleFunc("/transaction/", TransactionHandler)
servemux.HandleFunc("/import/gnucash", GnucashImportHandler)
servemux.HandleFunc("/report/", ReportHandler)
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, servemux)
2015-06-25 22:36:58 -04:00
} else {
http.Serve(listener, servemux)
2015-06-25 22:36:58 -04:00
}
}