diff options
Diffstat (limited to '')
-rw-r--r-- | pkg/backend/delete.go (renamed from cmd/tfstated/delete.go) | 2 | ||||
-rw-r--r-- | pkg/backend/get.go (renamed from cmd/tfstated/get.go) | 2 | ||||
-rw-r--r-- | pkg/backend/healthz.go | 12 | ||||
-rw-r--r-- | pkg/backend/lock.go (renamed from cmd/tfstated/lock.go) | 2 | ||||
-rw-r--r-- | pkg/backend/post.go (renamed from cmd/tfstated/post.go) | 2 | ||||
-rw-r--r-- | pkg/backend/routes.go (renamed from cmd/tfstated/routes.go) | 4 | ||||
-rw-r--r-- | pkg/backend/run.go | 45 | ||||
-rw-r--r-- | pkg/backend/unlock.go (renamed from cmd/tfstated/unlock.go) | 2 |
8 files changed, 64 insertions, 7 deletions
diff --git a/cmd/tfstated/delete.go b/pkg/backend/delete.go index d594073..61007c4 100644 --- a/cmd/tfstated/delete.go +++ b/pkg/backend/delete.go @@ -1,4 +1,4 @@ -package main +package backend import ( "fmt" diff --git a/cmd/tfstated/get.go b/pkg/backend/get.go index 3310560..ca9b2c0 100644 --- a/cmd/tfstated/get.go +++ b/pkg/backend/get.go @@ -1,4 +1,4 @@ -package main +package backend import ( "fmt" 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/cmd/tfstated/lock.go b/pkg/backend/lock.go index 80e3575..ef62198 100644 --- a/cmd/tfstated/lock.go +++ b/pkg/backend/lock.go @@ -1,4 +1,4 @@ -package main +package backend import ( "fmt" diff --git a/cmd/tfstated/post.go b/pkg/backend/post.go index 86344b1..8271022 100644 --- a/cmd/tfstated/post.go +++ b/pkg/backend/post.go @@ -1,4 +1,4 @@ -package main +package backend import ( "fmt" diff --git a/cmd/tfstated/routes.go b/pkg/backend/routes.go index 019bb76..960a2e8 100644 --- a/cmd/tfstated/routes.go +++ b/pkg/backend/routes.go @@ -1,10 +1,10 @@ -package main +package backend import ( "net/http" - "git.adyxax.org/adyxax/tfstated/pkg/basic_auth" "git.adyxax.org/adyxax/tfstated/pkg/database" + "git.adyxax.org/adyxax/tfstated/pkg/middlewares/basic_auth" ) func addRoutes( diff --git a/pkg/backend/run.go b/pkg/backend/run.go new file mode 100644 index 0000000..d76d189 --- /dev/null +++ b/pkg/backend/run.go @@ -0,0 +1,45 @@ +package backend + +import ( + "context" + "log/slog" + "net" + "net/http" + + "git.adyxax.org/adyxax/tfstated/pkg/database" + "git.adyxax.org/adyxax/tfstated/pkg/middlewares/logger" +) + +func Run( + ctx context.Context, + db *database.DB, + getenv func(string) string, +) *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() { + slog.Info("backend http server listening", "address", httpServer.Addr) + if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { + slog.Error("error listening and serving backend http server", "address", httpServer.Addr, "error", err) + } + }() + + return httpServer +} diff --git a/cmd/tfstated/unlock.go b/pkg/backend/unlock.go index c003d8d..bc601f0 100644 --- a/cmd/tfstated/unlock.go +++ b/pkg/backend/unlock.go @@ -1,4 +1,4 @@ -package main +package backend import ( "fmt" |