-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
135 lines (117 loc) · 3.6 KB
/
main.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"errors"
"io/fs"
"io/ioutil"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/sirupsen/logrus"
)
var storageBackend Backend
var config Config
func getTfstate(w http.ResponseWriter, r *http.Request) {
var body []byte
var err error
var e *fs.PathError
tfID := chi.URLParam(r, "id")
if body, err = storageBackend.get(tfID); err != nil {
if errors.As(err, &e) {
var operation = err.(*fs.PathError).Op
if operation == "stat" || operation == "CreateFile" {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(http.StatusText(http.StatusNotFound)))
} else {
logger.Warnf("Can not Access File: %v", err)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
}
return
}
logger.Warnf("Complete unexpected Error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
}
func updateTfstate(w http.ResponseWriter, r *http.Request) {
tfID := chi.URLParam(r, "id")
reqBody, _ := ioutil.ReadAll(r.Body)
if err := storageBackend.update(tfID, reqBody); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(reqBody)
}
func purgeTfstate(w http.ResponseWriter, r *http.Request) {
tfID := chi.URLParam(r, "id")
if err := storageBackend.purge(tfID); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
_, _ = w.Write([]byte("{\"state\": \"tfstate deleted\"}"))
}
func lockTfstate(w http.ResponseWriter, r *http.Request) {
var lockFile []byte
var err error
var conflict *ConflictError
tfID := chi.URLParam(r, "id")
reqBody, _ := ioutil.ReadAll(r.Body)
if lockFile, err = storageBackend.lock(tfID, reqBody); err != nil {
if errors.As(err, &conflict) {
w.WriteHeader(http.StatusConflict)
_, _ = w.Write([]byte(http.StatusText(http.StatusConflict)))
return
}
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
_, _ = w.Write(lockFile)
}
func unlockTfstate(w http.ResponseWriter, r *http.Request) {
var conflict *ConflictError
tfID := chi.URLParam(r, "id")
reqBody, _ := ioutil.ReadAll(r.Body)
if err := storageBackend.unlock(tfID, reqBody); err != nil {
if errors.As(err, &conflict) {
w.WriteHeader(http.StatusConflict)
_, _ = w.Write([]byte(http.StatusText(http.StatusConflict)))
return
}
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
_, _ = w.Write(reqBody)
}
func handleRequests() {
logger.Debugf("current storage path: %s", config.storageDirectory)
storageBackend = Backend{dir: config.storageDirectory}
r := chi.NewRouter()
r.Use(middleware.Logger)
if config.authEnabled {
r.Use(middleware.BasicAuth("restricted access", config.getAuthMap()))
}
chi.RegisterMethod("LOCK")
chi.RegisterMethod("UNLOCK")
r.Use(middleware.SetHeader("Content-Type", "application/json"))
r.Get("/{id}", getTfstate)
r.Post("/{id}", updateTfstate)
r.Delete("/{id}", purgeTfstate)
r.MethodFunc("LOCK", "/{id}", lockTfstate)
r.MethodFunc("UNLOCK", "/{id}", unlockTfstate)
logger.Fatal(http.ListenAndServe(config.getAddr(), r))
}
func init() {
SetLogger(logrus.New())
}
func main() {
config.loadConfig(".env")
handleRequests()
}