chore(tfstated): refactor backend code to a dedicated package
This commit is contained in:
parent
1dbb1b9ee7
commit
36e3d473f2
9 changed files with 60 additions and 36 deletions
28
pkg/backend/get.go
Normal file
28
pkg/backend/get.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
|
||||
)
|
||||
|
||||
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 == "/" {
|
||||
helpers.ErrorResponse(w, http.StatusBadRequest,
|
||||
fmt.Errorf("no state path provided, cannot GET /"))
|
||||
return
|
||||
}
|
||||
|
||||
if data, err := db.GetState(r.URL.Path); err != nil {
|
||||
helpers.ErrorResponse(w, http.StatusInternalServerError, err)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(data)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue