-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (63 loc) · 2.02 KB
/
Copy pathmain.go
File metadata and controls
79 lines (63 loc) · 2.02 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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"sync/atomic"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/nk-reddy/chirpy/internal/database"
)
type apiConfig struct {
fileserverHits atomic.Int32
db *database.Queries
platform string
jwtSK string
polkaKey string
}
func main() {
const filepathRoot = "."
const port = "8080"
godotenv.Load()
jwt_sk := os.Getenv("JWT_SECRET_KEY")
polka_key := os.Getenv("POLKA_KEY")
dbURL := os.Getenv("DB_URL")
if dbURL == "" {
log.Fatal("DB_URL must be set")
}
dbConn, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatalf("Error opening database: %s", err)
}
dbQueries := database.New(dbConn)
apiCfg := apiConfig{
fileserverHits: atomic.Int32{},
db: dbQueries,
platform: os.Getenv("PLATFORM"),
jwtSK: jwt_sk,
polkaKey: polka_key,
}
mux := http.NewServeMux()
fsHandler := apiCfg.middlewareMetricsInc(http.StripPrefix("/app", http.FileServer(http.Dir(filepathRoot))))
mux.Handle("/app/", fsHandler)
mux.HandleFunc("POST /api/users", apiCfg.handlerCreateUser)
mux.HandleFunc("POST /api/chirps", apiCfg.handlerPostChirp)
mux.HandleFunc("POST /api/login", apiCfg.handlerUserLogin)
mux.HandleFunc("POST /api/refresh", apiCfg.handlerRefresh)
mux.HandleFunc("POST /api/revoke", apiCfg.handlerRevoke)
mux.HandleFunc("PUT /api/users", apiCfg.handlerUpdateUser)
mux.HandleFunc("GET /api/chirps", apiCfg.handlerGetAllChirps)
mux.HandleFunc("GET /api/chirps/{chirpID}", apiCfg.handlerGetChirp)
mux.HandleFunc("GET /api/healthz", handlerReadiness)
mux.HandleFunc("DELETE /api/chirps/{chirpID}", apiCfg.handlerDeleteChirp)
mux.HandleFunc("POST /api/polka/webhooks", apiCfg.handlerUpgradeUser)
mux.HandleFunc("POST /admin/reset", apiCfg.handlerReset)
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())
}