summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--cmd/tfstated/main.go89
-rw-r--r--cmd/tfstated/main_test.go51
-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/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/unlock.go (renamed from cmd/tfstated/unlock.go)2
-rw-r--r--pkg/webui/healthz.go (renamed from cmd/tfstated/healthz.go)2
9 files changed, 59 insertions, 97 deletions
diff --git a/cmd/tfstated/main.go b/cmd/tfstated/main.go
index e6c6272..e228d53 100644
--- a/cmd/tfstated/main.go
+++ b/cmd/tfstated/main.go
@@ -3,85 +3,50 @@ package main
import (
"context"
"fmt"
- "io"
- "log"
"log/slog"
- "net"
- "net/http"
"os"
"os/signal"
- "strconv"
"sync"
+ "syscall"
"time"
+ "git.adyxax.org/adyxax/tfstated/pkg/backend"
"git.adyxax.org/adyxax/tfstated/pkg/database"
- "git.adyxax.org/adyxax/tfstated/pkg/logger"
+ "git.adyxax.org/adyxax/tfstated/pkg/webui"
)
-type Config struct {
- Host string
- Port string
-}
-
func run(
ctx context.Context,
- config *Config,
db *database.DB,
- //args []string,
getenv func(string) string,
- //stdin io.Reader,
- //stdout io.Writer,
- stderr io.Writer,
) error {
- ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
+ ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer cancel()
- dataEncryptionKey := getenv("DATA_ENCRYPTION_KEY")
- if dataEncryptionKey == "" {
- return fmt.Errorf("the DATA_ENCRYPTION_KEY environment variable is not set")
- }
- if err := db.SetDataEncryptionKey(dataEncryptionKey); err != nil {
- return err
- }
- versionsHistoryLimit := getenv("VERSIONS_HISTORY_LIMIT")
- if versionsHistoryLimit != "" {
- n, err := strconv.Atoi(versionsHistoryLimit)
- if err != nil {
- return fmt.Errorf("failed to parse the VERSIONS_HISTORY_LIMIT environment variable: %w", err)
- }
- db.SetVersionsHistoryLimit(n)
- }
-
if err := db.InitAdminAccount(); err != nil {
return err
}
- mux := http.NewServeMux()
- addRoutes(
- mux,
- db,
- )
+ backend := backend.Run(ctx, db, getenv)
+ webui := webui.Run(ctx, db, getenv)
- httpServer := &http.Server{
- Addr: net.JoinHostPort(config.Host, config.Port),
- Handler: logger.Middleware(mux, false),
- }
+ <-ctx.Done()
+ shutdownCtx := context.Background()
+ shutdownCtx, shutdownCancel := context.WithTimeout(shutdownCtx, 10*time.Second)
+ defer shutdownCancel()
+
+ var wg sync.WaitGroup
+ wg.Add(2)
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)
+ defer wg.Done()
+ if err := backend.Shutdown(shutdownCtx); err != nil {
+ slog.Error("error shutting down backend http server", "error", err)
}
}()
- var wg sync.WaitGroup
- wg.Add(1)
go func() {
defer wg.Done()
- <-ctx.Done()
- shutdownCtx := context.Background()
- shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
- defer cancel()
- if err := httpServer.Shutdown(shutdownCtx); err != nil {
- fmt.Fprintf(stderr, "error shutting down http server: %+v\n", err)
+ if err := webui.Shutdown(shutdownCtx); err != nil {
+ slog.Error("error shutting down webui http server", "error", err)
}
}()
wg.Wait()
@@ -93,7 +58,7 @@ func main() {
ctx := context.Background()
var opts *slog.HandlerOptions
- if os.Getenv("TFSTATE_DEBUG") != "" {
+ if os.Getenv("TFSTATED_DEBUG") != "" {
opts = &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
@@ -102,12 +67,11 @@ func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
slog.SetDefault(logger)
- config := Config{
- Host: "0.0.0.0",
- Port: "8080",
- }
-
- db, err := database.NewDB(ctx, "./tfstate.db?_txlock=immediate")
+ db, err := database.NewDB(
+ ctx,
+ "./tfstate.db?_txlock=immediate",
+ os.Getenv,
+ )
if err != nil {
fmt.Fprintf(os.Stderr, "database init error: %+v\n", err)
os.Exit(1)
@@ -116,13 +80,8 @@ func main() {
if err := run(
ctx,
- &config,
db,
- //os.Args,
os.Getenv,
- //os.Stdin,
- //os.Stdout,
- os.Stderr,
); err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
diff --git a/cmd/tfstated/main_test.go b/cmd/tfstated/main_test.go
index 82b7736..b28edbd 100644
--- a/cmd/tfstated/main_test.go
+++ b/cmd/tfstated/main_test.go
@@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"os"
+ "sync"
"testing"
"time"
@@ -14,31 +15,23 @@ import (
)
var baseURI = url.URL{
- Host: "127.0.0.1:8081",
+ Host: "127.0.0.1:8082",
Path: "/",
Scheme: "http",
}
var db *database.DB
var adminPassword string
+var adminPasswordMutex sync.Mutex
func TestMain(m *testing.M) {
- ctx := context.Background()
- ctx, cancel := context.WithCancel(ctx)
- config := Config{
- Host: "127.0.0.1",
- Port: "8081",
- }
- _ = os.Remove("./test.db")
- var err error
- db, err = database.NewDB(ctx, "./test.db")
- if err != nil {
- fmt.Fprintf(os.Stderr, "%+v\n", err)
- os.Exit(1)
- }
getenv := func(key string) string {
switch key {
case "DATA_ENCRYPTION_KEY":
return "hP3ZSCnY3LMgfTQjwTaGrhKwdA0yXMXIfv67OJnntqM="
+ case "TFSTATED_HOST":
+ return "127.0.0.1"
+ case "TFSTATED_PORT":
+ return "8082"
case "VERSIONS_HISTORY_LIMIT":
return "3"
default:
@@ -46,17 +39,26 @@ func TestMain(m *testing.M) {
}
}
+ ctx := context.Background()
+ ctx, cancel := context.WithCancel(ctx)
+ _ = os.Remove("./test.db")
+ var err error
+ db, err = database.NewDB(ctx, "./test.db", getenv)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%+v\n", err)
+ os.Exit(1)
+ }
database.AdvertiseAdminPassword = func(password string) {
+ adminPasswordMutex.Lock()
+ defer adminPasswordMutex.Unlock()
adminPassword = password
}
go run(
ctx,
- &config,
db,
getenv,
- os.Stderr,
)
- err = waitForReady(ctx, 5*time.Second, "http://127.0.0.1:8081/healthz")
+ err = waitForReady(ctx, 5*time.Second, "http://127.0.0.1:8082/healthz")
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
@@ -84,6 +86,8 @@ func runHTTPRequest(method string, auth bool, uriRef *url.URL, body io.Reader, t
return
}
if auth {
+ adminPasswordMutex.Lock()
+ defer adminPasswordMutex.Unlock()
req.SetBasicAuth("admin", adminPassword)
}
resp, err := client.Do(req)
@@ -119,14 +123,13 @@ func waitForReady(
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error making request: %s\n", err.Error())
- continue
- }
- if resp.StatusCode == http.StatusOK {
- fmt.Println("Endpoint is ready!")
- resp.Body.Close()
- return nil
+ } else {
+ _ = resp.Body.Close()
+ if resp.StatusCode == http.StatusOK {
+ fmt.Println("Endpoint is ready!")
+ return nil
+ }
}
- resp.Body.Close()
select {
case <-ctx.Done():
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/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/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"
diff --git a/cmd/tfstated/healthz.go b/pkg/webui/healthz.go
index 20c72c9..dee51d0 100644
--- a/cmd/tfstated/healthz.go
+++ b/pkg/webui/healthz.go
@@ -1,4 +1,4 @@
-package main
+package webui
import "net/http"