feat: Added chapter 3 and 4
This commit is contained in:
parent
c93e88b2fe
commit
141e1df0cb
71
chirp.go
Normal file
71
chirp.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func decode(w http.ResponseWriter, r *http.Request) {
|
||||
type parameters struct {
|
||||
Content string `json:"body"`
|
||||
}
|
||||
type returnVals struct {
|
||||
Data string `json:"cleaned_body"`
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
params := parameters{}
|
||||
err := decoder.Decode(¶ms)
|
||||
if err != nil {
|
||||
log.Printf("Error decoding parameters: %s", err)
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
} else if len(params.Content) > 140 {
|
||||
type errorVals struct {
|
||||
Data string `json:"error"`
|
||||
}
|
||||
|
||||
respBody := errorVals{
|
||||
Data: "Chirp is too long",
|
||||
}
|
||||
dat, err := json.Marshal(respBody)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling JSON: %s", err)
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(400)
|
||||
w.Write(dat)
|
||||
} else {
|
||||
cleanedData := cleanText(params.Content)
|
||||
respBody := returnVals{
|
||||
Data: cleanedData,
|
||||
}
|
||||
dat, err := json.Marshal(respBody)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling JSON: %s", err)
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
w.Write(dat)
|
||||
}
|
||||
}
|
||||
|
||||
func cleanText(s string) string {
|
||||
splittedString := strings.Split(s, " ")
|
||||
for idx, element := range splittedString {
|
||||
switch strings.ToLower(element) {
|
||||
case
|
||||
"kerfuffle",
|
||||
"sharbert",
|
||||
"fornax":
|
||||
splittedString[idx] = "****"
|
||||
}
|
||||
}
|
||||
return strings.Join(splittedString, " ")
|
||||
}
|
12
main.go
12
main.go
|
@ -12,16 +12,16 @@ type apiConfig struct {
|
|||
func main() {
|
||||
apiCfg := &apiConfig{}
|
||||
mux := http.NewServeMux()
|
||||
fileServer := http.FileServer(http.Dir("."))
|
||||
mux.Handle("/app", apiCfg.middlewareMetricsInc(http.StripPrefix("/app", fileServer)))
|
||||
mux.Handle("/app/assets/", http.StripPrefix("/app/assets/", http.FileServer(http.Dir("./assets/"))))
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, req *http.Request) {
|
||||
fsHandler := apiCfg.middlewareMetricsInc(http.StripPrefix("/app", http.FileServer(http.Dir("."))))
|
||||
mux.Handle("/app/", fsHandler)
|
||||
mux.HandleFunc("GET /api/healthz", func(w http.ResponseWriter, req *http.Request) {
|
||||
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("OK"))
|
||||
})
|
||||
mux.Handle("GET /metrics", http.HandlerFunc(apiCfg.serveMetrics))
|
||||
mux.Handle("POST /reset", http.HandlerFunc(apiCfg.serveReset))
|
||||
mux.Handle("GET /admin/metrics", http.HandlerFunc(apiCfg.serveMetrics))
|
||||
mux.Handle("POST /admin/reset", http.HandlerFunc(apiCfg.serveReset))
|
||||
mux.HandleFunc("POST /api/validate_chirp", decode)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: ":8080",
|
||||
|
|
10
metrics.go
10
metrics.go
|
@ -13,6 +13,14 @@ func (cfg *apiConfig) middlewareMetricsInc(next http.Handler) http.Handler {
|
|||
}
|
||||
|
||||
func (cfg *apiConfig) serveMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
hits := cfg.fileserverHits.Load()
|
||||
fmt.Fprintf(w, "Hits: %d", hits)
|
||||
template := `
|
||||
<html>
|
||||
<body>
|
||||
<h1>Welcome, Chirpy Admin</h1>
|
||||
<p>Chirpy has been visited %d times!</p>
|
||||
</body>
|
||||
</html>`
|
||||
fmt.Fprintf(w, template, hits)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user