-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
910 lines (788 loc) · 24.5 KB
/
main.go
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"sync/atomic"
"time"
"github.com/1729prashant/microbloggingplatform/internal/auth"
"github.com/1729prashant/microbloggingplatform/internal/database"
"github.com/google/uuid"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
const HTTP_SERVER_PORT = "8080"
// Add these new types at the top with your other structs
type BlogPost struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Body string `json:"body"`
UserID uuid.UUID `json:"user_id"`
}
type CreateBlogPostRequest struct {
Body string `json:"body"`
UserID uuid.UUID `json:"user_id"`
}
// Convert database.BlogPost to main.BlogPost
func databaseBlogPostToBlogPost(dbBlogPost database.Blogpost) BlogPost {
return BlogPost{
ID: dbBlogPost.ID,
CreatedAt: dbBlogPost.CreatedAt,
UpdatedAt: dbBlogPost.UpdatedAt,
Body: dbBlogPost.Body,
UserID: dbBlogPost.UserID,
}
}
// New handler for creating BlogPosts
func (cfg *apiConfig) createBlogPostHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
// Get and validate JWT
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Missing or invalid token")
return
}
userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Invalid token")
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to read request body")
return
}
var req CreateBlogPostRequest
err = json.Unmarshal(body, &req)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request body")
return
}
// Validate BlogPost length
if len(req.Body) > 140 {
respondWithError(w, http.StatusBadRequest, "BlogPost is too long")
return
}
// Clean the BlogPost
cleanedBody := cleanChirp(req.Body)
// Create the BlogPost
dbBlogPost, err := cfg.db.CreateBlogPost(r.Context(), database.CreateBlogPostParams{
Body: cleanedBody,
UserID: userID, // Use the ID from the JWT
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to create BlogPost")
return
}
// Convert and respond with the BlogPost
BlogPost := databaseBlogPostToBlogPost(dbBlogPost)
respondWithJSON(w, http.StatusCreated, BlogPost)
}
// Handler for fetching all BlogPosts
func (cfg *apiConfig) GetAllBlogPostsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
/*
// Create the BlogPost
dbBlogPosts, err := cfg.db.GetAllBlogPosts(r.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to fetch posts")
return
}
blogPostsResponse := make([]BlogPost, len(dbBlogPosts))
for i, post := range dbBlogPosts {
blogPostsResponse[i] = databaseBlogPostToBlogPost(post)
}
*/
// Extract query parameters
authorIDStr := r.URL.Query().Get("author_id")
sortOrder := r.URL.Query().Get("sort")
// Default to "asc" if no sort parameter is provided
if sortOrder == "" {
sortOrder = "asc"
}
// Validate sortOrder to ensure it's either 'asc' or 'desc'
if sortOrder != "asc" && sortOrder != "desc" {
respondWithError(w, http.StatusBadRequest, "Invalid sort value")
return
}
// Convert author_id to UUID if present
var authorID uuid.UUID
if authorIDStr != "" {
parsedID, err := uuid.Parse(authorIDStr)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid author_id format")
return
}
authorID = parsedID
}
// Fetch blogposts from the database, passing authorID and sortOrder
if sortOrder == "desc" && authorIDStr != "" {
dbBlogPosts, err := cfg.db.GetBlogPostsDesc(r.Context(), authorID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to fetch chirps")
return
}
blogPostsResponse := make([]BlogPost, len(dbBlogPosts))
for i, post := range dbBlogPosts {
blogPostsResponse[i] = databaseBlogPostToBlogPost(post)
}
respondWithJSON(w, http.StatusOK, blogPostsResponse)
}
if (sortOrder == "asc" || sortOrder == "") && authorIDStr != "" {
dbBlogPosts, err := cfg.db.GetBlogPostsAsc(r.Context(), authorID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to fetch chirps")
return
}
blogPostsResponse := make([]BlogPost, len(dbBlogPosts))
for i, post := range dbBlogPosts {
blogPostsResponse[i] = databaseBlogPostToBlogPost(post)
}
respondWithJSON(w, http.StatusOK, blogPostsResponse)
}
if sortOrder == "desc" && authorIDStr == "" {
dbBlogPosts, err := cfg.db.GetAllBlogPostsDesc(r.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to fetch chirps")
return
}
blogPostsResponse := make([]BlogPost, len(dbBlogPosts))
for i, post := range dbBlogPosts {
blogPostsResponse[i] = databaseBlogPostToBlogPost(post)
}
respondWithJSON(w, http.StatusOK, blogPostsResponse)
}
if (sortOrder == "asc" || sortOrder == "") && authorIDStr == "" {
dbBlogPosts, err := cfg.db.GetAllBlogPosts(r.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to fetch chirps")
return
}
blogPostsResponse := make([]BlogPost, len(dbBlogPosts))
for i, post := range dbBlogPosts {
blogPostsResponse[i] = databaseBlogPostToBlogPost(post)
}
respondWithJSON(w, http.StatusOK, blogPostsResponse)
}
}
// In your main.go, add this new handler
func (cfg *apiConfig) getChirpByIDHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
chirpID := strings.TrimPrefix(r.URL.Path, "/api/chirps/")
if chirpID == "" {
respondWithError(w, http.StatusBadRequest, "Missing chirp ID")
return
}
// Parse the UUID
chirpUUID, err := uuid.Parse(chirpID)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid chirp ID")
return
}
// Get the chirp from the database
dbBlogPost, err := cfg.db.GetBlogPost(r.Context(), chirpUUID)
if err != nil {
if err == sql.ErrNoRows {
respondWithError(w, http.StatusNotFound, "Chirp not found")
return
}
respondWithError(w, http.StatusInternalServerError, "Failed to fetch chirp")
return
}
// Convert and respond with the chirp
chirp := databaseBlogPostToBlogPost(dbBlogPost)
respondWithJSON(w, http.StatusOK, chirp)
}
// User struct for JSON responses
type User struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Email string `json:"email"`
IsChirpyRed bool `json:"is_chirpy_red"`
}
// Struct to hold stateful, in-memory data
type apiConfig struct {
fileserverHits atomic.Int32
db *database.Queries
platform string
jwtSecret string
polkaKey string
}
// Request structs
type CreateUserRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
// Convert database.User to main.User
func databaseUserToUser(dbUser database.User) User {
// Check if the sql.NullBool is valid and return its value, else default to false
isChirpyRed := false
if dbUser.IsChirpyRed.Valid {
isChirpyRed = dbUser.IsChirpyRed.Bool
}
return User{
ID: dbUser.ID,
CreatedAt: dbUser.CreatedAt,
UpdatedAt: dbUser.UpdatedAt,
Email: dbUser.Email,
IsChirpyRed: isChirpyRed,
}
}
// Handler for user creation
func (cfg *apiConfig) createUserHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to read request body")
return
}
var req CreateUserRequest
err = json.Unmarshal(body, &req)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request body")
return
}
if req.Email == "" {
respondWithError(w, http.StatusBadRequest, "Email is required")
return
}
hashedPassword, err := auth.HashPassword(req.Password)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to hash password")
return
}
dbUser, err := cfg.db.CreateUser(r.Context(), database.CreateUserParams{
Email: req.Email,
HashedPassword: hashedPassword,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to create user")
return
}
user := databaseUserToUser(dbUser)
respondWithJSON(w, http.StatusCreated, user)
}
// Updated reset handler with user deletion
func (cfg *apiConfig) resetHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Check if platform is dev
if cfg.platform != "dev" {
respondWithError(w, http.StatusForbidden, "This endpoint is only available in development mode")
return
}
// Delete all users
err := cfg.db.DeleteAllUsers(r.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to reset users")
return
}
// Reset the hits counter
cfg.fileserverHits.Store(0)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("Reset successful"))
}
// Request and response structs for the validate_chirp endpoint
type ValidateChirpRequest struct {
Body string `json:"body"`
}
type ValidateChirpResponse struct {
CleanedBody string `json:"cleaned_body"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
// Helper function to respond with JSON
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, err := json.Marshal(payload)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Something went wrong")
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
// Helper function to respond with error
func respondWithError(w http.ResponseWriter, code int, msg string) {
respondWithJSON(w, code, ErrorResponse{Error: msg})
}
// Helper function to clean profane words from text
func cleanChirp(body string) string {
profaneWords := map[string]bool{
"kerfuffle": true,
"sharbert": true,
"fornax": true,
}
words := strings.Split(body, " ")
for i, word := range words {
// Convert to lowercase for comparison, but keep original for non-matches
wordLower := strings.ToLower(word)
if profaneWords[wordLower] {
words[i] = "****"
}
}
return strings.Join(words, " ")
}
// Middleware to increment the fileserverHits counter
func (cfg *apiConfig) middlewareMetricsInc(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cfg.fileserverHits.Add(1)
next.ServeHTTP(w, r)
})
}
// Handler to return the metrics page as HTML
func (cfg *apiConfig) metricsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
hits := cfg.fileserverHits.Load()
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>`, hits)
}
// Readiness endpoint to check server health (GET only)
func readinessHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
// Add new login request struct
type LoginRequest struct {
Password string `json:"password"`
Email string `json:"email"`
ExpiresInSeconds int `json:"expires_in_seconds,omitempty"`
}
// Add response struct with token
type LoginResponse struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Email string `json:"email"`
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
IsChirpyRed bool `json:"is_chirpy_red"`
}
func (cfg *apiConfig) loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to read request body")
return
}
var req LoginRequest
err = json.Unmarshal(body, &req)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request body")
return
}
if req.Email == "" {
respondWithError(w, http.StatusBadRequest, "Email is required")
return
}
// Get user from database
dbUser, err := cfg.db.GetEncryptedPassword(r.Context(), req.Email)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Incorrect email or password")
return
}
// Check password
if err := auth.CheckPasswordHash(req.Password, dbUser.HashedPassword); err != nil {
respondWithError(w, http.StatusUnauthorized, "Incorrect email or password")
return
}
// Create JWT (expires in 1 hour)
token, err := auth.MakeJWT(dbUser.ID, cfg.jwtSecret, time.Hour)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to create token")
return
}
// Create Refresh Token (expires in 60 days)
refreshToken, err := auth.MakeRefreshToken()
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to create refresh token")
return
}
// Insert Refresh Token into database
expiresAt := sql.NullTime{Time: time.Now().AddDate(0, 2, 0), Valid: true} // 60 days
revokedAt := sql.NullTime{} // Null
_, err = cfg.db.InsertRefreshToken(r.Context(), database.InsertRefreshTokenParams{
Token: refreshToken,
UserID: dbUser.ID,
ExpiresAt: expiresAt,
RevokedAt: revokedAt,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to store refresh token")
return
}
// Respond with tokens
response := LoginResponse{
ID: dbUser.ID,
CreatedAt: dbUser.CreatedAt,
UpdatedAt: dbUser.UpdatedAt,
Email: dbUser.Email,
Token: token,
RefreshToken: refreshToken,
IsChirpyRed: dbUser.IsChirpyRed.Bool,
}
respondWithJSON(w, http.StatusOK, response)
}
func (cfg *apiConfig) refreshHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Missing or invalid token")
return
}
// Look up the refresh token in the database
dbToken, err := cfg.db.GetRefreshToken(r.Context(), token)
if err != nil || !dbToken.ExpiresAt.Valid || dbToken.ExpiresAt.Time.Before(time.Now()) || dbToken.RevokedAt.Valid {
respondWithError(w, http.StatusUnauthorized, "Invalid or expired refresh token")
return
}
// Create a new JWT (expires in 1 hour)
newToken, err := auth.MakeJWT(dbToken.UserID, cfg.jwtSecret, time.Hour)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to create token")
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"token": newToken})
}
func (cfg *apiConfig) revokeHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Missing or invalid token")
return
}
// Revoke the token
err = cfg.db.RevokeRefreshToken(r.Context(), database.RevokeRefreshTokenParams{
Token: token,
RevokedAt: sql.NullTime{Time: time.Now(), Valid: true},
UpdatedAt: time.Now(),
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to revoke token")
return
}
w.WriteHeader(http.StatusNoContent) // 204 No Content
}
func (cfg *apiConfig) updateUserHandler(w http.ResponseWriter, r *http.Request) {
// Ensure the request method is PUT
if r.Method != http.MethodPut {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
// Validate the access token
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Missing or invalid token")
return
}
// Get the user ID from the access token
userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Invalid token")
return
}
// Read and parse the request body
body, err := io.ReadAll(r.Body)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to read request body")
return
}
var req struct {
Email string `json:"email"`
Password string `json:"password"`
}
err = json.Unmarshal(body, &req)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request body")
return
}
// Validate the inputs
if req.Email == "" {
respondWithError(w, http.StatusBadRequest, "Email is required")
return
}
if req.Password == "" {
respondWithError(w, http.StatusBadRequest, "Password is required")
return
}
// Hash the new password
hashedPassword, err := auth.HashPassword(req.Password)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to hash password")
return
}
// Update the user's email and password in the database
err = cfg.db.UpdateUser(r.Context(), database.UpdateUserParams{
ID: userID,
Email: req.Email,
HashedPassword: hashedPassword,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to update user")
return
}
// Fetch the updated user
dbUser, err := cfg.db.GetUser(r.Context(), userID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to fetch updated user")
return
}
// Convert and respond with the updated user (excluding password)
user := User{
ID: dbUser.ID,
CreatedAt: dbUser.CreatedAt,
UpdatedAt: dbUser.UpdatedAt,
Email: dbUser.Email,
}
respondWithJSON(w, http.StatusOK, user)
}
func (cfg *apiConfig) deleteChirpHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
// Extract the chirpID from the URL
chirpID := strings.TrimPrefix(r.URL.Path, "/api/chirps/")
if chirpID == "" {
respondWithError(w, http.StatusBadRequest, "Missing chirp ID")
return
}
// Parse chirpID as UUID
chirpUUID, err := uuid.Parse(chirpID)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid chirp ID")
return
}
// Validate the access token
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Missing or invalid token")
return
}
// Get the user ID from the access token
userID, err := auth.ValidateJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Invalid token")
return
}
// Check if the chirp exists and belongs to the user
chirp, err := cfg.db.GetBlogPost(r.Context(), chirpUUID)
if err != nil {
if err == sql.ErrNoRows {
respondWithError(w, http.StatusNotFound, "Chirp not found")
return
}
respondWithError(w, http.StatusInternalServerError, "Failed to fetch chirp")
return
}
// Check if the authenticated user is the owner
if chirp.UserID != userID {
respondWithError(w, http.StatusForbidden, "You are not the author of this chirp")
return
}
// Delete the chirp
err = cfg.db.DeleteBlogPost(r.Context(), database.DeleteBlogPostParams{
ID: chirpUUID,
UserID: chirp.UserID,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to delete chirp")
return
}
// Respond with 204 No Content
w.WriteHeader(http.StatusNoContent)
}
func (cfg *apiConfig) polkaWebhookHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
return
}
// Extract API key from the Authorization header
apiKey, err := auth.GetAPIKey(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Invalid API key")
return
}
// Check if the API key matches the one stored in the config
if apiKey != cfg.polkaKey {
respondWithError(w, http.StatusUnauthorized, "Unauthorized")
return
}
// Parse the request body
body, err := io.ReadAll(r.Body)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request body")
return
}
var req struct {
Event string `json:"event"`
Data struct {
UserID string `json:"user_id"`
} `json:"data"`
}
err = json.Unmarshal(body, &req)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid JSON format")
return
}
// Ignore events other than "user.upgraded"
if req.Event != "user.upgraded" {
w.WriteHeader(http.StatusNoContent)
return
}
// Parse the user ID
userID, err := uuid.Parse(req.Data.UserID)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid user ID format")
return
}
// Upgrade the user to Chirpy Red
err = cfg.db.UpgradeToChirpyRed(r.Context(), userID)
if err != nil {
if err == sql.ErrNoRows {
respondWithError(w, http.StatusNotFound, "User not found")
return
}
respondWithError(w, http.StatusInternalServerError, "Failed to upgrade user")
return
}
// Respond with 204 No Content
w.WriteHeader(http.StatusNoContent)
}
func main() {
godotenv.Load()
dbURL := os.Getenv("DB_URL")
platform := os.Getenv("PLATFORM")
jwtSecret := os.Getenv("JWT_SECRET")
polkaKey := os.Getenv("POLKA_KEY")
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer db.Close()
// Initialize database queries
dbQueries := database.New(db)
// Initialize apiConfig to hold stateful data
apiCfg := &apiConfig{
db: dbQueries,
platform: platform,
jwtSecret: jwtSecret,
polkaKey: polkaKey,
}
// Create a new ServeMux
mux := http.NewServeMux()
// Create and configure the HTTP server
httpServer := &http.Server{
Addr: ":" + HTTP_SERVER_PORT,
Handler: mux,
}
// Add the API endpoints
mux.HandleFunc("/api/healthz", readinessHandler)
mux.HandleFunc("/api/chirps", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
apiCfg.GetAllBlogPostsHandler(w, r)
case http.MethodPost:
apiCfg.createBlogPostHandler(w, r)
default:
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
}
})
mux.HandleFunc("/api/chirps/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/chirps/" {
switch r.Method {
case http.MethodGet:
apiCfg.GetAllBlogPostsHandler(w, r)
case http.MethodPost:
apiCfg.createBlogPostHandler(w, r)
default:
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
}
return
}
// Handle specific chirp by ID (`/api/chirps/{chirpID}`)
chirpID := strings.TrimPrefix(r.URL.Path, "/api/chirps/")
if chirpID == "" {
respondWithError(w, http.StatusBadRequest, "Missing chirp ID")
return
}
switch r.Method {
case http.MethodGet:
apiCfg.getChirpByIDHandler(w, r)
case http.MethodDelete:
apiCfg.deleteChirpHandler(w, r)
default:
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
}
})
mux.HandleFunc("/api/users", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
apiCfg.createUserHandler(w, r) // Existing handler for POST
case http.MethodPut:
apiCfg.updateUserHandler(w, r) // New handler for PUT
default:
respondWithError(w, http.StatusMethodNotAllowed, "Method not allowed")
}
})
mux.HandleFunc("/api/login", apiCfg.loginHandler)
mux.HandleFunc("/api/refresh", apiCfg.refreshHandler)
mux.HandleFunc("/api/revoke", apiCfg.revokeHandler)
mux.HandleFunc("/api/polka/webhooks", apiCfg.polkaWebhookHandler)
mux.HandleFunc("/admin/metrics", apiCfg.metricsHandler)
mux.HandleFunc("/admin/reset", apiCfg.resetHandler)
// Keep the file server path at /app/ and wrap it with the middleware
fileServer := http.FileServer(http.Dir("./"))
mux.Handle("/app/", apiCfg.middlewareMetricsInc(http.StripPrefix("/app", fileServer)))
// Start the server
err = httpServer.ListenAndServe()
if err != nil {
panic(err) // Log error if the server fails to start
}
}