ch3.2
This commit is contained in:
@@ -1,12 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
|
func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, "status: available")
|
data := map[string]string{
|
||||||
fmt.Fprintf(w, "environment: %s\n", app.config.env)
|
"status": "available",
|
||||||
fmt.Fprintf(w, "version: %s\n", version)
|
"environment": app.config.env,
|
||||||
|
"version": version,
|
||||||
|
}
|
||||||
|
err := app.writeJSON(w, http.StatusOK, data, nil)
|
||||||
|
if err != nil {
|
||||||
|
app.logger.Print(err)
|
||||||
|
http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -17,3 +18,20 @@ func (app *application) readIDParam(r *http.Request) (int64, error) {
|
|||||||
}
|
}
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *application) writeJSON(w http.ResponseWriter, status int, data any, headers http.Header) error {
|
||||||
|
js, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
js = append(js, '\n')
|
||||||
|
for key, value := range headers {
|
||||||
|
w.Header()[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
w.Write(js)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
func (app *application) routes() *httprouter.Router {
|
func (app *application) routes() *httprouter.Router {
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
|
|
||||||
router.HandlerFunc(http.MethodGet, "/v1/healtcheck", app.healthcheckHandler)
|
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
|
||||||
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
|
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
|
||||||
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
|
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user