Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 2 additions & 44 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ import (
"syscall"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"

"github.com/SCE-Development/SCEvents/internal/config"
"github.com/SCE-Development/SCEvents/pkg/db"
"github.com/SCE-Development/SCEvents/pkg/handlers"
"github.com/SCE-Development/SCEvents/pkg/middleware"
"github.com/SCE-Development/SCEvents/pkg/registration"
)

Expand Down Expand Up @@ -46,62 +41,25 @@ func main() {

ctx, cancel := context.WithCancel(context.Background())

redisStore := db.NewRedisStore(db.RedisClient())
stores := &db.Stores{
Redis: redisStore,
Redis: db.NewRedisStore(db.RedisClient()),
}

producer := registration.NewProducer(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep this

[]string{cfg.KafkaBroker},
cfg.KafkaTopic,
)

consumer := registration.NewConsumer(
[]string{cfg.KafkaBroker},
cfg.KafkaTopic,
cfg.KafkaGroupID,
stores,
)

eventHandler := handlers.NewEventHandler(stores)

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
consumer.Run(ctx)
}()

r := gin.Default()

config := cors.DefaultConfig()
config.AllowOrigins = []string{cfg.ClientURL}
config.AllowCredentials = true
config.AddAllowHeaders("Authorization")
r.Use(cors.New(config))

r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "response",
})
})

events := r.Group("/events")
{
events.GET("/", eventHandler.GetEvents)
events.GET("/:id", eventHandler.GetEventByID)
events.GET("/registrations/:request_id", eventHandler.GetRegistrationStatus)

protected := events.Group("/")
protected.Use(middleware.RequireAuth(middleware.MembershipStateNonMember, cfg.ClientAPIURL))
{
protected.POST("/", eventHandler.CreateEvent)
protected.POST("/:id/register", eventHandler.RegisterForEvent(producer))
protected.POST("/:id/waitlist", eventHandler.JoinEventWaitlist)
protected.DELETE("/:id", eventHandler.DeleteEventByID)
protected.PATCH("/:id", eventHandler.UpdateEventByID)
}
}
r, producer := setupRouter(cfg)

srv := &http.Server{
Addr: ":" + cfg.ServerPort,
Expand Down
64 changes: 64 additions & 0 deletions cmd/server/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"net/http"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/SCE-Development/SCEvents/internal/config"
"github.com/SCE-Development/SCEvents/pkg/db"
"github.com/SCE-Development/SCEvents/pkg/handlers"
"github.com/SCE-Development/SCEvents/pkg/middleware"
"github.com/SCE-Development/SCEvents/pkg/registration"
)

func setupRouter(cfg config.AppConfig) (*gin.Engine, *registration.Producer) {
redisStore := db.NewRedisStore(db.RedisClient())
stores := &db.Stores{
Redis: redisStore,
}

producer := registration.NewProducer(
[]string{cfg.KafkaBroker},
cfg.KafkaTopic,
)
Comment on lines +21 to +24
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's extract the producer creation logic


r := gin.Default()

// Set up CORS
config := cors.DefaultConfig()
config.AllowOrigins = []string{cfg.ClientURL}
config.AllowCredentials = true
config.AddAllowHeaders("Authorization")
r.Use(cors.New(config))

// Create handlers
eventHandler := handlers.NewEventHandler(stores)

// Ping route
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "response",
})
})

// Events routes
events := r.Group("/events")
{
events.GET("/", eventHandler.GetEvents)
events.GET("/:id", eventHandler.GetEventByID)
events.GET("/registrations/:request_id", eventHandler.GetRegistrationStatus)

protected := events.Group("/")
protected.Use(middleware.RequireAuth(middleware.MembershipStateNonMember, cfg.ClientAPIURL))
{
protected.POST("/", eventHandler.CreateEvent)
protected.DELETE("/:id", eventHandler.DeleteEventByID)
protected.PATCH("/:id", eventHandler.UpdateEventByID)
protected.POST("/:id/register", eventHandler.RegisterForEvent(producer))
protected.POST("/:id/waitlist", eventHandler.JoinEventWaitlist)
}
}

return r, producer
}
Loading