-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
49 lines (44 loc) · 1.25 KB
/
admin.go
File metadata and controls
49 lines (44 loc) · 1.25 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
package main
/*Stuff related to admin routes*/
import (
"fmt"
"net/http"
"sync/atomic"
)
func (cfg *apiConfig) middlewareMetricsInc(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cfg.fileserverHits.Add(1)
next.ServeHTTP(w, r)
})
}
func (cfg *apiConfig) metrics(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
val := cfg.fileserverHits.Load()
template := `<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>
`
w.Write([]byte(fmt.Sprintf(template, val)))
}
func (cfg *apiConfig) reset(w http.ResponseWriter, req *http.Request) {
if cfg.platform != "dev" {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Forbidden"))
return
}
err := cfg.db.DeleteAllUsers(req.Context())
if err != nil {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Error deleting Users"))
}
cfg.fileserverHits = atomic.Int32{}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}