Return the video details embedded in the torrents This requires the eventers to have the app env
131 lines
3.0 KiB
Go
131 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
|
|
"git.quimbo.fr/odwrtw/canape/backend/auth"
|
|
"git.quimbo.fr/odwrtw/canape/backend/config"
|
|
extmedias "git.quimbo.fr/odwrtw/canape/backend/external_medias"
|
|
"git.quimbo.fr/odwrtw/canape/backend/models"
|
|
"git.quimbo.fr/odwrtw/canape/backend/ratings"
|
|
"git.quimbo.fr/odwrtw/canape/backend/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() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "error while running the app: %s\n", err.Error())
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
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 {
|
|
return err
|
|
}
|
|
|
|
// Connect to the database
|
|
db, err := sqlx.Connect("postgres", cf.PGDSN)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
backend := &models.Backend{
|
|
Database: db,
|
|
}
|
|
models.SetPublicDir(cf.PublicDir)
|
|
models.SetImgURLPrefix(cf.ImgURLPrefix)
|
|
|
|
// Generate auth params
|
|
authParams := auth.Params{
|
|
Pepper: cf.Authorizer.Pepper,
|
|
Cost: cf.Authorizer.Cost,
|
|
Secret: cf.Authorizer.Secret,
|
|
}
|
|
authorizer := auth.New(db, 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()
|
|
|
|
if cf.PeriodicRefresh.Enabled {
|
|
// Refresh the library every 6h
|
|
env.Log.Debugf("Running refresh cron every %s", cf.PeriodicRefresh.Interval)
|
|
if err := c.AddFunc(fmt.Sprintf("@every %s", cf.PeriodicRefresh.Interval), func() {
|
|
env.Log.Infof("Running refresh cron!")
|
|
extmedias.Refresh(env)
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
env.Log.Debugf("Running Imdb refresh cron every 24h")
|
|
if err := c.AddFunc(fmt.Sprintf("@every 24h"), func() {
|
|
env.Log.Infof("Running IMDB refresh cron!")
|
|
if err := ratings.Refresh(env); err != nil {
|
|
env.Log.Errorf("failed to refresh ratings: %s", err.Error())
|
|
}
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Start the cron
|
|
c.Start()
|
|
|
|
// Stop the cron
|
|
defer c.Stop()
|
|
|
|
n := negroni.Classic()
|
|
// Middleware for setting ips
|
|
n.Use(auth.NewIPMiddleware(log))
|
|
// Middleware for request id
|
|
n.Use(auth.NewRequestIDMiddleware(log))
|
|
// 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)
|
|
|
|
addr := ":" + cf.Port
|
|
env.Log.Infof("listening on %s", addr)
|
|
|
|
return http.ListenAndServe(addr, n)
|
|
}
|