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 (
2017-10-04 19:35:59 -04:00
"database/sql"
2015-06-25 22:36:58 -04:00
"flag"
2017-10-04 19:35:59 -04:00
"github.com/aclindsa/moneygo/internal/config"
"github.com/aclindsa/moneygo/internal/db"
"github.com/aclindsa/moneygo/internal/handlers"
2017-12-05 20:56:57 -05:00
"github.com/kabukky/httpscerts"
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
2017-10-04 19:35:59 -04:00
var cfg * 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-04 19:35:59 -04:00
cfg , err = config . ReadConfig ( configFile )
2017-10-01 21:15:40 -04:00
if err != nil {
log . Fatal ( err )
}
2017-10-04 19:35:59 -04:00
static_path := path . Join ( cfg . 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 )
2015-06-25 22:36:58 -04:00
}
2017-10-04 19:35:59 -04:00
type FileHandler func ( http . ResponseWriter , * http . Request , string )
2015-06-25 22:36:58 -04:00
2017-10-04 19:35:59 -04:00
func FileHandlerFunc ( h FileHandler , basedir string ) http . HandlerFunc {
2017-10-04 08:05:51 -04:00
return func ( w http . ResponseWriter , r * http . Request ) {
2017-10-04 19:35:59 -04:00
h ( w , r , basedir )
2017-10-04 08:05:51 -04:00
}
}
2017-10-04 19:35:59 -04:00
func rootHandler ( w http . ResponseWriter , r * http . Request , basedir string ) {
http . ServeFile ( w , r , path . Join ( basedir , "static/index.html" ) )
}
func staticHandler ( w http . ResponseWriter , r * http . Request , basedir string ) {
http . ServeFile ( w , r , path . Join ( basedir , r . URL . Path ) )
2017-10-04 08:05:51 -04:00
}
func main ( ) {
2017-10-30 21:06:27 -04:00
dsn := db . GetDSN ( cfg . MoneyGo . DBType , cfg . MoneyGo . DSN )
2017-10-24 20:57:55 -04:00
database , err := sql . Open ( cfg . MoneyGo . DBType . String ( ) , dsn )
2017-10-04 08:05:51 -04:00
if err != nil {
log . Fatal ( err )
}
2017-10-04 19:35:59 -04:00
defer database . Close ( )
2017-10-05 08:06:08 -04:00
dbmap , err := db . GetDbMap ( database , cfg . MoneyGo . DBType )
2017-10-04 19:35:59 -04:00
if err != nil {
log . Fatal ( err )
}
// Get ServeMux for API and add our own handlers for files
2017-11-12 20:17:27 -05:00
servemux := http . NewServeMux ( )
servemux . Handle ( "/v1/" , & handlers . APIHandler { DB : dbmap } )
2017-10-04 19:35:59 -04:00
servemux . HandleFunc ( "/" , FileHandlerFunc ( rootHandler , cfg . MoneyGo . Basedir ) )
servemux . HandleFunc ( "/static/" , FileHandlerFunc ( staticHandler , cfg . MoneyGo . Basedir ) )
2015-06-25 22:36:58 -04:00
2017-10-04 19:35:59 -04:00
listener , err := net . Listen ( "tcp" , ":" + strconv . Itoa ( cfg . MoneyGo . Port ) )
2015-06-25 22:36:58 -04:00
if err != nil {
log . Fatal ( err )
}
2017-10-04 19:35:59 -04:00
if cfg . MoneyGo . Fcgi {
2017-12-05 20:56:57 -05:00
log . Printf ( "Serving via FCGI on port %d out of directory: %s" , cfg . MoneyGo . Port , cfg . MoneyGo . Basedir )
2017-10-04 19:35:59 -04:00
fcgi . Serve ( listener , servemux )
2015-06-25 22:36:58 -04:00
} else {
2017-12-05 20:56:57 -05:00
cert := cfg . Https . CertFile
key := cfg . Https . KeyFile
if err := httpscerts . Check ( cert , key ) ; err != nil {
if ! cfg . Https . GenerateCerts {
log . Fatalf ( "HTTPS certficates not found at '%s' and '%s'. If you would like for them to be auto-generated for you, specify 'generate-certs-if-absent = true' in your config file at '%s'" , cert , key , configFile )
}
err = httpscerts . Generate ( cert , key , cfg . Https . GenerateCertsHosts )
if err != nil {
log . Fatalf ( "Error: Generating HTTPS cert/key at '%s' and '%s' failed: %s" , cert , key , err )
}
}
log . Printf ( "Serving via HTTPS on port %d out of directory: %s" , cfg . MoneyGo . Port , cfg . MoneyGo . Basedir )
http . ServeTLS ( listener , servemux , cert , key )
2015-06-25 22:36:58 -04:00
}
}