1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00
mxpv-podsync/cmd/app/main.go

102 lines
2.2 KiB
Go
Raw Normal View History

2017-07-19 19:40:44 -07:00
package main
2017-07-22 22:16:05 -07:00
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
2017-08-19 16:58:23 -07:00
"github.com/mxpv/podsync/pkg/api"
"github.com/mxpv/podsync/pkg/builders"
"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/feeds"
2017-10-28 01:11:49 -07:00
"github.com/mxpv/podsync/pkg/handler"
2017-11-04 17:27:01 -07:00
"github.com/mxpv/podsync/pkg/stats"
2018-11-24 11:58:08 -08:00
"github.com/mxpv/podsync/pkg/storage"
2017-11-02 18:01:35 -07:00
"github.com/mxpv/podsync/pkg/support"
2018-12-02 13:58:41 -08:00
log "github.com/sirupsen/logrus"
2017-07-22 22:16:05 -07:00
)
2017-07-19 19:40:44 -07:00
func main() {
2017-07-22 22:16:05 -07:00
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2018-11-24 11:58:08 -08:00
// Create core services
cfg, err := config.ReadConfiguration()
if err != nil {
2018-12-02 13:58:41 -08:00
log.WithError(err).Fatal("failed to read configuration")
}
2018-11-24 11:58:08 -08:00
database, err := storage.NewPG(cfg.PostgresConnectionURL, true)
2017-10-30 17:26:46 -07:00
if err != nil {
2018-12-02 13:58:41 -08:00
log.WithError(err).Fatal("failed to create pg")
2017-10-30 17:26:46 -07:00
}
2017-11-04 17:27:01 -07:00
statistics, err := stats.NewRedisStats(cfg.RedisURL)
if err != nil {
2018-12-02 13:58:41 -08:00
log.WithError(err).Fatal("failed to create redis")
2017-11-04 17:27:01 -07:00
}
2017-11-02 18:19:21 -07:00
patreon := support.NewPatreon(database)
// Builders
youtube, err := builders.NewYouTubeBuilder(cfg.YouTubeApiKey)
if err != nil {
2018-12-02 13:58:41 -08:00
log.WithError(err).Fatal("failed to create YouTube builder")
}
vimeo, err := builders.NewVimeoBuilder(ctx, cfg.VimeoApiKey)
if err != nil {
2018-12-02 13:58:41 -08:00
log.WithError(err).Fatal("failed to create Vimeo builder")
}
2017-11-03 17:19:44 -07:00
feed, err := feeds.NewFeedService(
2018-11-24 11:58:08 -08:00
feeds.WithStorage(database),
2017-11-04 17:27:01 -07:00
feeds.WithStats(statistics),
2017-11-03 15:04:33 -07:00
feeds.WithBuilder(api.ProviderYoutube, youtube),
feeds.WithBuilder(api.ProviderVimeo, vimeo),
)
2017-11-03 17:19:44 -07:00
if err != nil {
panic(err)
}
2017-07-22 22:16:05 -07:00
srv := http.Server{
2017-08-20 20:28:08 -07:00
Addr: fmt.Sprintf(":%d", 5001),
2017-11-02 18:01:35 -07:00
Handler: handler.New(feed, patreon, cfg),
2017-07-22 22:16:05 -07:00
}
go func() {
2018-12-02 13:58:41 -08:00
log.Infof("running listener at %s", srv.Addr)
2017-07-22 22:16:05 -07:00
if err := srv.ListenAndServe(); err != nil {
2018-12-02 13:58:41 -08:00
log.WithError(err).Error("failed to listen")
2017-07-22 22:16:05 -07:00
}
}()
<-stop
2018-12-02 13:58:41 -08:00
log.Info("shutting down server")
2017-07-22 22:16:05 -07:00
2018-12-02 13:58:41 -08:00
if err := srv.Shutdown(ctx); err != nil {
log.WithError(err).Error("server shutdown failed")
}
if err := database.Close(); err != nil {
log.WithError(err).Error("failed to close database")
}
if err := statistics.Close(); err != nil {
log.WithError(err).Error("failed to close stats storage")
}
2017-07-22 22:16:05 -07:00
2018-12-02 13:58:41 -08:00
log.Info("server gracefully stopped")
2017-07-19 19:40:44 -07:00
}