145 lines
4.0 KiB
Go
145 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"thamanyah/cms/v2/internal/db"
|
|
"thamanyah/cms/v2/internal/db/repositories"
|
|
"thamanyah/cms/v2/internal/handlers"
|
|
"thamanyah/cms/v2/internal/services"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
"github.com/aws/aws-sdk-go-v2/service/mediaconvert"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
_ "github.com/lib/pq"
|
|
httpSwagger "github.com/swaggo/http-swagger/v2"
|
|
|
|
_ "thamanyah/cms/v2/docs"
|
|
)
|
|
|
|
// @title Thamanyah CMS API
|
|
// @version 1.0
|
|
// @description JSON API for ingesting videos into the Thamanyah catalogue. Uploads are two-step: presign, PUT the file straight to S3, then register the video to queue transcoding.
|
|
// @BasePath /
|
|
|
|
func main() {
|
|
if len(os.Args) > 1 && os.Args[1] == "migrate" {
|
|
runMigrate()
|
|
return
|
|
}
|
|
runServer()
|
|
}
|
|
|
|
func runMigrate() {
|
|
concreteDBClient := db.CreateDBConnection(requireDBConnectionString())
|
|
defer db.CloseConnection(concreteDBClient)
|
|
db.Migrate(concreteDBClient)
|
|
log.Println("migrations applied successfully")
|
|
}
|
|
|
|
func runServer() {
|
|
awsConfig, err := config.LoadDefaultConfig(context.Background())
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed To Load S3 Config: %s", err))
|
|
}
|
|
|
|
bucketName := os.Getenv("S3_BUCKET")
|
|
if bucketName == "" {
|
|
panic(fmt.Errorf("missing required env var: S3_BUCKET"))
|
|
}
|
|
|
|
mediaConvertRole := os.Getenv("MEDIACONVERT_ROLE_ARN")
|
|
if mediaConvertRole == "" {
|
|
panic(fmt.Errorf("missing required env var: MEDIACONVERT_ROLE_ARN"))
|
|
}
|
|
|
|
mediaConvertInputBucket := os.Getenv("MEDIACONVERT_INPUT_BUCKET")
|
|
if mediaConvertInputBucket == "" {
|
|
panic(fmt.Errorf("missing required env var: MEDIACONVERT_INPUT_BUCKET"))
|
|
}
|
|
|
|
mediaConvertOutputBucket := os.Getenv("MEDIACONVERT_OUTPUT_BUCKET")
|
|
if mediaConvertOutputBucket == "" {
|
|
panic(fmt.Errorf("missing required env var: MEDIACONVERT_OUTPUT_BUCKET"))
|
|
}
|
|
|
|
concreteS3Client := &services.S3Concrete{
|
|
S3Client: s3.NewFromConfig(awsConfig),
|
|
Bucket: bucketName,
|
|
}
|
|
concreteS3Client.AssertSuccessfulConnection(context.Background())
|
|
|
|
services.S3Client = concreteS3Client
|
|
|
|
services.MediaConvertClient = &services.MediaConvertConcrete{
|
|
MediaConvertClient: mediaconvert.NewFromConfig(awsConfig),
|
|
Role: mediaConvertRole,
|
|
InputBucket: mediaConvertInputBucket,
|
|
OutputBucket: mediaConvertOutputBucket,
|
|
}
|
|
|
|
concreteDBClient := db.CreateDBConnection(requireDBConnectionString())
|
|
defer db.CloseConnection(concreteDBClient)
|
|
db.AssertSuccessfulConnection(context.Background(), concreteDBClient)
|
|
|
|
repositories.VideoRepo = repositories.VideoRepository{
|
|
SQLDB: concreteDBClient,
|
|
}
|
|
|
|
repositories.CatagoriesRepo = repositories.CatagoriesRepository{
|
|
SQLDB: concreteDBClient,
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("GET /health", handlers.Health)
|
|
mux.HandleFunc("GET /api/categories", handlers.ListCategories)
|
|
mux.HandleFunc("POST /api/videos/presign", handlers.PresignVideoUpload)
|
|
mux.HandleFunc("POST /api/videos", handlers.CompleteVideoUpload)
|
|
|
|
// Swagger UI and the generated spec. The UI assets are embedded in the
|
|
// binary by swaggo/files, so this needs no static directory on disk.
|
|
mux.Handle("GET /swagger/", httpSwagger.WrapHandler)
|
|
|
|
addr := ":8081"
|
|
log.Printf("listening on %s", addr)
|
|
if err := http.ListenAndServe(addr, mux); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func requireDBConnectionString() string {
|
|
dbHost := os.Getenv("DB_HOST")
|
|
if dbHost == "" {
|
|
panic(fmt.Errorf("missing required env var: DB_HOST"))
|
|
}
|
|
|
|
dbPort := os.Getenv("DB_PORT")
|
|
if dbPort == "" {
|
|
panic(fmt.Errorf("missing required env var: DB_PORT"))
|
|
}
|
|
|
|
dbUser := os.Getenv("DB_USER")
|
|
if dbUser == "" {
|
|
panic(fmt.Errorf("missing required env var: DB_USER"))
|
|
}
|
|
|
|
dbPassword := os.Getenv("DB_PASSWORD")
|
|
if dbPassword == "" {
|
|
panic(fmt.Errorf("missing required env var: DB_PASSWORD"))
|
|
}
|
|
|
|
dbName := os.Getenv("DB_NAME")
|
|
if dbName == "" {
|
|
panic(fmt.Errorf("missing required env var: DB_NAME"))
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
"host=%s port=%s user=%s password=%s dbname=%s sslmode=require",
|
|
dbHost, dbPort, dbUser, dbPassword, dbName,
|
|
)
|
|
}
|