fix: address code review findings from story #01
- Makefile: -include .env (soft) so fresh clone doesn't hard-fail
- main.go: replace log.Fatalf-in-goroutine with error channel; startup
failures now reach main goroutine so defer pool.Close() always runs
- main.go: context.WithTimeout(30s) on database.Connect to fail fast
instead of hanging indefinitely on unreachable Postgres
- router.go: store pool on Echo context via middleware so future story
handlers can retrieve it with c.Get("pool")
- config.go: require DATABASE_URL explicitly (log.Fatal if missing)
instead of falling back to hardcoded credentials
- HelloWorld.test.ts: URL-aware fetch stub + afterEach restoreAllMocks;
add flushPromises test verifying backend status renders
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -40,9 +40,12 @@ func main() {
|
||||
}
|
||||
|
||||
func serve(cfg *config.Config) {
|
||||
ctx := context.Background()
|
||||
// Use a timeout context for the initial DB connection so a slow/unreachable
|
||||
// Postgres doesn't block server startup indefinitely.
|
||||
connectCtx, connectCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer connectCancel()
|
||||
|
||||
pool, err := database.Connect(ctx, cfg.DatabaseURL)
|
||||
pool, err := database.Connect(connectCtx, cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
log.Fatalf("database connect: %v", err)
|
||||
}
|
||||
@@ -51,19 +54,27 @@ func serve(cfg *config.Config) {
|
||||
|
||||
e := New(pool)
|
||||
|
||||
// Start server
|
||||
// Start server; signal failures via a channel so the main goroutine can
|
||||
// handle them without calling os.Exit from a goroutine (which would skip
|
||||
// all deferred cleanup, including pool.Close).
|
||||
startErr := make(chan error, 1)
|
||||
go func() {
|
||||
addr := fmt.Sprintf(":%s", cfg.Port)
|
||||
log.Printf("server: listening on %s", addr)
|
||||
if err := e.Start(addr); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("server: %v", err)
|
||||
startErr <- err
|
||||
}
|
||||
}()
|
||||
|
||||
// Graceful shutdown on SIGINT / SIGTERM
|
||||
// Graceful shutdown on SIGINT / SIGTERM or fatal startup error.
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
select {
|
||||
case err := <-startErr:
|
||||
log.Fatalf("server: %v", err)
|
||||
case <-quit:
|
||||
}
|
||||
|
||||
log.Println("server: shutting down")
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
|
||||
Reference in New Issue
Block a user