package main import ( "flag" "net" "net/http" "github.com/NYTimes/gziphandler" "github.com/astaxie/session" _ "github.com/astaxie/session/providers/memory" "github.com/go-xorm/xorm" "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" "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 *xorm.Engine 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 = xorm.NewEngine("postgres", config.Database) if err != nil { log.Log.Fatal("database connection: ", err) } defer dbconnection.Close() dbconnection.ShowSQL(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)) }