Skip to content

Commit 61ce056

Browse files
committed
feat: add vercel serverless backend deployment setup
1 parent c8bbd5b commit 61ce056

4 files changed

Lines changed: 105 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ frontend/dist/
2020
Thumbs.db
2121
.vscode/
2222
.idea/
23+
.vercel

api/index.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package handler
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
"sync"
8+
9+
"eventra/bootstrap"
10+
)
11+
12+
var (
13+
initOnce sync.Once
14+
initErr error
15+
router http.Handler
16+
)
17+
18+
func Handler(w http.ResponseWriter, r *http.Request) {
19+
if r.URL.Path == "/health" {
20+
w.Header().Set("Content-Type", "application/json")
21+
w.WriteHeader(http.StatusOK)
22+
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok", "service": "eventra-auth"})
23+
return
24+
}
25+
26+
initOnce.Do(func() {
27+
router, initErr = bootstrap.NewHTTPHandler(context.Background())
28+
})
29+
30+
if initErr != nil {
31+
w.Header().Set("Content-Type", "application/json")
32+
w.WriteHeader(http.StatusInternalServerError)
33+
_ = json.NewEncoder(w).Encode(map[string]string{
34+
"error": "backend initialization failed",
35+
})
36+
return
37+
}
38+
39+
router.ServeHTTP(w, r)
40+
}

bootstrap/http_handler.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package bootstrap
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"eventra/internal/config"
9+
"eventra/internal/delivery/httpserver"
10+
"eventra/internal/repository/postgres"
11+
"eventra/internal/usecase/auth"
12+
"eventra/pkg/database"
13+
"eventra/pkg/security"
14+
)
15+
16+
func NewHTTPHandler(ctx context.Context) (http.Handler, error) {
17+
cfg, err := config.Load()
18+
if err != nil {
19+
return nil, fmt.Errorf("load config: %w", err)
20+
}
21+
22+
dbPool, err := database.NewPostgresPool(ctx, cfg.DBURL)
23+
if err != nil {
24+
return nil, fmt.Errorf("connect database: %w", err)
25+
}
26+
27+
jwtManager := security.NewJWTManager(cfg.JWTSecret, cfg.JWTExpiration)
28+
userRepo := postgres.NewUserRepository(dbPool)
29+
refreshRepo := postgres.NewRefreshTokenRepository(dbPool)
30+
securityRepo := postgres.NewLoginSecurityRepository(dbPool)
31+
auditRepo := postgres.NewSecurityAuditRepository(dbPool, cfg.SecurityAlertWebhookURL, cfg.SecurityAlertWebhookFormat)
32+
tokenBlacklistRepo := postgres.NewTokenBlacklistRepository(dbPool)
33+
34+
authService := auth.NewService(
35+
userRepo,
36+
refreshRepo,
37+
jwtManager,
38+
cfg.RefreshTokenExpiration,
39+
auth.WithLoginSecurityRepository(securityRepo),
40+
auth.WithAuditLogger(auditRepo),
41+
auth.WithTokenBlacklist(tokenBlacklistRepo),
42+
)
43+
44+
authHandler := httpserver.NewAuthHandler(authService)
45+
router := httpserver.NewRouter(authHandler, jwtManager, tokenBlacklistRepo, cfg.CORSAllowedOrigins)
46+
return router, nil
47+
}

vercel.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"functions": {
3+
"api/index.go": {
4+
"maxDuration": 15
5+
}
6+
},
7+
"routes": [
8+
{
9+
"src": "/health",
10+
"dest": "api/index.go"
11+
},
12+
{
13+
"src": "/api/(.*)",
14+
"dest": "api/index.go"
15+
}
16+
]
17+
}

0 commit comments

Comments
 (0)