package main import ( "flag" "net" "net/http" "github.com/NYTimes/gziphandler" "github.com/astaxie/session" _ "github.com/astaxie/session/providers/memory" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" "github.com/julienschmidt/httprouter" "github.com/rs/cors" libconfig "dev.sum7.de/sum7/warehost/config" log "dev.sum7.de/sum7/warehost/lib/log" "dev.sum7.de/sum7/warehost/system" ) var ( configFile string config *libconfig.Config dbconnection *gorm.DB sessions *session.Manager ) func main() { var err error flag.StringVar(&configFile, "c", "config.yml", "path of configuration file") flag.Parse() config = libconfig.ReadConfigFile(configFile) log.NewLogger(config.Log.Path) // Session mgmt sessions, _ = session.NewManager("memory", "session", 3600) go sessions.GC() // Main Databaseconnection dbconnection, err = gorm.Open("postgres", config.Database) if err != nil { log.Log.Fatal("database connection: ", err) } defer dbconnection.Close() dbconnection.SingularTable(true) dbconnection.LogMode(config.DatabaseDebug) //load system Models to database system.SyncModels(dbconnection) // API routes router := httprouter.New() system.NewAPI(config, sessions, dbconnection, router, "") // Fallback filesystem if config.Webroot != "" { router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot))) } // Manage CORS (JsonOutput allow requested -> lib/api) c := cors.New(cors.Options{ AllowedOrigins: []string{config.API.AllowedOrigins}, AllowCredentials: true, }) handler := c.Handler(router) // Start server address := net.JoinHostPort(config.API.Address, config.API.Port) log.Log.Info("starting api on ", address) // TODO bad log.Log.Fatal(http.ListenAndServe(address, handler)) }