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)
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:55:26 -04:00
|
|
|
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)
|
|
|
|
}
|
2016-02-19 19:59:31 -05:00
|
|
|
|
|
|
|
// Setup the logging flags to be printed
|
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
2017-10-02 20:55:26 -04:00
|
|
|
|
|
|
|
initDB(config)
|
2015-06-25 22:36:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
2017-10-02 20:55:26 -04:00
|
|
|
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) {
|
2017-10-02 20:55:26 -04:00
|
|
|
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)
|
2016-10-16 08:19:11 -04:00
|
|
|
servemux.HandleFunc("/securitytemplate/", SecurityTemplateHandler)
|
2015-06-27 17:46:06 -04:00
|
|
|
servemux.HandleFunc("/account/", AccountHandler)
|
2015-06-28 23:03:34 -04:00
|
|
|
servemux.HandleFunc("/transaction/", TransactionHandler)
|
2016-02-15 11:28:44 -05:00
|
|
|
servemux.HandleFunc("/import/gnucash", GnucashImportHandler)
|
2017-01-23 20:40:39 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:55:26 -04:00
|
|
|
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 {
|
2017-10-03 11:24:07 -04:00
|
|
|
fcgi.Serve(listener, servemux)
|
2015-06-25 22:36:58 -04:00
|
|
|
} else {
|
2017-10-03 11:24:07 -04:00
|
|
|
http.Serve(listener, servemux)
|
2015-06-25 22:36:58 -04:00
|
|
|
}
|
|
|
|
}
|