summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/tfstated/main.go31
-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 (renamed from cmd/tfstated/healthz.go)2
-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)2
-rw-r--r--pkg/backend/run.go51
-rw-r--r--pkg/backend/unlock.go (renamed from cmd/tfstated/unlock.go)2
9 files changed, 60 insertions, 36 deletions
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/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/cmd/tfstated/healthz.go b/pkg/backend/healthz.go
index 20c72c9..70ece68 100644
--- a/cmd/tfstated/healthz.go
+++ b/pkg/backend/healthz.go
@@ -1,4 +1,4 @@
-package main
+package backend
import "net/http"
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..058febd 100644
--- a/cmd/tfstated/routes.go
+++ b/pkg/backend/routes.go
@@ -1,4 +1,4 @@
-package main
+package backend
import (
"net/http"
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/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"