clipper/backend/cmd/clipper/main.go

106 lines
2.4 KiB
Go
Raw Normal View History

2021-09-04 22:29:12 +00:00
package main
import (
2021-10-29 12:52:31 +00:00
"context"
2021-09-04 22:29:12 +00:00
"log"
2021-09-06 09:39:43 +00:00
"time"
2021-09-04 22:29:12 +00:00
2021-11-22 18:26:51 +00:00
"git.netflux.io/rob/clipper/config"
2021-12-07 19:58:11 +00:00
"git.netflux.io/rob/clipper/filestore"
2021-11-01 05:28:40 +00:00
"git.netflux.io/rob/clipper/generated/store"
2021-12-08 19:58:13 +00:00
"git.netflux.io/rob/clipper/media"
2021-09-25 17:00:19 +00:00
"git.netflux.io/rob/clipper/server"
2021-11-22 18:26:51 +00:00
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
2021-10-29 12:52:31 +00:00
"github.com/aws/aws-sdk-go-v2/service/s3"
2021-11-23 13:17:50 +00:00
"github.com/jackc/pgx/v4/pgxpool"
2021-10-29 12:52:31 +00:00
"github.com/kkdai/youtube/v2"
2021-12-07 19:58:11 +00:00
"go.uber.org/zap"
2021-09-04 22:29:12 +00:00
)
2021-09-05 05:24:51 +00:00
const (
2021-12-07 19:58:11 +00:00
defaultTimeout = 600 * time.Second
defaultURLExpiry = time.Hour
2021-09-05 05:24:51 +00:00
)
2021-09-06 09:39:43 +00:00
func main() {
2021-10-29 12:52:31 +00:00
ctx := context.Background()
2021-11-22 18:26:51 +00:00
config, err := config.NewFromEnv()
if err != nil {
log.Fatalf("error loading config: %v", err)
2021-11-06 20:52:47 +00:00
}
2021-11-22 18:26:51 +00:00
// Create a store
2021-11-23 13:17:50 +00:00
dbConn, err := pgxpool.Connect(ctx, config.DatabaseURL)
2021-10-29 12:52:31 +00:00
if err != nil {
log.Fatal(err)
}
2021-11-23 12:20:56 +00:00
store := store.New(dbConn)
2021-10-29 12:52:31 +00:00
2021-12-07 19:58:11 +00:00
// Create a Youtube client
var youtubeClient youtube.Client
// Create a logger
logger, err := buildLogger(config)
if err != nil {
log.Fatal(err)
}
defer logger.Sync()
2021-10-29 12:52:31 +00:00
2021-12-07 19:58:11 +00:00
// Create a file store
2021-12-08 19:58:13 +00:00
fileStore, err := buildFileStore(ctx, config, logger)
if err != nil {
log.Fatal(err)
}
2021-12-07 19:58:11 +00:00
log.Fatal(server.Start(server.Options{
Config: config,
Timeout: defaultTimeout,
Store: store,
YoutubeClient: &youtubeClient,
FileStore: fileStore,
Logger: logger,
}))
}
2021-09-04 22:29:12 +00:00
2021-12-07 19:58:11 +00:00
func buildLogger(c config.Config) (*zap.Logger, error) {
if c.Environment == config.EnvDevelopment {
return zap.NewDevelopment()
}
return zap.NewProduction()
2021-09-04 22:29:12 +00:00
}
2021-12-08 19:58:13 +00:00
func buildFileStore(ctx context.Context, c config.Config, logger *zap.Logger) (media.FileStore, error) {
if c.FileStore == config.S3Store {
logger.Info("Initializing S3 store")
// Create an Amazon S3 service s3Client
cfg, err := awsconfig.LoadDefaultConfig(
ctx,
awsconfig.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(c.AWSAccessKeyID, c.AWSSecretAccessKey, ""),
),
awsconfig.WithRegion(c.AWSRegion),
)
if err != nil {
log.Fatal(err)
}
s3Client := s3.NewFromConfig(cfg)
s3PresignClient := s3.NewPresignClient(s3Client)
// Create a file store
return filestore.NewS3FileStore(
filestore.S3API{
S3Client: s3Client,
S3PresignClient: s3PresignClient,
},
c.S3Bucket,
defaultURLExpiry,
logger.Sugar().Named("filestore"),
), nil
}
logger.Info("Initializing file sysytem store")
return filestore.NewFileSystemStore(c.FileStoreHTTPBasePath, "/")
}