Files
inventory-plus-plus/main.go
T

166 lines
3.9 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
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"ruben/inventory2/internal/domains/accounts"
etsy_platform "ruben/inventory2/internal/domains/platforms/etsy"
"ruben/inventory2/internal/domains/raw_events"
"ruben/inventory2/internal/site"
"ruben/inventory2/internal/webhooks"
etsy_webhooks "ruben/inventory2/internal/webhooks/etsy"
)
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()
// connect to the database
connPool, err := newPool(ctx)
if err != nil {
return fmt.Errorf("failed to initialize database connection pool: %w", err)
}
// start http server
srvErrCh := runServer(ctx, connPool)
// wait for interrupt signal or unrecoverable failure, then shutdown
osSignalCh := make(chan os.Signal, 1)
signal.Notify(osSignalCh, syscall.SIGINT, syscall.SIGTERM)
var serverAlreadyShutdown bool
select {
case s := <-osSignalCh:
fmt.Println("application received shutdown signal:", s)
fmt.Println("shutting down")
case err := <-srvErrCh:
serverAlreadyShutdown = 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 !serverAlreadyShutdown {
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 runServer(ctx context.Context, connPool *pgxpool.Pool) <-chan error {
srv := &http.Server{
Addr: ":8082", // local
Handler: buildHTTPHandler(connPool),
}
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
}
func buildHTTPHandler(connPool *pgxpool.Pool) http.Handler {
eventsDB := raw_events.NewStore(connPool)
etsy := etsy_platform.NewPlatform(
func(acctID int64) string {
return fmt.Sprintf("/oauth/account/%d/auth_code", acctID)
},
etsyAPIKeystring,
etsyAPISharedSecret,
connPool,
)
accts := accounts.NewStore(connPool)
mux := http.NewServeMux()
mux.Handle("/webhooks/", http.StripPrefix("/webhooks", webhooks.New(eventsDB, etsy, webhooks.Config{
Etsy: etsy_webhooks.Config{
OAuthRedirectURIWithAcctIDParam: "/oauth/account/{acctID}/auth_code",
},
})))
mux.Handle("/site/", http.StripPrefix("/site", site.NewSiteHandler("./internal/site", eventsDB, accts, etsy)))
mux.Handle("/", http.RedirectHandler("/site", http.StatusPermanentRedirect))
return mux
}