mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-09-21 04:10:05 -04:00
Aaron Lindsay
c783e2c1bb
It was being used for session management, but we weren't using any of the features that differentiated it from using go's cookies directly so it is hard to justify the additional dependencies.
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
package main
|
|
|
|
//go:generate make
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"net/http/fcgi"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
)
|
|
|
|
var configFile string
|
|
var config *Config
|
|
|
|
func init() {
|
|
var err error
|
|
flag.StringVar(&configFile, "config", "/etc/moneygo/config.ini", "Path to config file")
|
|
flag.Parse()
|
|
|
|
config, err = readConfig(configFile)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
static_path := path.Join(config.MoneyGo.Basedir, "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 your config file contains the" +
|
|
"right path for 'base-directory'."
|
|
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)
|
|
}
|
|
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, "static/index.html"))
|
|
}
|
|
|
|
func staticHandler(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, path.Join(config.MoneyGo.Basedir, r.URL.Path))
|
|
}
|
|
|
|
func main() {
|
|
servemux := http.NewServeMux()
|
|
servemux.HandleFunc("/", rootHandler)
|
|
servemux.HandleFunc("/static/", staticHandler)
|
|
servemux.HandleFunc("/session/", SessionHandler)
|
|
servemux.HandleFunc("/user/", UserHandler)
|
|
servemux.HandleFunc("/security/", SecurityHandler)
|
|
servemux.HandleFunc("/securitytemplate/", SecurityTemplateHandler)
|
|
servemux.HandleFunc("/account/", AccountHandler)
|
|
servemux.HandleFunc("/transaction/", TransactionHandler)
|
|
servemux.HandleFunc("/import/gnucash", GnucashImportHandler)
|
|
servemux.HandleFunc("/report/", ReportHandler)
|
|
|
|
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", config.MoneyGo.Port, config.MoneyGo.Basedir)
|
|
if config.MoneyGo.Fcgi {
|
|
fcgi.Serve(listener, servemux)
|
|
} else {
|
|
http.Serve(listener, servemux)
|
|
}
|
|
}
|