Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (49 loc) · 1.76 KB
/
Copy pathmain.go
File metadata and controls
65 lines (49 loc) · 1.76 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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"sync/atomic"
"github.com/devKiratu/chirpy/internal/database"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
var apiCfg = &apiConfig{fileServerHits: atomic.Int32{}}
func main(){
godotenv.Load()
dbUrl := os.Getenv("DB_URL")
jwtSecret := os.Getenv("JWT_SECRET")
polkaKey := os.Getenv("POLKA_KEY")
db, err := sql.Open("postgres", dbUrl)
if err!= nil {
log.Fatal(err)
}
dbQueries := database.New(db)
apiCfg.dbQueries = dbQueries
apiCfg.jwtSecret = jwtSecret
apiCfg.polkaKey = polkaKey
serveMux := http.NewServeMux()
port := "8080"
serveMux.Handle("/app/", apiCfg.middlewareMetricsInc(http.StripPrefix("/app",http.FileServer(http.Dir(".")))))
serveMux.HandleFunc("GET /api/healthz", handlerHealthz)
serveMux.HandleFunc("GET /admin/metrics", apiCfg.handlerMetrics)
serveMux.HandleFunc("POST /admin/reset", apiCfg.handlerReset)
serveMux.HandleFunc("POST /api/validate_chirp", handlerValidateChirp)
serveMux.HandleFunc("POST /api/users", handlerCreateUser)
serveMux.HandleFunc("POST /api/chirps", handlerCreateChirp)
serveMux.HandleFunc("GET /api/chirps", handlerGetAllChirps)
serveMux.HandleFunc("GET /api/chirps/{chirpId}", handlerGetChirpById)
serveMux.HandleFunc("POST /api/login", handlerLogin)
serveMux.HandleFunc("POST /api/refresh", handlerRefreshToken)
serveMux.HandleFunc("POST /api/revoke", handlerRevokeRefreshToken)
serveMux.HandleFunc("PUT /api/users", handlerUpdateUserDetails)
serveMux.HandleFunc("DELETE /api/chirps/{chirpID}", handlerDeleteChirpById)
serveMux.HandleFunc("POST /api/polka/webhooks", handlerPolkaWebhooks)
server := &http.Server{
Addr: ":" + port,
Handler: serveMux,
}
log.Printf("server running on port %v...\n",port)
log.Fatal(server.ListenAndServe())
}