-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (116 loc) · 3.69 KB
/
main.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"sync/atomic"
"github.com/RodolfoCamposGlz/internal/database"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
const filepathRoot = "."
type apiConfig struct {
fileserverHits atomic.Int32
dbQueries *database.Queries
platform string
jwtSecret string
polkaKey string
}
func (cfg *apiConfig) middlewareMetricsInc(next http.Handler) http.Handler {
// Return an http.Handler that wraps the next handler
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Increment your metric
cfg.fileserverHits.Add(1)
// Call the next handler in the chain
next.ServeHTTP(w, r)
})
}
func (cfg *apiConfig) handlerMetrics(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=utf-8")
html := fmt.Sprintf(`
<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>
`, cfg.fileserverHits.Load())
w.WriteHeader(http.StatusOK)
w.Write([]byte(html))
}
func (cfg *apiConfig) handlerReadiness(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(http.StatusText(http.StatusOK)))
}
func (cfg *apiConfig) handlerReset(w http.ResponseWriter, r *http.Request) {
if cfg.platform != "dev" {
respondWithError(w, http.StatusForbidden, "Not allowed")
return
}
err := cfg.dbQueries.DeleteUsers(r.Context())
if err != nil {
log.Printf("Error: %v\n", err)
respondWithError(w, http.StatusInternalServerError, "Error deleting users")
return
}
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(http.StatusText(http.StatusOK)))
}
var handler http.Handler = http.StripPrefix("/app", http.FileServer(http.Dir(filepathRoot)))
func main (){
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
apiCfg := apiConfig{
fileserverHits: atomic.Int32{},
}
dbURL := os.Getenv("DB_URL")
platform := os.Getenv("PLATFORM")
jwtSecret := os.Getenv("JWT_SECRET")
polkaKey := os.Getenv("POLKA_KEY")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal(err)
}
dbQueries := database.New(db)
apiCfg.dbQueries = dbQueries
apiCfg.platform = platform
apiCfg.jwtSecret = jwtSecret
apiCfg.polkaKey = polkaKey
mux := http.NewServeMux()
// Create a new http.Server
server := &http.Server{
Addr: ":" + port, // Bind to port 8080
Handler: mux, // Use the ServeMux as the handler
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
w.WriteHeader(http.StatusOK)
})
mux.Handle("/app/",apiCfg.middlewareMetricsInc(handler))
mux.HandleFunc("GET /api/healthz", apiCfg.handlerReadiness)
mux.HandleFunc("POST /api/users", apiCfg.handlerCreateUser)
mux.HandleFunc("PUT /api/users", apiCfg.handlerUpdateUser)
mux.HandleFunc("POST /api/login", apiCfg.handlerLogin)
mux.HandleFunc("POST /api/refresh", apiCfg.handlerRefresh)
mux.HandleFunc("POST /api/revoke", apiCfg.handlerRevokeToken)
mux.HandleFunc("POST /api/chirps", apiCfg.handlerCreateChirp)
mux.HandleFunc("GET /api/chirps", apiCfg.handlerGetChirps)
mux.HandleFunc("GET /api/chirps/{chirpID}", apiCfg.handlerGetChirp)
mux.HandleFunc("DELETE /api/chirps/{chirpID}", apiCfg.handlerDeleteChirp)
mux.HandleFunc("POST /api/polka/webhooks", apiCfg.handlePolkaWebhooks)
mux.HandleFunc("GET /admin/metrics", apiCfg.handlerMetrics)
mux.HandleFunc("POST /admin/reset", apiCfg.handlerReset)
// Start the server
log.Printf("Serving files from %s on port: %s\n", filepathRoot, port)
log.Fatal(server.ListenAndServe())
}