2024-12-30 23:07:00 +01:00
|
|
|
package backend
|
2024-09-30 00:58:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
2024-11-17 00:05:22 +01:00
|
|
|
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
|
2024-09-30 00:58:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
if r.URL.Path == "/" {
|
2024-11-17 00:05:22 +01:00
|
|
|
helpers.ErrorResponse(w, http.StatusBadRequest,
|
2024-10-01 17:32:14 +02:00
|
|
|
fmt.Errorf("no state path provided, cannot GET /"))
|
2024-09-30 00:58:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if data, err := db.GetState(r.URL.Path); err != nil {
|
2024-11-17 00:05:22 +01:00
|
|
|
helpers.ErrorResponse(w, http.StatusInternalServerError, err)
|
2024-09-30 00:58:49 +02:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|