aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-03-11 19:39:00 +0000
committerMatthew Lemon <y@yulqen.org>2024-03-11 19:39:00 +0000
commitbac244e9d1cc2f3af4a63c662f38be60a1bcf7fd (patch)
tree8be7381f92d9bb3eb9e3a4434ea71ba8000d45e2
parent02bf649c8981af4b46a18d9643386f7227f156a1 (diff)
Adds a writeJSON helper
-rw-r--r--cmd/api/healthcheck.go16
-rw-r--r--cmd/api/helpers.go36
-rw-r--r--cmd/api/main.go2
3 files changed, 50 insertions, 4 deletions
diff --git a/cmd/api/healthcheck.go b/cmd/api/healthcheck.go
index c4f52f2..c046995 100644
--- a/cmd/api/healthcheck.go
+++ b/cmd/api/healthcheck.go
@@ -1,13 +1,23 @@
package main
import (
- "fmt"
+ "encoding/json"
"net/http"
)
func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
- js := `{"status": "available", "environment": %q, "version": %q}`
- js = fmt.Sprintf(js, app.config.env, version)
+ data := map[string]string{
+ "status": "available",
+ "environment": app.config.env,
+ "version": version,
+ }
+ js, err := json.Marshal(data)
+ if err != nil {
+ app.logger.Debug("server error", "err", err)
+ http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
+ return
+ }
+ js = append(js, '\n')
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(js))
}
diff --git a/cmd/api/helpers.go b/cmd/api/helpers.go
new file mode 100644
index 0000000..77fbf0c
--- /dev/null
+++ b/cmd/api/helpers.go
@@ -0,0 +1,36 @@
+package main
+
+import (
+ "encoding/json"
+ "net/http"
+)
+
+// writeJSON() helper for sending responses. This takes the destination http.ResponseWriter, the
+// HTTP status code to sned, the data to encode to JSON and a header map containing any additional
+// HTTP headers we want to include in the response.
+func (app *application) writeJSON(w http.ResponseWriter, status int, data any, headers http.Header) error {
+ // Encode the data to JSON, returing the error if there was one.
+ js, err := json.Marshal(data)
+ if err != nil {
+ return err
+ }
+
+ // Append a newline to make it easier tro view in terminal applications
+ js = append(js, '\n')
+
+ // We know now that we won't encounter any more errors before writing the response,
+ // so it's safe to add any headers that we want to include. We loop through the
+ // header map and add each header to the http.ResponseWriter header map.
+ // Note that it's okay if the provided header map is nil. Go doesn't throw an error
+ // if you wanmt to try to range over (or more generally reader from) a nil map.
+ for key, value := range headers {
+ w.Header()[key] = value
+ }
+ // Add the "Content-Type: application/json" header, then write the status code and
+ // JSON response.
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(status)
+ w.Write(js)
+
+ return nil
+}
diff --git a/cmd/api/main.go b/cmd/api/main.go
index 9d3e9bd..960d6a7 100644
--- a/cmd/api/main.go
+++ b/cmd/api/main.go
@@ -55,7 +55,7 @@ func main() {
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
}
- logger.Info("startings server", "addr", srv.Addr, "env", cfg.env)
+ logger.Info("starting server", "addr", srv.Addr, "env", cfg.env)
err := srv.ListenAndServe()
logger.Error(err.Error())