From 36e3d473f2126e920061218996cfa5cfecade7d6 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 30 Dec 2024 23:07:00 +0100 Subject: chore(tfstated): refactor backend code to a dedicated package --- cmd/tfstated/delete.go | 28 ----------------------- cmd/tfstated/get.go | 28 ----------------------- cmd/tfstated/healthz.go | 12 ---------- cmd/tfstated/lock.go | 61 ------------------------------------------------- cmd/tfstated/main.go | 31 ++----------------------- cmd/tfstated/post.go | 40 -------------------------------- cmd/tfstated/routes.go | 22 ------------------ cmd/tfstated/unlock.go | 32 -------------------------- pkg/backend/delete.go | 28 +++++++++++++++++++++++ pkg/backend/get.go | 28 +++++++++++++++++++++++ pkg/backend/healthz.go | 12 ++++++++++ pkg/backend/lock.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ pkg/backend/post.go | 40 ++++++++++++++++++++++++++++++++ pkg/backend/routes.go | 22 ++++++++++++++++++ pkg/backend/run.go | 51 +++++++++++++++++++++++++++++++++++++++++ pkg/backend/unlock.go | 32 ++++++++++++++++++++++++++ 16 files changed, 276 insertions(+), 252 deletions(-) delete mode 100644 cmd/tfstated/delete.go delete mode 100644 cmd/tfstated/get.go delete mode 100644 cmd/tfstated/healthz.go delete mode 100644 cmd/tfstated/lock.go delete mode 100644 cmd/tfstated/post.go delete mode 100644 cmd/tfstated/routes.go delete mode 100644 cmd/tfstated/unlock.go create mode 100644 pkg/backend/delete.go create mode 100644 pkg/backend/get.go create mode 100644 pkg/backend/healthz.go create mode 100644 pkg/backend/lock.go create mode 100644 pkg/backend/post.go create mode 100644 pkg/backend/routes.go create mode 100644 pkg/backend/run.go create mode 100644 pkg/backend/unlock.go diff --git a/cmd/tfstated/delete.go b/cmd/tfstated/delete.go deleted file mode 100644 index d594073..0000000 --- a/cmd/tfstated/delete.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - - "git.adyxax.org/adyxax/tfstated/pkg/database" - "git.adyxax.org/adyxax/tfstated/pkg/helpers" -) - -func handleDelete(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 DELETE /")) - return - } - - if success, err := db.DeleteState(r.URL.Path); err != nil { - helpers.ErrorResponse(w, http.StatusInternalServerError, err) - } else if success { - w.WriteHeader(http.StatusOK) - } else { - helpers.ErrorResponse(w, http.StatusNotFound, - fmt.Errorf("state path not found: %s", r.URL.Path)) - } - }) -} diff --git a/cmd/tfstated/get.go b/cmd/tfstated/get.go deleted file mode 100644 index 3310560..0000000 --- a/cmd/tfstated/get.go +++ /dev/null @@ -1,28 +0,0 @@ -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) - } - }) -} diff --git a/cmd/tfstated/healthz.go b/cmd/tfstated/healthz.go deleted file mode 100644 index 20c72c9..0000000 --- a/cmd/tfstated/healthz.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import "net/http" - -func handleHealthz() http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Cache-Control", "no-store, no-cache") - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - _, _ = w.Write([]byte("{}")) - }) -} diff --git a/cmd/tfstated/lock.go b/cmd/tfstated/lock.go deleted file mode 100644 index 80e3575..0000000 --- a/cmd/tfstated/lock.go +++ /dev/null @@ -1,61 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - "regexp" - "time" - - "git.adyxax.org/adyxax/tfstated/pkg/database" - "git.adyxax.org/adyxax/tfstated/pkg/helpers" -) - -type lockRequest struct { - Created time.Time `json:"Created"` - ID string `json:"ID"` - Info string `json:"Info"` - Operation string `json:"Operation"` - Path string `json:"Path"` - Version string `json:"Version"` - Who string `json:"Who"` -} - -var ( - validID = regexp.MustCompile("[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}") -) - -func (l *lockRequest) valid() []error { - err := make([]error, 0) - if !validID.MatchString(l.ID) { - err = append(err, fmt.Errorf("invalid ID")) - } - return err -} - -func handleLock(db *database.DB) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/" { - _ = helpers.Encode(w, http.StatusBadRequest, - fmt.Errorf("no state path provided, cannot LOCK /")) - return - } - - var lock lockRequest - if err := helpers.Decode(r, &lock); err != nil { - _ = helpers.Encode(w, http.StatusBadRequest, err) - return - } - if errs := lock.valid(); len(errs) > 0 { - _ = helpers.Encode(w, http.StatusBadRequest, - fmt.Errorf("invalid lock: %+v", errs)) - return - } - if success, err := db.SetLockOrGetExistingLock(r.URL.Path, &lock); err != nil { - helpers.ErrorResponse(w, http.StatusInternalServerError, err) - } else if success { - w.WriteHeader(http.StatusOK) - } else { - _ = helpers.Encode(w, http.StatusConflict, lock) - } - }) -} diff --git a/cmd/tfstated/main.go b/cmd/tfstated/main.go index 79b64f7..e6e9a38 100644 --- a/cmd/tfstated/main.go +++ b/cmd/tfstated/main.go @@ -4,17 +4,14 @@ import ( "context" "fmt" "io" - "log" "log/slog" - "net" - "net/http" "os" "os/signal" "sync" "time" + "git.adyxax.org/adyxax/tfstated/pkg/backend" "git.adyxax.org/adyxax/tfstated/pkg/database" - "git.adyxax.org/adyxax/tfstated/pkg/logger" ) func run( @@ -33,31 +30,7 @@ func run( return err } - mux := http.NewServeMux() - addRoutes( - mux, - db, - ) - - host := getenv("TFSTATED_HOST") - if host == "" { - host = "127.0.0.1" - } - port := getenv("TFSTATED_PORT") - if port == "" { - port = "8080" - } - - httpServer := &http.Server{ - Addr: net.JoinHostPort(host, port), - Handler: logger.Middleware(mux, false), - } - go func() { - log.Printf("listening on %s\n", httpServer.Addr) - if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { - _, _ = fmt.Fprintf(stderr, "error listening and serving: %+v\n", err) - } - }() + httpServer := backend.Run(ctx, db, getenv, stderr) var wg sync.WaitGroup wg.Add(1) go func() { diff --git a/cmd/tfstated/post.go b/cmd/tfstated/post.go deleted file mode 100644 index 86344b1..0000000 --- a/cmd/tfstated/post.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -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) - } - }) -} diff --git a/cmd/tfstated/routes.go b/cmd/tfstated/routes.go deleted file mode 100644 index 019bb76..0000000 --- a/cmd/tfstated/routes.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "net/http" - - "git.adyxax.org/adyxax/tfstated/pkg/basic_auth" - "git.adyxax.org/adyxax/tfstated/pkg/database" -) - -func addRoutes( - mux *http.ServeMux, - db *database.DB, -) { - mux.Handle("GET /healthz", handleHealthz()) - - basicAuth := basic_auth.Middleware(db) - mux.Handle("DELETE /", basicAuth(handleDelete(db))) - mux.Handle("GET /", basicAuth(handleGet(db))) - mux.Handle("LOCK /", basicAuth(handleLock(db))) - mux.Handle("POST /", basicAuth(handlePost(db))) - mux.Handle("UNLOCK /", basicAuth(handleUnlock(db))) -} diff --git a/cmd/tfstated/unlock.go b/cmd/tfstated/unlock.go deleted file mode 100644 index c003d8d..0000000 --- a/cmd/tfstated/unlock.go +++ /dev/null @@ -1,32 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - - "git.adyxax.org/adyxax/tfstated/pkg/database" - "git.adyxax.org/adyxax/tfstated/pkg/helpers" -) - -func handleUnlock(db *database.DB) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/" { - _ = helpers.Encode(w, http.StatusBadRequest, - fmt.Errorf("no state path provided, cannot LOCK /")) - return - } - - var lock lockRequest - if err := helpers.Decode(r, &lock); err != nil { - _ = helpers.Encode(w, http.StatusBadRequest, err) - return - } - if success, err := db.Unlock(r.URL.Path, &lock); err != nil { - helpers.ErrorResponse(w, http.StatusInternalServerError, err) - } else if success { - w.WriteHeader(http.StatusOK) - } else { - _ = helpers.Encode(w, http.StatusConflict, lock) - } - }) -} diff --git a/pkg/backend/delete.go b/pkg/backend/delete.go new file mode 100644 index 0000000..61007c4 --- /dev/null +++ b/pkg/backend/delete.go @@ -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 handleDelete(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 DELETE /")) + return + } + + if success, err := db.DeleteState(r.URL.Path); err != nil { + helpers.ErrorResponse(w, http.StatusInternalServerError, err) + } else if success { + w.WriteHeader(http.StatusOK) + } else { + helpers.ErrorResponse(w, http.StatusNotFound, + fmt.Errorf("state path not found: %s", r.URL.Path)) + } + }) +} diff --git a/pkg/backend/get.go b/pkg/backend/get.go new file mode 100644 index 0000000..ca9b2c0 --- /dev/null +++ b/pkg/backend/get.go @@ -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) + } + }) +} diff --git a/pkg/backend/healthz.go b/pkg/backend/healthz.go new file mode 100644 index 0000000..70ece68 --- /dev/null +++ b/pkg/backend/healthz.go @@ -0,0 +1,12 @@ +package backend + +import "net/http" + +func handleHealthz() http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-store, no-cache") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("{}")) + }) +} diff --git a/pkg/backend/lock.go b/pkg/backend/lock.go new file mode 100644 index 0000000..ef62198 --- /dev/null +++ b/pkg/backend/lock.go @@ -0,0 +1,61 @@ +package backend + +import ( + "fmt" + "net/http" + "regexp" + "time" + + "git.adyxax.org/adyxax/tfstated/pkg/database" + "git.adyxax.org/adyxax/tfstated/pkg/helpers" +) + +type lockRequest struct { + Created time.Time `json:"Created"` + ID string `json:"ID"` + Info string `json:"Info"` + Operation string `json:"Operation"` + Path string `json:"Path"` + Version string `json:"Version"` + Who string `json:"Who"` +} + +var ( + validID = regexp.MustCompile("[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}") +) + +func (l *lockRequest) valid() []error { + err := make([]error, 0) + if !validID.MatchString(l.ID) { + err = append(err, fmt.Errorf("invalid ID")) + } + return err +} + +func handleLock(db *database.DB) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + _ = helpers.Encode(w, http.StatusBadRequest, + fmt.Errorf("no state path provided, cannot LOCK /")) + return + } + + var lock lockRequest + if err := helpers.Decode(r, &lock); err != nil { + _ = helpers.Encode(w, http.StatusBadRequest, err) + return + } + if errs := lock.valid(); len(errs) > 0 { + _ = helpers.Encode(w, http.StatusBadRequest, + fmt.Errorf("invalid lock: %+v", errs)) + return + } + if success, err := db.SetLockOrGetExistingLock(r.URL.Path, &lock); err != nil { + helpers.ErrorResponse(w, http.StatusInternalServerError, err) + } else if success { + w.WriteHeader(http.StatusOK) + } else { + _ = helpers.Encode(w, http.StatusConflict, lock) + } + }) +} diff --git a/pkg/backend/post.go b/pkg/backend/post.go new file mode 100644 index 0000000..8271022 --- /dev/null +++ b/pkg/backend/post.go @@ -0,0 +1,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) + } + }) +} diff --git a/pkg/backend/routes.go b/pkg/backend/routes.go new file mode 100644 index 0000000..058febd --- /dev/null +++ b/pkg/backend/routes.go @@ -0,0 +1,22 @@ +package backend + +import ( + "net/http" + + "git.adyxax.org/adyxax/tfstated/pkg/basic_auth" + "git.adyxax.org/adyxax/tfstated/pkg/database" +) + +func addRoutes( + mux *http.ServeMux, + db *database.DB, +) { + mux.Handle("GET /healthz", handleHealthz()) + + basicAuth := basic_auth.Middleware(db) + mux.Handle("DELETE /", basicAuth(handleDelete(db))) + mux.Handle("GET /", basicAuth(handleGet(db))) + mux.Handle("LOCK /", basicAuth(handleLock(db))) + mux.Handle("POST /", basicAuth(handlePost(db))) + mux.Handle("UNLOCK /", basicAuth(handleUnlock(db))) +} diff --git a/pkg/backend/run.go b/pkg/backend/run.go new file mode 100644 index 0000000..dd7f3bf --- /dev/null +++ b/pkg/backend/run.go @@ -0,0 +1,51 @@ +package backend + +import ( + "context" + "fmt" + "io" + "log" + "net" + "net/http" + + "git.adyxax.org/adyxax/tfstated/pkg/database" + "git.adyxax.org/adyxax/tfstated/pkg/logger" +) + +func Run( + ctx context.Context, + db *database.DB, + //args []string, + getenv func(string) string, + //stdin io.Reader, + //stdout io.Writer, + stderr io.Writer, +) *http.Server { + mux := http.NewServeMux() + addRoutes( + mux, + db, + ) + + host := getenv("TFSTATED_HOST") + if host == "" { + host = "127.0.0.1" + } + port := getenv("TFSTATED_PORT") + if port == "" { + port = "8080" + } + + httpServer := &http.Server{ + Addr: net.JoinHostPort(host, port), + Handler: logger.Middleware(mux, false), + } + go func() { + log.Printf("listening on %s\n", httpServer.Addr) + if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { + _, _ = fmt.Fprintf(stderr, "error listening and serving: %+v\n", err) + } + }() + + return httpServer +} diff --git a/pkg/backend/unlock.go b/pkg/backend/unlock.go new file mode 100644 index 0000000..bc601f0 --- /dev/null +++ b/pkg/backend/unlock.go @@ -0,0 +1,32 @@ +package backend + +import ( + "fmt" + "net/http" + + "git.adyxax.org/adyxax/tfstated/pkg/database" + "git.adyxax.org/adyxax/tfstated/pkg/helpers" +) + +func handleUnlock(db *database.DB) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + _ = helpers.Encode(w, http.StatusBadRequest, + fmt.Errorf("no state path provided, cannot LOCK /")) + return + } + + var lock lockRequest + if err := helpers.Decode(r, &lock); err != nil { + _ = helpers.Encode(w, http.StatusBadRequest, err) + return + } + if success, err := db.Unlock(r.URL.Path, &lock); err != nil { + helpers.ErrorResponse(w, http.StatusInternalServerError, err) + } else if success { + w.WriteHeader(http.StatusOK) + } else { + _ = helpers.Encode(w, http.StatusConflict, lock) + } + }) +} -- cgit v1.2.3