You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.runcible.io/learning/pulley/internal/data"
|
|
"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.
|
|
|
|
// TODO refactor id retrieval to an app.readIDParam receiver
|
|
params := httprouter.ParamsFromContext(r.Context())
|
|
|
|
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
|
|
|
if err != nil || id < 1 {
|
|
// http.NotFound(w, r)
|
|
app.notFoundResponse(w, r)
|
|
return
|
|
}
|
|
|
|
movie := data.Movie{
|
|
ID: id,
|
|
CreatedAt: time.Now(),
|
|
Title: "Eisenhorn",
|
|
Runtime: 102,
|
|
Genres: []string{"sci-fi", "action"},
|
|
Version: 1,
|
|
}
|
|
app.logger.Info("Hit the get movies and found", "movie", movie.Title)
|
|
|
|
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, 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)
|
|
app.serverErrorResponse(w, r, err)
|
|
}
|
|
}
|
|
|
|
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))
|
|
env := envelope{
|
|
"status": "available",
|
|
"system_info": map[string]string{
|
|
"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, env, 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)
|
|
app.serverErrorResponse(w, r, err)
|
|
return
|
|
}
|
|
}
|