130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"database/sql"
|
|
"embed"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"runtime/debug"
|
|
|
|
_ "github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/pressly/goose/v3"
|
|
"github.com/riverqueue/river"
|
|
"github.com/riverqueue/river/riverdriver/riverpgxv5"
|
|
"github.com/riverqueue/river/rivermigrate"
|
|
)
|
|
|
|
var (
|
|
//go:embed migrations/*.sql
|
|
embedMigrations embed.FS
|
|
SERVE_ADDR = cmp.Or(os.Getenv("SERVE_ADDR"), "0.0.0.0:9876")
|
|
RUN_MIGRATIONS = os.Getenv("MIGRATIONS") != "skip"
|
|
ONLY_MIGRATION = os.Getenv("MIGRATIONS") == "only"
|
|
DATABASE_DSN = cmp.Or(os.Getenv("DATABASE_DSN"), "postgres://username:password@localhost:5432/ligmotes")
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
ctx = context.Background()
|
|
buildInfo, _ = debug.ReadBuildInfo()
|
|
logger = slog.Default().With(
|
|
"go-version", buildInfo.GoVersion,
|
|
)
|
|
)
|
|
commit := "unknown"
|
|
for _, s := range buildInfo.Settings {
|
|
if s.Key == "vcs.revision" {
|
|
commit = s.Value
|
|
if len(commit) > 8 {
|
|
commit = commit[:8]
|
|
}
|
|
}
|
|
if s.Key == "vcs.modified" {
|
|
commit += " (dirty)"
|
|
}
|
|
}
|
|
logger = logger.With("version", commit)
|
|
|
|
pgxPool, err := pgxpool.New(ctx, DATABASE_DSN)
|
|
if err != nil {
|
|
logger.ErrorContext(ctx, "failed to open db", "error", err)
|
|
return
|
|
}
|
|
riverDriver := riverpgxv5.New(pgxPool)
|
|
|
|
if RUN_MIGRATIONS {
|
|
|
|
rmigrator, err := rivermigrate.New(riverDriver, nil)
|
|
if err != nil {
|
|
logger.ErrorContext(ctx, "failed to create river migrator ", "error", err)
|
|
return
|
|
}
|
|
|
|
rmigrator.Logger = logger
|
|
|
|
_, err = rmigrator.Migrate(ctx, rivermigrate.DirectionUp, nil)
|
|
if err != nil {
|
|
logger.ErrorContext(ctx, "failed to do river migration", "error", err)
|
|
return
|
|
}
|
|
|
|
db := stdlib.OpenDBFromPool(pgxPool)
|
|
if err := migrate(ctx, db); err != nil {
|
|
logger.ErrorContext(ctx, "failed to run migrations", "error", err)
|
|
return
|
|
}
|
|
}
|
|
if ONLY_MIGRATION {
|
|
return
|
|
}
|
|
|
|
riverClient, err := river.NewClient(riverDriver, &river.Config{
|
|
// TODO (Any) define a queue
|
|
Queues: nil,
|
|
Workers: nil,
|
|
})
|
|
if err != nil {
|
|
logger.ErrorContext(ctx, "failed to create river client", "error", err)
|
|
return
|
|
}
|
|
|
|
defer func() {
|
|
err := riverClient.StopAndCancel(ctx)
|
|
if err != nil {
|
|
logger.ErrorContext(ctx, "failed to stop river client", "error", err)
|
|
}
|
|
}()
|
|
|
|
//TODO(any) use river
|
|
_ = riverClient
|
|
|
|
logger.InfoContext(ctx, "starting server", "addr", SERVE_ADDR)
|
|
err = http.ListenAndServe(SERVE_ADDR, nil)
|
|
if err != nil {
|
|
logger.ErrorContext(ctx, "failed to serve", "error", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func migrate(ctx context.Context, db *sql.DB) error {
|
|
|
|
goose.SetBaseFS(embedMigrations)
|
|
|
|
if err := goose.SetDialect(string(goose.DialectSQLite3)); err != nil {
|
|
return fmt.Errorf("could not set goose dialect: %w", err)
|
|
}
|
|
|
|
// goose grabs a lock on the database when doing migrations
|
|
// so running multiple instances of the server is safe.
|
|
if err := goose.UpContext(ctx, db, "migrations"); err != nil {
|
|
return fmt.Errorf("failed to execute: %w", err)
|
|
}
|
|
return nil
|
|
}
|