97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/auth"
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/backend"
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/config"
|
|
extmedias "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/external_medias"
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/web"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
_ "github.com/lib/pq"
|
|
"github.com/phyber/negroni-gzip/gzip"
|
|
"github.com/robfig/cron"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/negroni"
|
|
)
|
|
|
|
func main() {
|
|
var cfgPath string
|
|
cfgPath = os.Getenv("CONFIG_FILE")
|
|
if cfgPath == "" {
|
|
cfgPath = "./config.yml"
|
|
}
|
|
|
|
// Setup the logger
|
|
logger := logrus.New()
|
|
logger.Formatter = &logrus.TextFormatter{FullTimestamp: true}
|
|
logger.Level = logrus.DebugLevel
|
|
|
|
log := logrus.NewEntry(logger)
|
|
cf, err := config.Load(cfgPath, log)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
// Connect to the database
|
|
db, err := sqlx.Connect("postgres", cf.PGDSN)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
backend := &backend.Backend{Database: db}
|
|
|
|
// Generate auth params
|
|
authParams := auth.Params{
|
|
Backend: backend,
|
|
Pepper: cf.Authorizer.Pepper,
|
|
Cost: cf.Authorizer.Cost,
|
|
Secret: cf.Authorizer.Secret,
|
|
}
|
|
authorizer := auth.New(authParams)
|
|
|
|
// Create web environment needed by the app
|
|
env := web.NewEnv(web.EnvParams{
|
|
Database: db,
|
|
Auth: authorizer,
|
|
Log: log,
|
|
Config: cf,
|
|
Backend: web.DetailerTorrenter{
|
|
Torrenter: backend,
|
|
Detailer: backend,
|
|
},
|
|
})
|
|
|
|
authMiddleware := auth.NewMiddleware(env.Auth, log)
|
|
|
|
// Setup the routes
|
|
setupRoutes(env)
|
|
|
|
// Create the cron object
|
|
c := cron.New()
|
|
|
|
// Refresh the library every 6h
|
|
c.AddFunc("@every 6h", func() {
|
|
env.Log.Infof("Running refresh cron!")
|
|
extmedias.Refresh(env)
|
|
})
|
|
|
|
// Start the cron
|
|
c.Start()
|
|
|
|
// Stop the cron
|
|
defer c.Stop()
|
|
|
|
n := negroni.Classic()
|
|
// Middleware for authentication
|
|
n.Use(authMiddleware)
|
|
// Serve static files
|
|
n.Use(negroni.NewStatic(http.Dir(cf.PublicDir)))
|
|
// Compress responses
|
|
n.Use(gzip.Gzip(gzip.DefaultCompression))
|
|
n.UseHandler(env.Router)
|
|
n.Run(":" + cf.Port)
|
|
}
|