about summary refs log tree commit diff stats
path: root/cmd/server/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/server/main.go')
-rw-r--r--cmd/server/main.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/cmd/server/main.go b/cmd/server/main.go
new file mode 100644
index 0000000..be36e3b
--- /dev/null
+++ b/cmd/server/main.go
@@ -0,0 +1,63 @@
+package main
+
+import (
+	"context"
+	"fmt"
+	"os"
+	"os/signal"
+
+	"go.alanpearce.eu/website/internal/server"
+	"go.alanpearce.eu/x/log"
+
+	"github.com/ardanlabs/conf/v3"
+	"gitlab.com/tozd/go/errors"
+)
+
+func main() {
+	runtimeConfig := server.Config{}
+	help, err := conf.Parse("", &runtimeConfig)
+	if err != nil {
+		if errors.Is(err, conf.ErrHelpWanted) {
+			fmt.Println(help)
+			os.Exit(1)
+		}
+		panic("parsing runtime configuration" + err.Error())
+	}
+	log := log.Configure(!runtimeConfig.Development)
+
+	if listenAddress := runtimeConfig.ListenAddress; listenAddress[0] == '[' {
+		runtimeConfig.ListenAddress = listenAddress[1 : len(listenAddress)-1]
+	}
+
+	if runtimeConfig.Development {
+		tmpdir, err := os.MkdirTemp("", "website")
+		if err != nil {
+			log.Fatal("could not create temporary directory", "error", err)
+		}
+		defer os.RemoveAll(tmpdir)
+		runtimeConfig.Root = tmpdir
+	}
+
+	sv, err := server.New(&runtimeConfig, log)
+	if err != nil {
+		log.Error("could not create server", "error", err)
+
+		return
+	}
+
+	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
+	defer cancel()
+
+	go func() {
+		err = sv.Start()
+		if err != nil {
+			// Error starting or closing listener:
+			log.Fatal("error %v", err)
+		}
+	}()
+
+	<-ctx.Done()
+	log.Debug("calling stop")
+	sv.Stop()
+	log.Debug("done")
+}