2017-07-19 19:40:44 -07:00
|
|
|
package main
|
|
|
|
|
2017-07-22 22:16:05 -07:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2017-08-13 17:39:30 -07:00
|
|
|
|
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"
|
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
|
2017-08-13 17:39:30 -07:00
|
|
|
|
|
|
|
cfg, err := config.ReadConfiguration()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-11-04 17:27:01 -07:00
|
|
|
statistics, err := stats.NewRedisStats(cfg.RedisURL)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-11-02 18:19:21 -07:00
|
|
|
patreon := support.NewPatreon(database)
|
|
|
|
|
2017-08-13 17:39:30 -07:00
|
|
|
// Builders
|
|
|
|
|
|
|
|
youtube, err := builders.NewYouTubeBuilder(cfg.YouTubeApiKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vimeo, err := builders.NewVimeoBuilder(ctx, cfg.VimeoApiKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
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-08-13 17:39:30 -07:00
|
|
|
)
|
|
|
|
|
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() {
|
|
|
|
log.Println("running listener")
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-stop
|
|
|
|
|
|
|
|
log.Printf("shutting down server")
|
|
|
|
|
2018-11-24 11:58:08 -08:00
|
|
|
_ = srv.Shutdown(ctx)
|
|
|
|
_ = database.Close()
|
|
|
|
_ = statistics.Close()
|
2017-07-22 22:16:05 -07:00
|
|
|
|
|
|
|
log.Printf("server gracefully stopped")
|
2017-07-19 19:40:44 -07:00
|
|
|
}
|