This commit is contained in:
lyx0 2022-08-16 18:05:55 +02:00
parent 3843c76906
commit 0099236bc8
4 changed files with 28 additions and 26 deletions

View file

@ -11,7 +11,7 @@ import (
func (app *application) showCommandHandler(w http.ResponseWriter, r *http.Request) {
name, err := app.readCommandNameParam(r)
if err != nil {
// app.Logger.Errorf("showCommandHandler, Command not found", err)
app.logError(r, err)
return
}
@ -21,18 +21,20 @@ func (app *application) showCommandHandler(w http.ResponseWriter, r *http.Reques
if err != nil {
switch {
case errors.Is(err, data.ErrRecordNotFound):
app.Logger.Errorf("showCommandHandler, Command not found", err)
app.logError(r, err)
return
default:
app.serverErrorResponse(w, r, err)
}
return
}
app.Logger.Info("Command Name:", command.Name)
err = app.writeJSON(w, http.StatusOK, envelope{"movie": command}, nil)
// if err != nil {
// app.serverErrorResponse(w, r, err)
// }
app.Logger.Infow("GET Command",
"Command", command,
)
err = app.writeJSON(w, http.StatusOK, envelope{"command": command}, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}
type envelope map[string]interface{}

View file

@ -16,8 +16,10 @@ func (app *application) serverErrorResponse(w http.ResponseWriter, r *http.Reque
// book we'll upgrade this to use structured logging, and record additional information
// about the request including the HTTP method and URL.
func (app *application) logError(r *http.Request, err error) {
app.Logger.Infow("logError",
"error", err)
app.Logger.Errorw("Error",
"Request URI", r.RequestURI,
"error", err,
)
}
func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message interface{}) {
@ -25,5 +27,5 @@ func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, st
// Write the response using the writeJSON() helper. If this happens to return an
// error then log it, and fall back to sending the client an empty response with a
// 500 Internal Server Error status code.
fmt.Fprintf(w, "Error: %s", status)
fmt.Fprintf(w, "Error: %d", status)
}

View file

@ -1,30 +1,27 @@
package main
import (
"errors"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
)
func (app *application) readIDParam(r *http.Request) (int64, error) {
params := httprouter.ParamsFromContext(r.Context())
// Use `ByName()` function to get the value of the "id" parameter from the slice.
// The value returned by `ByName()` is always a string so we try to convert it to
// base64 with a bit size of 64.
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
if err != nil || id < 1 {
return 0, errors.New("invalid id parameter")
}
return id, nil
}
// func (app *application) readIDParam(r *http.Request) (int64, error) {
// params := httprouter.ParamsFromContext(r.Context())
//
// // Use `ByName()` function to get the value of the "id" parameter from the slice.
// // The value returned by `ByName()` is always a string so we try to convert it to
// // base64 with a bit size of 64.
// id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
// if err != nil || id < 1 {
// return 0, errors.New("invalid id parameter")
// }
//
// return id, nil
// }
func (app *application) readCommandNameParam(r *http.Request) (string, error) {
params := httprouter.ParamsFromContext(r.Context())
app.Logger.Info(r.Context())
// Use `ByName()` function to get the value of the "id" parameter from the slice.
// The value returned by `ByName()` is always a string so we try to convert it to

View file

@ -8,6 +8,7 @@ CREATE TABLE IF NOT EXISTS commands (
INSERT INTO commands (name,"text","category","level") VALUES
('repeat','xset r rate 175 50','default',0),
('xset','xset r rate 175 50','default',0),
('eurkey','setxkbmap -layout eu','default',0),
('clueless','ch02 ch21 ch31','default',0),
('justinfan','64537','default',0),