-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (63 loc) · 1.86 KB
/
Copy pathmain.go
File metadata and controls
74 lines (63 loc) · 1.86 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/AvivKermann/Chirpy/internal/database"
"github.com/go-chi/chi/v5"
"github.com/joho/godotenv"
)
const port = "8080"
const filepathRoot = "."
const dbFilePath = "./database.json"
type apiConfig struct {
fileServerHits int
DB *database.DB
jwtSecret string
apiKey string
}
func main() {
godotenv.Load()
router := chi.NewRouter()
apiRouter := chi.NewRouter()
adminRouter := chi.NewRouter()
router.Mount("/api", apiRouter)
router.Mount("/admin", adminRouter)
db, err := database.NewDB(dbFilePath)
if err != nil {
log.Fatal("db cannot be loaded")
}
cfg := apiConfig{
fileServerHits: 0,
DB: db,
jwtSecret: os.Getenv("JWT_SECRET"),
apiKey: os.Getenv("API_KEY"),
}
fsHandler := cfg.MiddlewareMetricsInc(http.StripPrefix("/app", http.FileServer(http.Dir(filepathRoot))))
router.Handle("/app", fsHandler)
router.Handle("/app/*", fsHandler)
apiRouter.Get("/healthz", handlerHealthz)
apiRouter.Get("/reset", cfg.handlerReset)
apiRouter.Get("/chirps", cfg.handlerGetChirps)
apiRouter.Get("/chirps/{chirpId}", cfg.handlerGetSingleChirp)
apiRouter.Post("/chirps", cfg.handlerCreateChirp)
apiRouter.Delete("/chirps/{chirpId}", cfg.handlerDeleteChirp)
apiRouter.Post("/users", cfg.handlerCreateUser)
apiRouter.Post("/login", cfg.handlerLogin)
apiRouter.Post("/refresh", cfg.handlerRefresh)
apiRouter.Post("/revoke", cfg.handlerRevoke)
apiRouter.Post("/polka/webhooks", cfg.handlerUserSubscribe)
apiRouter.Put("/users", cfg.handlerUpdateUser)
adminRouter.Get("/metrics", cfg.handlerMetrics)
fmt.Printf("started local host on http://localhost:%s\n", port)
corsMux := middlewareCors(router)
server := &http.Server{
Handler: corsMux,
Addr: ":" + port,
}
err = server.ListenAndServe()
if err != nil {
log.Fatal("server couldn't run")
}
}