Skip to content

Commit e8fa1dc

Browse files
committed
fix startup with kafka
1 parent 8becc18 commit e8fa1dc

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

internal/api/fiber.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"log"
5+
"time"
56

67
"github.com/gofiber/fiber/v2"
78
"github.com/gofiber/fiber/v2/middleware/compress"
@@ -11,7 +12,6 @@ import (
1112
"github.com/ortelius/pdvd-backend/v12/database"
1213
"github.com/ortelius/pdvd-backend/v12/graphql"
1314
"github.com/ortelius/pdvd-backend/v12/restapi"
14-
"github.com/ortelius/pdvd-backend/v12/restapi/modules/auth"
1515
)
1616

1717
// NewFiberApp creates and configures a Fiber app with REST and GraphQL routes
@@ -26,34 +26,34 @@ func NewFiberApp(db database.DBConnection) *fiber.App {
2626
app := fiber.New(fiber.Config{
2727
AppName: "pdvd-backend/v12 API v1.0",
2828
BodyLimit: 50 * 1024 * 1024, // 50MB
29-
ReadTimeout: 60, // seconds
29+
ReadTimeout: 60 * time.Second, // seconds
3030
})
3131

3232
// Middleware
3333
app.Use(fiberrecover.New())
3434
app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))
35-
app.Use(func(c *fiber.Ctx) error {
36-
c.Locals("graphql_op", "-")
37-
return c.Next()
38-
})
39-
app.Use(logger.New())
35+
36+
// Consolidated CORS Configuration
4037
app.Use(cors.New(cors.Config{
41-
AllowOrigins: "http://localhost:3000,http://localhost:4000",
38+
AllowOrigins: "http://localhost:3000,http://localhost:4000,http://127.0.0.1:3000,http://127.0.0.1:4000",
4239
AllowHeaders: "Origin, Content-Type, Accept, Authorization, X-Requested-With",
4340
AllowCredentials: true,
4441
AllowMethods: "GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS",
4542
}))
4643

44+
app.Use(func(c *fiber.Ctx) error {
45+
c.Locals("graphql_op", "-")
46+
return c.Next()
47+
})
48+
app.Use(logger.New())
49+
4750
// Health check endpoint
4851
app.Get("/", func(c *fiber.Ctx) error {
4952
return c.JSON(fiber.Map{"status": "healthy"})
5053
})
5154

52-
// REST routes
53-
restapi.SetupRoutes(app, db)
54-
55-
// GraphQL endpoint
56-
app.Post("/api/v1/graphql", auth.OptionalAuth(db), GraphQLHandler(schema))
55+
// Setup REST and GraphQL routes (Pass the schema here)
56+
restapi.SetupRoutes(app, db, schema)
5757

5858
return app
5959
}

main.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,33 @@ func main() {
1818
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
1919
defer cancel()
2020

21-
// Attempt to start Kafka (Optional)
22-
if err := kafka.RunEventProcessor(ctx, db); err != nil {
23-
log.Printf("Warning: Kafka initialization failed after 3 tries: %v. Starting without Kafka support.", err)
24-
} else {
25-
log.Println("Kafka processor initialized successfully.")
26-
}
27-
28-
// Start everything else normally
21+
// 1. Initialize Fiber App FIRST
2922
app := api.NewFiberApp(db)
3023
port := os.Getenv("MS_PORT")
3124
if port == "" {
3225
port = "3000"
3326
}
3427

28+
// 2. Start Server in Goroutine
29+
// This ensures the API is listening immediately
3530
go func() {
3631
log.Printf("Starting server on port %s", port)
3732
if err := app.Listen(":" + port); err != nil {
3833
log.Fatalf("Failed to start server: %v", err)
3934
}
4035
}()
4136

37+
// 3. Attempt to start Kafka (Optional) - Moved AFTER Fiber
38+
// We run this in a separate goroutine so it doesn't block the main thread
39+
// or delay the application if Kafka is unavailable.
40+
go func() {
41+
if err := kafka.RunEventProcessor(ctx, db); err != nil {
42+
log.Printf("Warning: Kafka initialization failed after 3 tries: %v. Starting without Kafka support.", err)
43+
} else {
44+
log.Println("Kafka processor initialized successfully.")
45+
}
46+
}()
47+
4248
<-ctx.Done()
4349
log.Println("Shutting down pdvd-backend...")
4450
app.Shutdown()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package api
1+
package restapi
22

33
import (
44
"context"

restapi/router.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,20 @@ import (
1212
"github.com/go-git/go-git/v5"
1313
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
1414
"github.com/gofiber/fiber/v2"
15-
"github.com/gofiber/fiber/v2/middleware/cors"
15+
"github.com/graphql-go/graphql"
1616
"github.com/ortelius/pdvd-backend/v12/database"
1717
"github.com/ortelius/pdvd-backend/v12/restapi/modules/auth"
1818
"github.com/ortelius/pdvd-backend/v12/restapi/modules/github"
1919
"github.com/ortelius/pdvd-backend/v12/restapi/modules/releases"
2020
"github.com/ortelius/pdvd-backend/v12/restapi/modules/sync"
2121
)
2222

23-
// SetupRoutes configures all REST API routes
24-
func SetupRoutes(app *fiber.App, db database.DBConnection) {
25-
// ========================================================================
26-
// MIDDLEWARE
27-
// ========================================================================
28-
app.Use(cors.New(cors.Config{
29-
AllowOrigins: "http://localhost:3000,http://localhost:4000,http://127.0.0.1:3000,http://127.0.0.1:4000",
30-
AllowHeaders: "Origin, Content-Type, Accept, Authorization, X-Requested-With",
31-
AllowCredentials: true,
32-
AllowMethods: "GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS",
33-
}))
23+
// SetupRoutes configures all REST API routes and the GraphQL endpoint.
24+
// Redundant CORS middleware has been removed here as it is now handled globally
25+
// in internal/api/fiber.go to prevent 408 timeouts.
26+
func SetupRoutes(app *fiber.App, db database.DBConnection, schema graphql.Schema) {
3427

28+
// Background initialization tasks
3529
go func() {
3630
if err := auth.BootstrapAdmin(db); err != nil {
3731
log.Printf("WARNING: Failed to bootstrap admin: %v", err)
@@ -48,8 +42,12 @@ func SetupRoutes(app *fiber.App, db database.DBConnection) {
4842
go autoApplyRBACOnStartup(db, emailConfig)
4943
go startInvitationCleanup(db)
5044

45+
// API Group /api/v1
5146
api := app.Group("/api/v1")
5247

48+
// GraphQL Route - Mounted within the api group to inherit path prefixes
49+
api.Post("/graphql", auth.OptionalAuth(db), GraphQLHandler(schema))
50+
5351
// Public Routes
5452
api.Post("/signup", auth.Signup(db, emailConfig))
5553

0 commit comments

Comments
 (0)