summaryrefslogtreecommitdiff
path: root/pkg/backend/post.go
blob: 8271022c14df6ce884bb98c1e6fd7ee3d48fb78c (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
29
30
31
32
33
34
35
36
37
38
39
40
package backend

import (
	"fmt"
	"io"
	"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 == "/" {
			helpers.ErrorResponse(w, http.StatusBadRequest,
				fmt.Errorf("no state path provided, cannot POST /"),
			)
			return
		}

		id := r.URL.Query().Get("ID")

		data, err := io.ReadAll(r.Body)
		if err != nil || len(data) == 0 {
			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 {
				helpers.ErrorResponse(w, http.StatusConflict, err)
			} else {
				helpers.ErrorResponse(w, http.StatusInternalServerError, err)
			}
		} else {
			w.WriteHeader(http.StatusOK)
		}
	})
}