54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"log"
|
||
|
"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"
|
||
|
|
||
|
libconfig "dev.sum7.de/sum7/warehost/config"
|
||
|
"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)
|
||
|
|
||
|
sessions, _ = session.NewManager("memory", "session", 3600)
|
||
|
go sessions.GC()
|
||
|
|
||
|
dbconnection, err = xorm.NewEngine("postgres", config.Database)
|
||
|
if err != nil {
|
||
|
log.Fatal("[system] Error database connection: ", err)
|
||
|
}
|
||
|
defer dbconnection.Close()
|
||
|
system.SyncModels(dbconnection)
|
||
|
|
||
|
router := httprouter.New()
|
||
|
system.NewAPI(config, sessions, dbconnection, router, "")
|
||
|
|
||
|
if config.Webroot != "" {
|
||
|
router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot)))
|
||
|
}
|
||
|
address := net.JoinHostPort(config.API.Address, config.API.Port)
|
||
|
log.Println("starting webserver on", address)
|
||
|
// TODO bad
|
||
|
log.Fatal(http.ListenAndServe(address, router))
|
||
|
}
|