2016-08-13 11:03:03 +02:00
|
|
|
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"
|
2016-08-14 13:33:53 +02:00
|
|
|
"github.com/rs/cors"
|
2016-08-13 11:03:03 +02:00
|
|
|
|
|
|
|
libconfig "dev.sum7.de/sum7/warehost/config"
|
2016-08-14 15:29:54 +02:00
|
|
|
log "dev.sum7.de/sum7/warehost/lib/log"
|
2016-08-13 11:03:03 +02:00
|
|
|
"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)
|
2016-08-14 15:29:54 +02:00
|
|
|
log.NewLogger(config.Log.Path)
|
2016-08-13 11:03:03 +02:00
|
|
|
|
2016-08-14 13:33:53 +02:00
|
|
|
// Session mgmt
|
2016-08-13 11:03:03 +02:00
|
|
|
sessions, _ = session.NewManager("memory", "session", 3600)
|
|
|
|
go sessions.GC()
|
|
|
|
|
2016-08-14 13:33:53 +02:00
|
|
|
// Main Databaseconnection
|
2016-08-13 11:03:03 +02:00
|
|
|
dbconnection, err = xorm.NewEngine("postgres", config.Database)
|
|
|
|
if err != nil {
|
2016-08-14 18:29:25 +02:00
|
|
|
log.Log.Fatal("database connection: ", err)
|
2016-08-13 11:03:03 +02:00
|
|
|
}
|
|
|
|
defer dbconnection.Close()
|
2016-08-14 13:33:53 +02:00
|
|
|
dbconnection.ShowSQL(config.DatabaseDebug)
|
|
|
|
//load system Models to database
|
2016-08-13 11:03:03 +02:00
|
|
|
system.SyncModels(dbconnection)
|
|
|
|
|
2016-08-14 13:33:53 +02:00
|
|
|
// API routes
|
2016-08-13 11:03:03 +02:00
|
|
|
router := httprouter.New()
|
|
|
|
system.NewAPI(config, sessions, dbconnection, router, "")
|
|
|
|
|
2016-08-14 13:33:53 +02:00
|
|
|
// Fallback filesystem
|
2016-08-13 11:03:03 +02:00
|
|
|
if config.Webroot != "" {
|
|
|
|
router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot)))
|
|
|
|
}
|
2016-08-14 13:33:53 +02:00
|
|
|
|
|
|
|
// 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
|
2016-08-13 11:03:03 +02:00
|
|
|
address := net.JoinHostPort(config.API.Address, config.API.Port)
|
2016-08-14 15:29:54 +02:00
|
|
|
log.Log.Info("starting api on ", address)
|
2016-08-13 11:03:03 +02:00
|
|
|
// TODO bad
|
2016-08-14 15:29:54 +02:00
|
|
|
log.Log.Fatal(http.ListenAndServe(address, handler))
|
2016-08-13 11:03:03 +02:00
|
|
|
}
|