Files
inventory-plus-plus/main.go
T
2026-01-11 15:53:42 -07:00

208 lines
5.0 KiB
Go

package main
//go:generate planter postgres://planter:planter@localhost:5432/inventory_2?sslmode=disable -o diagrams/database_schema.uml
//go:generate plantuml diagrams/*.uml -tsvg
//go:generate plantuml diagrams/etsy/*.uml -tsvg
//go:generate npx @tailwindcss/cli -i styles/source.css -o styles/index.css --cwd ./internal/server
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"ruben/inventory2/internal/domains/accounts"
"ruben/inventory2/internal/domains/authentication"
etsy_platform "ruben/inventory2/internal/domains/platforms/etsy"
"ruben/inventory2/internal/domains/raw_events"
"ruben/inventory2/internal/server"
)
const (
etsyAPIKeystring = "38ncokqh0jih5jshfk8iv4n5"
etsyAPISharedSecret = "jaaw0tyizf"
)
func main() {
if err := runApp(context.Background()); err != nil {
panic(err)
}
fmt.Println("application shutdown")
}
func runApp(ctx context.Context) error {
ctx, shutdown := context.WithCancel(ctx)
defer shutdown()
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
fmt.Println("slog groups:", groups)
fmt.Println("slog attr:", a)
// TODO: not sure if this will be needed, but keep around until known.
return a
},
}))
// connect to the database
connPool, err := newPool(ctx)
if err != nil {
return fmt.Errorf("failed to initialize database connection pool: %w", err)
}
// start background processes
auth, err := authentication.New(ctx, connPool, logger.WithGroup("*authentication.Authenticator"))
if err != nil {
return fmt.Errorf("failed to construct authenticator: %w", err)
}
authErrCh := runAuthProcesses(ctx, auth)
// start http server
srvErrCh := runServer(ctx, logger, connPool, auth)
// wait for interrupt signal or unrecoverable failure, then shutdown
osSignalCh := make(chan os.Signal, 1)
signal.Notify(osSignalCh, syscall.SIGINT, syscall.SIGTERM)
var (
alreadyShutdown struct {
server bool
authProcesses bool
}
)
select {
case s := <-osSignalCh:
fmt.Println("application received shutdown signal:", s)
fmt.Println("shutting down")
case err := <-authErrCh:
alreadyShutdown.authProcesses = true
fmt.Println("auth processes shutdown unexpectedly")
if err != nil {
fmt.Println("auth processes encountered error:", err)
}
case err := <-srvErrCh:
alreadyShutdown.server = true
fmt.Println("server shutdown unexpectedly")
if err != nil {
fmt.Println("server encountered error:", err)
}
}
shutdown()
// capture application errors that occurred during or caused shutdown
var errs []error
if !alreadyShutdown.authProcesses {
if err := <-authErrCh; err != nil {
errs = append(errs, fmt.Errorf("auth processes experienced an error: %w", err))
}
fmt.Println("auth processes shut down")
}
if !alreadyShutdown.server {
if err := <-srvErrCh; err != nil {
errs = append(errs, fmt.Errorf("server experienced an error: %w", err))
}
fmt.Println("server shut down")
}
return errors.Join(errs...)
}
func runAuthProcesses(ctx context.Context, auth *authentication.Authenticator) <-chan error {
errCh := make(chan error, 1)
go func() {
defer close(errCh)
if err := auth.RunBackgroundCleanup(ctx); err != nil {
errCh <- err
}
}()
return errCh
}
func runServer(ctx context.Context, logger *slog.Logger, connPool *pgxpool.Pool, auth *authentication.Authenticator) <-chan error {
srv := &http.Server{
Addr: ":8082", // local
Handler: server.NewServer(
logger.WithGroup("server"),
"./",
raw_events.NewStore(logger.WithGroup("raw-event-store"), connPool),
accounts.NewStore(logger, connPool),
etsy_platform.NewPlatform(
logger,
func(acctID int64) string {
return fmt.Sprintf("/oauth/account/%d/auth_code", acctID)
},
etsyAPIKeystring,
etsyAPISharedSecret,
connPool,
),
auth,
),
}
ctx, cancel := context.WithCancel(ctx)
alreadyShutdownCh := make(chan struct{}, 1)
runningErrCh := make(chan error, 1)
go func() {
defer close(runningErrCh)
defer cancel()
defer close(alreadyShutdownCh)
fmt.Println("server running on 8082...")
if err := srv.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
runningErrCh <- fmt.Errorf("server experienced error: %w", err)
}
}
}()
shutdownErrCh := make(chan error, 1)
go func() {
defer close(shutdownErrCh)
select {
case <-alreadyShutdownCh:
return
case <-ctx.Done():
}
shutdownCtx, cancelShutdown := context.WithTimeout(context.Background(), 60*time.Second)
defer cancelShutdown()
if err := srv.Shutdown(shutdownCtx); err != nil {
shutdownErrCh <- fmt.Errorf("error occurred attempting to shutdown server: %w", err)
}
}()
errCh := make(chan error, 1)
go func() {
defer close(errCh)
err1 := <-runningErrCh
err2 := <-shutdownErrCh
if err := errors.Join(err1, err2); err != nil {
errCh <- err
}
}()
return errCh
}