summaryrefslogtreecommitdiff
path: root/cmd/tfstated/get.go
blob: 3310560220d4938c149f39448a50fa70d5c50cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

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)
		}
	})
}