diff --git a/chirp.go b/chirp.go new file mode 100644 index 0000000..d4e61d2 --- /dev/null +++ b/chirp.go @@ -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, " ") +} diff --git a/main.go b/main.go index 10112ee..19989d0 100644 --- a/main.go +++ b/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", diff --git a/metrics.go b/metrics.go index 961d6d1..0ca32cf 100644 --- a/metrics.go +++ b/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 := ` + +
+Chirpy has been visited %d times!
+ + ` + fmt.Fprintf(w, template, hits) }