-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (52 loc) · 1.72 KB
/
Copy pathmain.go
File metadata and controls
60 lines (52 loc) · 1.72 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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"sync/atomic"
"github.com/SamW94/chirpy/internal/database"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func main() {
godotenv.Load()
db, err := sql.Open("postgres", os.Getenv("DB_URL"))
if err != nil {
log.Fatal(err)
}
dbQueries := database.New(db)
const serverPort = "8080"
mux := http.NewServeMux()
apiCfg := apiConfig{
fileserverHits: atomic.Int32{},
DatabaseQueries: dbQueries,
Platform: os.Getenv("PLATFORM"),
JWTSecret: os.Getenv("JWT_SECRET"),
PolkaKey: os.Getenv("POLKA_KEY"),
}
mux.HandleFunc("GET /api/healthz", healthzHandler)
mux.HandleFunc("POST /admin/reset", apiCfg.resetHandler)
mux.HandleFunc("GET /admin/metrics", apiCfg.metricsHandler)
mux.HandleFunc("POST /api/users", apiCfg.createUserHandler)
mux.HandleFunc("PUT /api/users", apiCfg.updateUserHandler)
mux.HandleFunc("POST /api/login", apiCfg.loginHandler)
mux.HandleFunc("POST /api/chirps", apiCfg.createChirpHandler)
mux.HandleFunc("GET /api/chirps", apiCfg.getChirpsHandler)
mux.HandleFunc("GET /api/chirps/{chirp_id}", apiCfg.getChirpByIDHandler)
mux.HandleFunc("DELETE /api/chirps/{chirp_id}", apiCfg.deleteChirpByIDHandler)
mux.HandleFunc("POST /api/refresh", apiCfg.refreshHandler)
mux.HandleFunc("POST /api/revoke", apiCfg.revokeHandler)
mux.HandleFunc("POST /api/polka/webhooks", apiCfg.polkaWebHookHandler)
appPathHandler := http.StripPrefix("/app", http.FileServer(http.Dir(".")))
mux.Handle("/app/", apiCfg.middlewareFileServerHits(appPathHandler))
server := &http.Server{
Addr: ":" + serverPort,
Handler: mux,
}
log.Printf("Serving on port: %s", serverPort)
err = server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}