Skip to content

Commit 6d82148

Browse files
authored
Merge pull request #102 from swamphacks/dev
Release v0.3.0
2 parents c7da0c6 + 1e40f6b commit 6d82148

File tree

172 files changed

+23948
-358
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+23948
-358
lines changed

apps/api/.env.dev.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
DATABASE_URL="postgres://postgres:postgres@postgres:5432/coredb"
22
DATABASE_URL_MIGRATION="postgres://postgres:postgres@localhost:5432/coredb"
33
REDIS_URL="redis:6379"
4+
ALLOWED_ORIGINS="" # URLs in comma seperated list
45

56
# For OAuth
67
AUTH_DISCORD_CLIENT_ID=

apps/api/cmd/api/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ func main() {
5454
accountRepo := repository.NewAccountRespository(database)
5555
sessionRepo := repository.NewSessionRepository(database)
5656
eventInterestRepo := repository.NewEventInterestRepository(database)
57+
eventRepo := repository.NewEventRespository(database)
5758

5859
// Injections into services
5960
authService := services.NewAuthService(userRepo, accountRepo, sessionRepo, txm, client, logger, &cfg.Auth)
6061
eventInterestService := services.NewEventInterestService(eventInterestRepo, logger)
62+
eventService := services.NewEventService(eventRepo, userRepo, logger)
6163
emailService := services.NewEmailService(taskQueueClient, logger)
6264

6365
// Injections into handlers
64-
apiHandlers := handlers.NewHandlers(authService, eventInterestService, emailService, cfg, logger)
66+
apiHandlers := handlers.NewHandlers(authService, eventInterestService, eventService, emailService, cfg, logger)
6567

6668
api := api.NewAPI(&logger, apiHandlers, mw)
6769

apps/api/go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/caarlos0/env/v11 v11.3.1
77
github.com/go-chi/chi/v5 v5.2.2
88
github.com/go-chi/cors v1.2.1
9+
github.com/go-playground/validator/v10 v10.27.0
910
github.com/google/uuid v1.6.0
1011
github.com/hibiken/asynq v0.25.1
1112
github.com/jackc/pgx/v5 v5.7.4
@@ -16,16 +17,21 @@ require (
1617
require (
1718
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1819
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
20+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
21+
github.com/go-playground/locales v0.14.1 // indirect
22+
github.com/go-playground/universal-translator v0.18.1 // indirect
1923
github.com/jackc/pgpassfile v1.0.0 // indirect
2024
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
2125
github.com/jackc/puddle/v2 v2.2.2 // indirect
26+
github.com/leodido/go-urn v1.4.0 // indirect
2227
github.com/mattn/go-colorable v0.1.13 // indirect
2328
github.com/mattn/go-isatty v0.0.19 // indirect
2429
github.com/redis/go-redis/v9 v9.7.0 // indirect
2530
github.com/robfig/cron/v3 v3.0.1 // indirect
2631
github.com/spf13/cast v1.7.0 // indirect
2732
github.com/stretchr/testify v1.10.0 // indirect
2833
golang.org/x/crypto v0.38.0 // indirect
34+
golang.org/x/net v0.38.0 // indirect
2935
golang.org/x/sync v0.14.0 // indirect
3036
golang.org/x/sys v0.34.0 // indirect
3137
golang.org/x/text v0.25.0 // indirect

apps/api/go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
1414
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
1515
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
1616
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
17+
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
18+
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
1719
github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
1820
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
1921
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
2022
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
23+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
24+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
25+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
26+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
27+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
28+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
29+
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
30+
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
2131
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
2232
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2333
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@@ -39,6 +49,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
3949
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
4050
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4151
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
52+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
53+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
4254
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4355
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
4456
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
@@ -67,6 +79,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
6779
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
6880
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
6981
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
82+
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
83+
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
7084
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
7185
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
7286
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

apps/api/internal/api/api.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/rs/zerolog/log"
1111
"github.com/swamphacks/core/apps/api/internal/api/handlers"
1212
mw "github.com/swamphacks/core/apps/api/internal/api/middleware"
13+
"github.com/swamphacks/core/apps/api/internal/config"
1314
"github.com/swamphacks/core/apps/api/internal/db/sqlc"
1415
)
1516

@@ -34,10 +35,18 @@ func NewAPI(logger *zerolog.Logger, handlers *handlers.Handlers, middleware *mw.
3435
}
3536

3637
func (api *API) setupRoutes(mw *mw.Middleware) {
38+
var (
39+
// Both requireXXRole functions automatically allow superusers
40+
ensureSuperuser = mw.Auth.RequirePlatformRole([]sqlc.AuthUserRole{sqlc.AuthUserRoleSuperuser})
41+
ensureEventAdmin = mw.Event.RequireEventRole([]sqlc.EventRoleType{sqlc.EventRoleTypeAdmin})
42+
)
43+
44+
AllowedOrigins := config.Load().AllowedOrigins
45+
3746
api.Router.Use(middleware.Logger)
3847
api.Router.Use(middleware.RealIP)
3948
api.Router.Use(cors.Handler(cors.Options{
40-
AllowedOrigins: []string{"http://localhost:5173"},
49+
AllowedOrigins: AllowedOrigins,
4150
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
4251
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
4352
ExposedHeaders: []string{"Link"},
@@ -67,8 +76,18 @@ func (api *API) setupRoutes(mw *mw.Middleware) {
6776
})
6877

6978
// Event routes
70-
api.Router.Route("/event", func(r chi.Router) {
71-
r.Post("/{eventId}/interest", api.Handlers.EventInterest.AddEmailToEvent)
79+
api.Router.Route("/events", func(r chi.Router) {
80+
r.With(mw.Auth.RequireAuth, ensureSuperuser).Post("/", api.Handlers.Event.CreateEvent)
81+
r.With(mw.Auth.RequireAuth).Get("/", api.Handlers.Event.GetEvents)
82+
r.Route("/{eventId}", func(r chi.Router) {
83+
r.With(mw.Auth.RequireAuth, ensureEventAdmin).Patch("/", api.Handlers.Event.UpdateEventById)
84+
r.With(mw.Auth.RequireAuth, ensureSuperuser).Delete("/", api.Handlers.Event.DeleteEventById)
85+
r.With(mw.Auth.RequireAuth, ensureEventAdmin).Get("/staff", api.Handlers.Event.GetEventStaffUsers)
86+
r.With(mw.Auth.RequireAuth, ensureEventAdmin).Post("/roles", api.Handlers.Event.AssignEventRole)
87+
r.With(mw.Auth.RequireAuth).Get("/role", api.Handlers.Event.GetEventRole)
88+
r.Get("/", api.Handlers.Event.GetEventByID)
89+
r.Post("/interest", api.Handlers.EventInterest.AddEmailToEvent)
90+
})
7291
})
7392

7493
// Email routes
@@ -87,7 +106,7 @@ func (api *API) setupRoutes(mw *mw.Middleware) {
87106
})
88107

89108
r.Group(func(r chi.Router) {
90-
r.Use(mw.Auth.RequirePlatformRole(sqlc.AuthUserRoleUser))
109+
r.Use(mw.Auth.RequirePlatformRole([]sqlc.AuthUserRole{sqlc.AuthUserRoleUser}))
91110
r.Get("/user", func(w http.ResponseWriter, r *http.Request) {
92111
if _, err := w.Write([]byte("Welcome, user!\n")); err != nil {
93112
log.Err(err)
@@ -96,7 +115,7 @@ func (api *API) setupRoutes(mw *mw.Middleware) {
96115
})
97116

98117
r.Group(func(r chi.Router) {
99-
r.Use(mw.Auth.RequirePlatformRole(sqlc.AuthUserRoleSuperuser))
118+
r.Use(mw.Auth.RequirePlatformRole([]sqlc.AuthUserRole{sqlc.AuthUserRoleSuperuser}))
100119
r.Get("/superuser", func(w http.ResponseWriter, r *http.Request) {
101120
if _, err := w.Write([]byte("Welcome, superuser!\n")); err != nil {
102121
log.Err(err)

0 commit comments

Comments
 (0)