-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·123 lines (100 loc) · 3.58 KB
/
main.go
File metadata and controls
executable file
·123 lines (100 loc) · 3.58 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-chi/chi"
"github.com/joho/godotenv"
"github.com/lordmoma/chirpy/internal/config"
"github.com/lordmoma/chirpy/internal/database"
"github.com/lordmoma/chirpy/internal/handlers"
"github.com/lordmoma/chirpy/internal/middleware"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
jwtSecret := os.Getenv("JWT_SECRET")
apikey := os.Getenv("APIKey")
apiCfg := &config.ApiConfig{
FileserverHits: 0,
JwtSecret: jwtSecret,
APIKey: apikey,
}
// fmt.Printf("JWT_SECRET: %s\n", apiCfg.JwtSecret)
// use flag package in Go to parse command line flags
debug := flag.Bool("debug", false, "enable debugging") // create a boolean value for the --debug flag
flag.Parse() // parse the command line flags
if *debug { // check the value of the debug flag
fmt.Println("Debugging enabled")
} else {
fmt.Println("Debugging disabled")
}
const filepathRoot = "."
const port = "8080"
// Create a new apiConfig struct to hold the request count
// apiCfg := &config.ApiConfig{}
// Create a new Database
db, err := database.NewDB("database.json")
if err != nil {
panic(err)
}
if db == nil {
panic("Failed to open database file")
}
defer os.Remove("database.json")
// Create a new router for the /api namespace
apiRouter := chi.NewRouter()
apiRouter.Get("/healthz", handlers.HealthzHandler)
apiRouter.Post("/chirps", handlers.CreateChirpsHandler(db, apiCfg))
apiRouter.Get("/chirps", handlers.GetChirpsHandler(db))
apiRouter.Get("/chirps/{id}", handlers.GetChirpIDHandler(db))
apiRouter.Delete("/chirps/{id}", handlers.DeleteChirpIDHandler(db, apiCfg))
// create users for /api namespaces
apiRouter.Post("/users", handlers.CreateUserHandler(db))
apiRouter.Put("/users", handlers.UpdateUserHandler(db, apiCfg))
apiRouter.Post("/login", handlers.LoginHandler(db, apiCfg))
// create access token with refresh token for /api namespaces
apiRouter.Post("/refresh", handlers.AccessTokenHandler(db, apiCfg))
// revoke the access token for /api namespaces
apiRouter.Post("/revoke", handlers.RevokeTokenHandler(db, apiCfg))
// create a webhook for /api namespaces
apiRouter.Post("/polka/webhooks", handlers.WebhookHandler(db, apiCfg))
// create a new router for the admin
adminRouter := chi.NewRouter()
adminRouter.Get("/metrics", handlers.MetricsHandler(apiCfg))
// Mount the apiRouter at /api in the main router
r := chi.NewRouter()
r.Mount("/api", apiRouter)
r.Mount("/admin", adminRouter)
// Serve static files from the root directory and add the middleware to track metrics
r.Mount("/", middleware.MiddlewareMetricsInc(http.FileServer(http.Dir(filepathRoot)), apiCfg))
// Wrap the mux in a custom middleware function that adds CORS headers to the response
corsMux := middleware.MiddlewareCors(r)
// Create a new http.Server and use the corsMux as the handler
srv := &http.Server{
Addr: ":" + port,
Handler: corsMux,
}
// Use the server's ListenAndServe method to start the server
log.Printf("Serving files from %s on port: %s\n", filepathRoot, port)
// log.Fatal(srv.ListenAndServe())
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Println("ListenAndServe:", err)
}
// Set up an operating system signal handler to capture the Ctrl+C signal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
// Wait for the signal
<-signalChan
// Shutdown the server gracefully
if err := srv.Shutdown(context.Background()); err != nil {
fmt.Println(err)
}
}