renamed site directory to server

This commit is contained in:
2026-01-11 15:27:54 -07:00
parent 889aead6b6
commit ae01554b0b
65 changed files with 123 additions and 49 deletions
+39 -14
View File
@@ -3,12 +3,13 @@ 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/site
//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"
@@ -21,7 +22,7 @@ import (
"ruben/inventory2/internal/domains/authentication"
etsy_platform "ruben/inventory2/internal/domains/platforms/etsy"
"ruben/inventory2/internal/domains/raw_events"
"ruben/inventory2/internal/site"
"ruben/inventory2/internal/server"
"ruben/inventory2/internal/webhooks"
etsy_webhooks "ruben/inventory2/internal/webhooks/etsy"
)
@@ -42,6 +43,17 @@ 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)
@@ -51,7 +63,7 @@ func runApp(ctx context.Context) error {
// start background processes
auth, err := authentication.New(ctx, connPool)
auth, err := authentication.New(ctx, connPool, logger.WithGroup("*authentication.Authenticator"))
if err != nil {
return fmt.Errorf("failed to construct authenticator: %w", err)
}
@@ -60,7 +72,7 @@ func runApp(ctx context.Context) error {
// start http server
srvErrCh := runServer(ctx, connPool, auth)
srvErrCh := runServer(ctx, logger, connPool, auth)
// wait for interrupt signal or unrecoverable failure, then shutdown
@@ -127,10 +139,10 @@ func runAuthProcesses(ctx context.Context, auth *authentication.Authenticator) <
return errCh
}
func runServer(ctx context.Context, connPool *pgxpool.Pool, auth *authentication.Authenticator) <-chan error {
func runServer(ctx context.Context, logger *slog.Logger, connPool *pgxpool.Pool, auth *authentication.Authenticator) <-chan error {
srv := &http.Server{
Addr: ":8082", // local
Handler: buildHTTPHandler(connPool, auth),
Handler: buildHTTPHandler(logger, connPool, auth),
}
ctx, cancel := context.WithCancel(ctx)
@@ -181,9 +193,10 @@ func runServer(ctx context.Context, connPool *pgxpool.Pool, auth *authentication
return errCh
}
func buildHTTPHandler(connPool *pgxpool.Pool, auth *authentication.Authenticator) http.Handler {
eventsDB := raw_events.NewStore(connPool)
func buildHTTPHandler(logger *slog.Logger, connPool *pgxpool.Pool, auth *authentication.Authenticator) http.Handler {
eventsDB := raw_events.NewStore(logger.WithGroup("raw-event-store"), connPool)
etsy := etsy_platform.NewPlatform(
logger,
func(acctID int64) string {
return fmt.Sprintf("/oauth/account/%d/auth_code", acctID)
},
@@ -191,16 +204,28 @@ func buildHTTPHandler(connPool *pgxpool.Pool, auth *authentication.Authenticator
etsyAPISharedSecret,
connPool,
)
accts := accounts.NewStore(connPool)
accts := accounts.NewStore(logger, 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("/webhooks/", http.StripPrefix("/webhooks", webhooks.New(
logger.WithGroup("webhooks"),
eventsDB,
etsy,
webhooks.Config{
Etsy: etsy_webhooks.Config{
OAuthRedirectURIWithAcctIDParam: "/oauth/account/{acctID}/auth_code",
},
},
})))
mux.Handle("/", site.NewServer("./internal/site", eventsDB, accts, etsy, auth))
)))
mux.Handle("/", server.NewServer(
logger.WithGroup("server"),
"./internal/server",
eventsDB,
accts,
etsy,
auth,
))
return mux
}