-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (60 loc) · 1.85 KB
/
Copy pathmain.go
File metadata and controls
75 lines (60 loc) · 1.85 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
package main
import (
"flag"
"github.com/hawkaii/Chirpy-go/internal/database"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
)
type apiConfig struct {
fileserverHits int
DB *database.DB
jwtKey string
polkaKey string
}
func main() {
const filepathRoot = "."
const port = "8080"
db, err := database.NewDB("database.json")
if err != nil {
log.Fatal(err)
}
dbg := flag.Bool("debug", false, "Enable debug mode")
flag.Parse()
if dbg != nil && *dbg {
err := db.ResetDB()
if err != nil {
log.Fatal(err)
}
}
godotenv.Load()
apiCfg := apiConfig{
fileserverHits: 0,
DB: db,
jwtKey: os.Getenv("JWT_SECRET"),
polkaKey: os.Getenv("POLKA_SECRET"),
}
mux := http.NewServeMux()
fsHandler := apiCfg.middlewareMetricsInc(http.StripPrefix("/app", http.FileServer(http.Dir(filepathRoot))))
mux.Handle("/app/*", fsHandler)
mux.HandleFunc("GET /api/healthz", handlerReadiness)
mux.HandleFunc("GET /api/reset", apiCfg.handlerReset)
mux.HandleFunc("POST /api/chirps", apiCfg.handlerChirpsCreate)
mux.HandleFunc("GET /api/chirps", apiCfg.handlerChirpsRetrieve)
mux.HandleFunc("GET /api/chirps/{chirpID}", apiCfg.handlerChirpsGet)
mux.HandleFunc("DELETE /api/chirps/{chirpID}", apiCfg.handleChirpsDelete)
mux.HandleFunc("POST /api/users", apiCfg.handlerUsersCreate)
mux.HandleFunc("PUT /api/users", apiCfg.handlerUsersUpdate)
mux.HandleFunc("POST /api/login", apiCfg.handlerLogin)
mux.HandleFunc("POST /api/polka/webhooks", apiCfg.handlerPolkaUpgrade)
mux.HandleFunc("POST /api/refresh", apiCfg.handlerRefresh)
mux.HandleFunc("POST /api/revoke", apiCfg.handlerRevoke)
mux.HandleFunc("GET /admin/metrics", apiCfg.handlerMetrics)
srv := &http.Server{
Addr: ":" + port,
Handler: mux,
}
log.Printf("Serving files from %s on port: %s\n", filepathRoot, port)
log.Fatal(srv.ListenAndServe())
}