package main import ( "fmt" "net/http" "strconv" "github.com/julienschmidt/httprouter" ) func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) fmt.Fprintln(w, "Resource created") } func (app *application) getMovieHandler(w http.ResponseWriter, r *http.Request) { // When httprouter is parsing a request, any interpolated URL parameters will be // stored in the request context. We can use the ParamsFromContext() function to // retrieve a slice containing these parameter names and values. params := httprouter.ParamsFromContext(r.Context()) id, err := strconv.ParseInt(params.ByName("id"), 10, 64) if err != nil || id < 1 { http.NotFound(w, r) return } app.logger.Debug((fmt.Sprintf("Using id: %d", id))) w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "show the details of movie %d\n", id) } func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) { // JSON has to be double quoted. FYI you would never really do this // js := `{"status": "available", "environment": %q, "version": %q}` // js = fmt.Sprintf(js, app.config.env, Version) // w.Write([]byte(js)) data := map[string]string{ "status": "available", "environment": app.config.env, "version": Version, } // js, err := json.Marshal(data) // if err != nil { // app.logger.Error(err.Error()) // http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) // return // } // // Append a newline to the JSON. This is just a small nicety to make it easier to // // view in terminal applications. // js = append(js, '\n') // w.Header().Set("Content-Type", "application/json") // w.Write(js) err := app.writeJSON(w, 200, data, nil) if err != nil { app.logger.Error(err.Error()) http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) return } }