summaryrefslogtreecommitdiff
path: root/pkg/backend/run.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pkg/backend/run.go51
1 files changed, 51 insertions, 0 deletions
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
+}