-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
277 lines (235 loc) · 11.1 KB
/
main.go
File metadata and controls
277 lines (235 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"CarBN/common"
"CarBN/feed"
"CarBN/friends"
"CarBN/likes"
"CarBN/login"
"CarBN/postgres"
"CarBN/scan"
"CarBN/subscription"
"CarBN/trade"
"CarBN/user"
"context"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
type userContextKey string
const UserContextKey userContextKey = "UserID"
// LoggerMiddleware injects the logger into the context
func LoggerMiddleware(logger *log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Always ensure we have a valid logger
contextLogger := logger
if contextLogger == nil {
contextLogger = log.New(os.Stdout, "[CarBN] ", log.LstdFlags)
}
ctx := context.WithValue(r.Context(), common.LoggerCtxKey, contextLogger)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
func Chain(handler http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
for i := len(middleware) - 1; i >= 0; i-- {
handler = middleware[i](handler)
}
return handler
}
// Simplify the imageFileHandler since we're now using consistent paths
func imageFileHandler(fs http.FileSystem) http.HandlerFunc {
fileServer := http.FileServer(fs)
return func(w http.ResponseWriter, r *http.Request) {
// Save original path and strip /images/ prefix
path := strings.TrimPrefix(r.URL.Path, "/images/")
// URL decode the path to handle spaces and special characters
decodedPath, err := url.QueryUnescape(path)
if err != nil {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
// Update the request path without the /images/ prefix
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = decodedPath
fileServer.ServeHTTP(w, r2)
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Initialize logger
logger := log.New(os.Stdout, "[CarBN] ", log.LstdFlags)
// Create root context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Add logger to context
ctx = context.WithValue(ctx, common.LoggerCtxKey, logger)
// Initialize database
if err := postgres.InitDB(ctx); err != nil {
logger.Fatalf("Database initialization failed: %v", err)
}
defer postgres.CloseDB(ctx)
logger.Println("Database connection established")
// Initialize services
loginSvc := login.NewService(postgres.DB, login.Config{
JWTSecret: []byte(os.Getenv("JWT_SECRET")),
AccessTokenExpiry: 15 * time.Minute,
RefreshTokenExpiry: 30 * 24 * time.Hour,
GoogleClientID: os.Getenv("GOOGLE_CLIENT_ID"),
AppleTeamID: os.Getenv("APPLE_TEAM_ID"),
AppleServiceID: os.Getenv("APPLE_SERVICE_ID"),
AppleKeyID: os.Getenv("APPLE_KEY_ID"),
ApplePrivateKey: []byte(os.Getenv("APPLE_PRIVATE_KEY")),
})
userSvc := user.NewService(postgres.DB, os.Getenv("GENERATED_SAVE_DIR"))
subscriptionSvc := subscription.NewSubscriptionService(postgres.DB) // Add subscription service
feedSvc := feed.NewService(postgres.DB)
friendsSvc := friends.NewService(postgres.DB, feedSvc)
tradeSvc := trade.NewService(postgres.DB, feedSvc, subscriptionSvc) // Add subscription service to trade
likesSvc := likes.NewService(postgres.DB)
scanSvc := scan.NewService(postgres.DB, feedSvc, subscriptionSvc) // Add subscription service to scan
// Initialize handlers
loginHandler := login.NewHTTPHandler(loginSvc)
userHandler := user.NewHTTPHandler(userSvc)
subscriptionHandler := subscription.NewSubscriptionHandler(subscriptionSvc) // Add subscription handler
friendsHandler := friends.NewHTTPHandler(friendsSvc)
feedHandler := feed.NewHTTPHandler(feedSvc)
tradeHandler := trade.NewHTTPHandler(tradeSvc)
scanHandler := scan.NewHTTPHandler(scanSvc)
likesHandler := likes.NewHandler(likesSvc)
// Setup router
mux := http.NewServeMux()
// Only serve images in development mode
if os.Getenv("CARBN_DEV") == "development" {
// Serve image files from root images directory
imageDir := http.Dir("_resources/images")
mux.HandleFunc("/images/", imageFileHandler(imageDir))
}
// Auth routes
mux.HandleFunc("POST /auth/google", loginHandler.HandleGoogleSignIn)
mux.HandleFunc("POST /auth/apple", loginHandler.HandleAppleSignIn)
mux.HandleFunc("POST /auth/apple/updates", loginHandler.HandleAppleEmailUpdateNotification)
mux.HandleFunc("POST /auth/refresh", loginHandler.HandleRefresh)
mux.HandleFunc("POST /auth/logout", loginHandler.HandleLogout)
// Protected routes
mux.HandleFunc("GET /user/{user_id}/cars", loginSvc.AuthMiddleware(userHandler.HandleGetCarCollection))
mux.HandleFunc("GET /user/cars", loginSvc.AuthMiddleware(userHandler.HandleGetSpecificUserCars))
mux.HandleFunc("GET /user/friend-requests", loginSvc.AuthMiddleware(userHandler.HandleGetPendingFriendRequests))
mux.HandleFunc("POST /user/profile/picture", loginSvc.AuthMiddleware(userHandler.HandleUploadProfilePicture))
mux.HandleFunc("POST /user/profile/display-name", loginSvc.AuthMiddleware(userHandler.HandleUpdateDisplayName))
mux.HandleFunc("GET /user/{user_id}/details", loginSvc.AuthMiddleware(userHandler.HandleGetUserProfile))
mux.HandleFunc("GET /user/search", loginSvc.AuthMiddleware(userHandler.HandleSearchUsers))
mux.HandleFunc("GET /user/{user_id}/friends", loginSvc.AuthMiddleware(friendsHandler.HandleGetFriends))
mux.HandleFunc("GET /user/{user_id}/is-friend", loginSvc.AuthMiddleware(friendsHandler.HandleCheckFriendship))
mux.HandleFunc("POST /user/cars/{user_car_id}/sell", loginSvc.AuthMiddleware(userHandler.HandleSellCar))
// Car sharing endpoints
mux.HandleFunc("POST /user/cars/{user_car_id}/share", loginSvc.AuthMiddleware(userHandler.HandleCreateShareLink))
// Reorder and fix route patterns:
// 1. First handle /share/static/ routes with a more specific pattern to avoid conflicts
mux.HandleFunc("GET /share/static/{filename}", func(w http.ResponseWriter, r *http.Request) {
// Extract filename from path
filename := r.PathValue("filename")
if filename == "" {
http.NotFound(w, r)
return
}
// Set proper MIME types
switch {
case strings.HasSuffix(filename, ".css"):
w.Header().Set("Content-Type", "text/css")
case strings.HasSuffix(filename, ".js"):
w.Header().Set("Content-Type", "application/javascript")
}
// Create request with path pointing to the file
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = "/" + filename
logger.Printf("Serving static file: %s", filename)
http.FileServer(http.Dir("./shared_car")).ServeHTTP(w, r2)
})
// 2. Then register the remaining share routes with updated paths to avoid conflicts
mux.HandleFunc("GET /share/token/{share_token}/data", userHandler.HandleGetSharedCar) // No auth required
mux.HandleFunc("GET /share/token/{share_token}", userHandler.HandleServeSharePage) // No auth required
mux.HandleFunc("GET /user/cars/{user_car_id}/upgrades", loginSvc.AuthMiddleware(userHandler.HandleGetCarUpgrades))
mux.HandleFunc("POST /user/cars/{user_car_id}/upgrade-image", loginSvc.AuthMiddleware(userHandler.HandleUpgradeCarImage))
mux.HandleFunc("POST /user/cars/{user_car_id}/revert-image", loginSvc.AuthMiddleware(userHandler.HandleRevertCarImage))
mux.HandleFunc("POST /friends/request", loginSvc.AuthMiddleware(friendsHandler.HandleSendFriendRequest))
mux.HandleFunc("POST /friends/respond", loginSvc.AuthMiddleware(friendsHandler.HandleFriendRequestResponse))
mux.HandleFunc("GET /feed", loginSvc.AuthMiddleware(feedHandler.HandleGetFeed))
mux.HandleFunc("GET /feed/{feed_item_id}", loginSvc.AuthMiddleware(feedHandler.HandleGetFeedItem))
mux.HandleFunc("POST /trade/request", loginSvc.AuthMiddleware(tradeHandler.HandleCreateTrade))
mux.HandleFunc("POST /trade/respond", loginSvc.AuthMiddleware(tradeHandler.HandleTradeRequestResponse))
mux.HandleFunc("GET /trade/history", loginSvc.AuthMiddleware(tradeHandler.HandleGetUserTrades))
mux.HandleFunc("GET /trade/{trade_id}", loginSvc.AuthMiddleware(tradeHandler.HandleGetTrade))
mux.HandleFunc("POST /scan", loginSvc.AuthMiddleware(scanHandler.HandleScanPost))
// Likes routes
mux.HandleFunc("POST /likes/{feedItemId}", loginSvc.AuthMiddleware(likesHandler.CreateFeedItemLike))
mux.HandleFunc("DELETE /likes/{feedItemId}", loginSvc.AuthMiddleware(likesHandler.DeleteFeedItemLike))
mux.HandleFunc("GET /likes/feed-item/{feedItemId}", loginSvc.AuthMiddleware(likesHandler.GetFeedItemLikes))
mux.HandleFunc("GET /likes/feed-item/{feedItemId}/check", loginSvc.AuthMiddleware(likesHandler.CheckFeedItemLike))
mux.HandleFunc("GET /likes/user/{userId}", loginSvc.AuthMiddleware(likesHandler.GetUserReceivedLikes))
// Car likes routes
mux.HandleFunc("POST /likes/car/{userCarId}", loginSvc.AuthMiddleware(likesHandler.CreateUserCarLike))
mux.HandleFunc("DELETE /likes/car/{userCarId}", loginSvc.AuthMiddleware(likesHandler.DeleteUserCarLike))
mux.HandleFunc("GET /likes/car/{userCarId}", loginSvc.AuthMiddleware(likesHandler.GetUserCarLikes))
mux.HandleFunc("GET /likes/car/{userCarId}/count", loginSvc.AuthMiddleware(likesHandler.GetUserCarLikesCount))
mux.HandleFunc("GET /likes/car/{userCarId}/check", loginSvc.AuthMiddleware(likesHandler.CheckUserCarLike))
// Account management
mux.HandleFunc("DELETE /user/account", loginSvc.AuthMiddleware(userHandler.HandleDeleteAccount))
// Add subscription routes
mux.HandleFunc("GET /user/subscription", loginSvc.AuthMiddleware(subscriptionHandler.HandleGetUserSubscription))
mux.HandleFunc("GET /user/{user_id}/subscription/status", loginSvc.AuthMiddleware(subscriptionHandler.HandleGetUserSubscriptionStatus))
mux.HandleFunc("POST /subscription/purchase", loginSvc.AuthMiddleware(subscriptionHandler.HandlePurchaseSubscription))
mux.HandleFunc("POST /scanpack/purchase", loginSvc.AuthMiddleware(subscriptionHandler.HandlePurchaseScanPack))
mux.HandleFunc("GET /subscription/products", loginSvc.AuthMiddleware(subscriptionHandler.HandleGetSubscriptionProducts))
// App Store Server Notifications V2 webhook - not protected by auth middleware
mux.HandleFunc("POST /webhook/apple/subscription", subscriptionHandler.HandleAppStoreNotification)
// Initialize server
server := &http.Server{
Addr: ":8080",
Handler: LoggerMiddleware(logger)(
common.RequestIDMiddleware(
common.RecoveryMiddleware(
common.TimeoutMiddleware(240 * time.Second)(mux),
),
),
),
ReadTimeout: 240 * time.Second,
WriteTimeout: 240 * time.Second,
IdleTimeout: 240 * time.Second,
}
// Start server in a goroutine
go func() {
logger.Printf("Server starting on %s", server.Addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("Server failed to start: %v", err)
}
}()
// Wait for interrupt signal
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
// Shutdown gracefully
logger.Println("Initiating server shutdown...")
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 10*time.Second)
defer shutdownCancel()
if err := server.Shutdown(shutdownCtx); err != nil {
logger.Printf("Error during server shutdown: %v", err)
}
logger.Println("Server shutdown complete")
}