feat(tfstated): add json encoding and decoding helpers

This commit is contained in:
Julien Dessaux 2024-10-01 17:32:14 +02:00
parent 2cdbc4e782
commit d4a82f610c
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 46 additions and 13 deletions

View file

@ -12,21 +12,20 @@ import (
func handleGet(db *database.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store, no-cache")
w.Header().Set("Content-Type", "application/json")
if r.URL.Path == "/" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("{\"msg\": \"No state path provided, cannot GET /\"}"))
_ = errorResponse(w, http.StatusBadRequest,
fmt.Errorf("no state path provided, cannot GET /"))
return
}
if data, err := db.GetState(r.URL.Path); err != nil {
if errors.Is(err, sql.ErrNoRows) {
w.WriteHeader(http.StatusNotFound)
_ = errorResponse(w, http.StatusNotFound,
fmt.Errorf("state path not found: %s", r.URL.Path))
} else {
w.WriteHeader(http.StatusInternalServerError)
_ = errorResponse(w, http.StatusInternalServerError, err)
}
_, _ = w.Write([]byte(fmt.Sprintf("{\"msg\": \"%+v\"}", err)))
} else {
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)

36
cmd/tfstated/helpers.go Normal file
View file

@ -0,0 +1,36 @@
package main
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
)
func decode(r *http.Request, data any) error {
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
return fmt.Errorf("failed to decode json: %w", err)
}
return nil
}
func encode(w http.ResponseWriter, status int, data any) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
slog.Error("failed to encode json", "err", err)
return fmt.Errorf("failed to encode json: %w", err)
}
return nil
}
func errorResponse(w http.ResponseWriter, status int, err error) error {
type errorResponse struct {
Msg string `json:"msg"`
Status int `json:"status"`
}
return encode(w, status, &errorResponse{
Msg: fmt.Sprintf("%+v", err),
Status: status,
})
}

View file

@ -10,21 +10,19 @@ import (
func handlePost(db *database.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.URL.Path == "/" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("{\"msg\": \"No state path provided, cannot POST /\"}"))
_ = errorResponse(w, http.StatusBadRequest,
fmt.Errorf("no state path provided, cannot POST /"),
)
return
}
data, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(fmt.Sprintf("{\"msg\": \"failed to read request body: %+v\"}", err)))
_ = errorResponse(w, http.StatusBadRequest, err)
return
}
if err := db.SetState(r.URL.Path, data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("{\"msg\": \"%+v\"}", err)))
_ = errorResponse(w, http.StatusInternalServerError, err)
} else {
w.WriteHeader(http.StatusOK)
}