Skip to content

Commit fe17231

Browse files
committed
feat: Enhance API subdomain configuration and add track endpoint with CORS support
1 parent bd7f20c commit fe17231

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

Caddyfile

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,74 @@
1-
# Replace with your domain
1+
# API Subdomain - handles tracking and API routes
2+
{$API_DOMAIN:api.localhost} {
3+
# Logging
4+
log {
5+
output file /var/log/caddy/api-access.log
6+
level INFO
7+
}
8+
9+
# Security headers
10+
header {
11+
# Enable HSTS
12+
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
13+
# Prevent XSS attacks
14+
X-XSS-Protection "1; mode=block"
15+
# Prevent MIME type sniffing
16+
X-Content-Type-Options "nosniff"
17+
# Referrer policy
18+
Referrer-Policy "strict-origin-when-cross-origin"
19+
# Remove Server header
20+
-Server
21+
}
22+
23+
# CORS headers for API subdomain
24+
@options {
25+
method OPTIONS
26+
}
27+
handle @options {
28+
header {
29+
Access-Control-Allow-Origin "*"
30+
Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS, PATCH"
31+
Access-Control-Allow-Headers "Accept, Authorization, Content-Type, X-Api-Key"
32+
Access-Control-Max-Age "3600"
33+
}
34+
respond 204
35+
}
36+
37+
# Health check endpoint
38+
handle /health {
39+
reverse_proxy app:4000 {
40+
header_up Host {host}
41+
header_up X-Real-IP {remote_host}
42+
header_up X-Forwarded-For {remote_host}
43+
header_up X-Forwarded-Proto {scheme}
44+
}
45+
}
46+
47+
# Track endpoint - public API
48+
handle /track {
49+
reverse_proxy app:4000 {
50+
header_up Host {host}
51+
header_up X-Real-IP {remote_host}
52+
header_up X-Forwarded-For {remote_host}
53+
header_up X-Forwarded-Proto {scheme}
54+
}
55+
}
56+
57+
# Authenticated API routes
58+
handle /api/* {
59+
reverse_proxy app:4000 {
60+
header_up Host {host}
61+
header_up X-Real-IP {remote_host}
62+
header_up X-Forwarded-For {remote_host}
63+
header_up X-Forwarded-Proto {scheme}
64+
}
65+
}
66+
67+
# Enable gzip compression
68+
encode gzip zstd
69+
}
70+
71+
# Main Domain - serves frontend and auth
272
{$DOMAIN:localhost} {
373
# Logging
474
log {

internal/server/routes.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func (s *Server) RegisterRouter() http.Handler {
2323
router := gin.Default()
2424

2525
router.Use(cors.New(cors.Config{
26-
AllowOrigins: []string{"http://localhost:4000", "https://trakrlog.com", "https://www.trakrlog.com"}, // Add your frontend URL
26+
AllowOrigins: []string{"http://localhost:4000", "https://trakrlog.com", "https://www.trakrlog.com", "https://api.trakrlog.com"}, // Add your frontend URL
2727
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
28-
AllowHeaders: []string{"Accept", "Authorization", "Content-Type"},
28+
AllowHeaders: []string{"Accept", "Authorization", "Content-Type", "X-API-KEY"},
2929
AllowCredentials: true, // Enable cookies/auth
3030
}))
3131

@@ -88,6 +88,14 @@ func (s *Server) RegisterRouter() http.Handler {
8888
api.DELETE("/events/:id", eventHandler.DeleteEvent)
8989
}
9090

91+
// Track endpoint - supports both /track and /api/track for backwards compatibility
92+
track := router.Group("/track")
93+
track.Use(middleware.RequireAuthApiKey(s.userService))
94+
{
95+
track.POST("/", handler.NewEventHandler(s.eventService).CreateEvent)
96+
}
97+
98+
// Legacy endpoint - keeping for backwards compatibility
9199
apiTrack := router.Group("/api/track")
92100
apiTrack.Use(middleware.RequireAuthApiKey(s.userService))
93101
{

0 commit comments

Comments
 (0)