chore(tfstated): refactor helpers to their own package
This commit is contained in:
parent
5b6da56089
commit
25ed1188ed
11 changed files with 78 additions and 62 deletions
|
@ -5,22 +5,23 @@ import (
|
|||
"net/http"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
|
||||
)
|
||||
|
||||
func handleDelete(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
_ = errorResponse(w, http.StatusBadRequest,
|
||||
helpers.ErrorResponse(w, http.StatusBadRequest,
|
||||
fmt.Errorf("no state path provided, cannot DELETE /"))
|
||||
return
|
||||
}
|
||||
|
||||
if success, err := db.DeleteState(r.URL.Path); err != nil {
|
||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||
helpers.ErrorResponse(w, http.StatusInternalServerError, err)
|
||||
} else if success {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
_ = errorResponse(w, http.StatusNotFound,
|
||||
helpers.ErrorResponse(w, http.StatusNotFound,
|
||||
fmt.Errorf("state path not found: %s", r.URL.Path))
|
||||
}
|
||||
})
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
|
||||
)
|
||||
|
||||
func handleGet(db *database.DB) http.Handler {
|
||||
|
@ -12,13 +13,13 @@ func handleGet(db *database.DB) http.Handler {
|
|||
w.Header().Set("Cache-Control", "no-store, no-cache")
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
_ = errorResponse(w, http.StatusBadRequest,
|
||||
helpers.ErrorResponse(w, http.StatusBadRequest,
|
||||
fmt.Errorf("no state path provided, cannot GET /"))
|
||||
return
|
||||
}
|
||||
|
||||
if data, err := db.GetState(r.URL.Path); err != nil {
|
||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||
helpers.ErrorResponse(w, http.StatusInternalServerError, err)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(data)
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
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,
|
||||
})
|
||||
}
|
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
|
||||
)
|
||||
|
||||
type lockRequest struct {
|
||||
|
@ -34,27 +35,27 @@ func (l *lockRequest) valid() []error {
|
|||
func handleLock(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
_ = encode(w, http.StatusBadRequest,
|
||||
_ = helpers.Encode(w, http.StatusBadRequest,
|
||||
fmt.Errorf("no state path provided, cannot LOCK /"))
|
||||
return
|
||||
}
|
||||
|
||||
var lock lockRequest
|
||||
if err := decode(r, &lock); err != nil {
|
||||
_ = encode(w, http.StatusBadRequest, err)
|
||||
if err := helpers.Decode(r, &lock); err != nil {
|
||||
_ = helpers.Encode(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if errs := lock.valid(); len(errs) > 0 {
|
||||
_ = encode(w, http.StatusBadRequest,
|
||||
_ = helpers.Encode(w, http.StatusBadRequest,
|
||||
fmt.Errorf("invalid lock: %+v", errs))
|
||||
return
|
||||
}
|
||||
if success, err := db.SetLockOrGetExistingLock(r.URL.Path, &lock); err != nil {
|
||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||
helpers.ErrorResponse(w, http.StatusInternalServerError, err)
|
||||
} else if success {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
_ = encode(w, http.StatusConflict, lock)
|
||||
_ = helpers.Encode(w, http.StatusConflict, lock)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -6,13 +6,14 @@ import (
|
|||
"net/http"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||
)
|
||||
|
||||
func handlePost(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
_ = errorResponse(w, http.StatusBadRequest,
|
||||
helpers.ErrorResponse(w, http.StatusBadRequest,
|
||||
fmt.Errorf("no state path provided, cannot POST /"),
|
||||
)
|
||||
return
|
||||
|
@ -22,15 +23,15 @@ func handlePost(db *database.DB) http.Handler {
|
|||
|
||||
data, err := io.ReadAll(r.Body)
|
||||
if err != nil || len(data) == 0 {
|
||||
_ = errorResponse(w, http.StatusBadRequest, err)
|
||||
helpers.ErrorResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
account := r.Context().Value(model.AccountContextKey{}).(*model.Account)
|
||||
if idMismatch, err := db.SetState(r.URL.Path, account.Id, data, id); err != nil {
|
||||
if idMismatch {
|
||||
_ = errorResponse(w, http.StatusConflict, err)
|
||||
helpers.ErrorResponse(w, http.StatusConflict, err)
|
||||
} else {
|
||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||
helpers.ErrorResponse(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
|
|
@ -5,27 +5,28 @@ import (
|
|||
"net/http"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
|
||||
)
|
||||
|
||||
func handleUnlock(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
_ = encode(w, http.StatusBadRequest,
|
||||
_ = helpers.Encode(w, http.StatusBadRequest,
|
||||
fmt.Errorf("no state path provided, cannot LOCK /"))
|
||||
return
|
||||
}
|
||||
|
||||
var lock lockRequest
|
||||
if err := decode(r, &lock); err != nil {
|
||||
_ = encode(w, http.StatusBadRequest, err)
|
||||
if err := helpers.Decode(r, &lock); err != nil {
|
||||
_ = helpers.Encode(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if success, err := db.Unlock(r.URL.Path, &lock); err != nil {
|
||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||
helpers.ErrorResponse(w, http.StatusInternalServerError, err)
|
||||
} else if success {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
_ = encode(w, http.StatusConflict, lock)
|
||||
_ = helpers.Encode(w, http.StatusConflict, lock)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue