yogai/service.go

68 lines
1.7 KiB
Go
Raw Normal View History

2023-05-06 12:08:34 +02:00
package main
import (
2023-05-10 20:08:45 +02:00
"context"
2023-05-10 16:28:45 +02:00
"ewintr.nl/yogai/fetcher"
2023-05-06 12:08:34 +02:00
"ewintr.nl/yogai/storage"
2023-05-10 19:27:31 +02:00
"golang.org/x/exp/slog"
2023-05-10 20:08:45 +02:00
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
2023-05-06 12:08:34 +02:00
"os"
2023-05-08 15:53:06 +02:00
"os/signal"
"time"
2023-05-06 12:08:34 +02:00
)
func main() {
2023-05-10 20:08:45 +02:00
ctx := context.Background()
2023-05-10 19:27:31 +02:00
logger := slog.New(slog.NewTextHandler(os.Stderr))
2023-05-08 15:53:06 +02:00
postgres, err := storage.NewPostgres(storage.PostgresInfo{
2023-05-06 12:08:34 +02:00
Host: getParam("POSTGRES_HOST", "localhost"),
Port: getParam("POSTGRES_PORT", "5432"),
User: getParam("POSTGRES_USER", "yogai"),
Password: getParam("POSTGRES_PASSWORD", "yogai"),
Database: getParam("POSTGRES_DB", "yogai"),
2023-05-08 15:53:06 +02:00
})
2023-05-06 12:08:34 +02:00
if err != nil {
2023-05-10 19:27:31 +02:00
logger.Error("unable to connect to postgres", err)
2023-05-06 12:08:34 +02:00
os.Exit(1)
}
2023-05-08 15:53:06 +02:00
videoRepo := storage.NewPostgresVideoRepository(postgres)
2023-05-10 16:28:45 +02:00
mflx := fetcher.NewMiniflux(fetcher.MinifluxInfo{
2023-05-08 15:53:06 +02:00
Endpoint: getParam("MINIFLUX_ENDPOINT", "http://localhost/v1"),
ApiKey: getParam("MINIFLUX_APIKEY", ""),
})
fetchInterval, err := time.ParseDuration(getParam("FETCH_INTERVAL", "1m"))
2023-05-06 12:08:34 +02:00
if err != nil {
2023-05-10 19:27:31 +02:00
logger.Error("unable to parse fetch interval", err)
2023-05-06 12:08:34 +02:00
os.Exit(1)
}
2023-05-06 14:03:14 +02:00
2023-05-10 20:08:45 +02:00
ytClient, err := youtube.NewService(ctx, option.WithAPIKey(getParam("YOUTUBE_API_KEY", "")))
if err != nil {
logger.Error("unable to create youtube service", err)
os.Exit(1)
}
yt := fetcher.NewYoutube(ytClient)
2023-05-13 12:53:37 +02:00
openAIClient := fetcher.NewOpenAI(getParam("OPENAI_API_KEY", ""))
fetcher := fetcher.NewFetch(videoRepo, mflx, fetchInterval, yt, openAIClient, logger)
2023-05-08 15:53:06 +02:00
go fetcher.Run()
2023-05-10 19:27:31 +02:00
logger.Info("service started")
2023-05-08 15:53:06 +02:00
done := make(chan os.Signal)
signal.Notify(done, os.Interrupt)
<-done
2023-05-10 19:27:31 +02:00
logger.Info("service stopped")
2023-05-06 12:08:34 +02:00
}
func getParam(param, def string) string {
if val, ok := os.LookupEnv(param); ok {
return val
}
return def
}