-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (73 loc) · 2.21 KB
/
main.go
File metadata and controls
93 lines (73 loc) · 2.21 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
package main
import (
"flag"
"log"
"net/http"
"os"
"os/signal"
"github.com/eliird/chirpy/internal/database"
"github.com/joho/godotenv"
)
type apiConfig struct {
fileserverHits int
DB *database.DB
jwtSecret string
polkaAPI string
}
func interruptRoutine(c chan os.Signal, dbToDelete string) {
<-c
os.Remove(dbToDelete)
os.Exit(1)
}
func main() {
const filepathRoot = "."
const port = "8080"
const dbName = "database.json"
sigInt := make(chan os.Signal, 1)
signal.Notify(sigInt, os.Interrupt)
dbg := flag.Bool("debug", false, "Enable debug mode")
flag.Parse()
if *dbg {
log.Printf("In Debug Mode will delete %s", dbName)
go interruptRoutine(sigInt, dbName)
defer os.Remove(dbName)
}
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
db, err := database.NewDB(dbName)
if err != nil {
log.Fatal(err)
}
jwtSecret := os.Getenv("JWT_SECRET")
polkaAPI := os.Getenv("API_KEY")
apiCfg := apiConfig{
fileserverHits: 0,
DB: db,
jwtSecret: jwtSecret,
polkaAPI: polkaAPI,
}
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/revoke", apiCfg.handlerRevoke)
mux.HandleFunc("POST /api/refresh", apiCfg.handlerRefresh)
mux.HandleFunc("POST /api/login", apiCfg.handlerLogin)
mux.HandleFunc("POST /api/users", apiCfg.handlerUsersCreate)
mux.HandleFunc("PUT /api/users", apiCfg.handlerUsersUpdate)
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.handlerChirpsDelete)
mux.HandleFunc("POST /api/polka/webhooks", apiCfg.handlerWebhookPolka)
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())
}