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.go50
1 files changed, 39 insertions, 11 deletions
diff --git a/cmd/server/main.go b/cmd/server/main.go
index b6817d8..bae215a 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -5,7 +5,10 @@ import (
 	"log"
 	"log/slog"
 	"os"
-	cfg "website/internal/config"
+	"os/signal"
+	"sync"
+
+	"website/internal/server"
 
 	"github.com/ardanlabs/conf/v3"
 	"github.com/pkg/errors"
@@ -16,20 +19,14 @@ var (
 	ShortSHA  string
 )
 
-type Config struct {
-	Production             bool    `conf:"default:false"`
-	ListenAddress          string  `conf:"default:localhost"`
-	Port                   uint16  `conf:"default:3000,short:p"`
-	BaseURL                cfg.URL `conf:"default:http://localhost:3000,short:b"`
-	RedirectOtherHostnames bool    `conf:"default:false"`
-}
-
 func main() {
 	if os.Getenv("DEBUG") != "" {
 		slog.SetLogLoggerLevel(slog.LevelDebug)
 	}
+	log.SetFlags(log.LstdFlags | log.Lmsgprefix)
+	log.SetPrefix("server: ")
 
-	runtimeConfig := Config{}
+	runtimeConfig := server.Config{}
 	help, err := conf.Parse("", &runtimeConfig)
 	if err != nil {
 		if errors.Is(err, conf.ErrHelpWanted) {
@@ -39,5 +36,36 @@ func main() {
 		log.Panicf("parsing runtime configuration: %v", err)
 	}
 
-	startServer(&runtimeConfig)
+	c := make(chan os.Signal, 2)
+	signal.Notify(c, os.Interrupt)
+	sv, err := server.New(&runtimeConfig)
+	if err != nil {
+		log.Fatalf("error setting up server: %v", err)
+	}
+	wg := &sync.WaitGroup{}
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		sig := <-c
+		log.Printf("signal captured: %v", sig)
+		<-sv.Stop()
+		slog.Debug("server stopped")
+	}()
+
+	sErr := make(chan error)
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		sErr <- sv.Start()
+	}()
+	if !runtimeConfig.InDevServer {
+		log.Printf("server listening on %s", sv.Addr)
+	}
+
+	err = <-sErr
+	if err != nil {
+		// Error starting or closing listener:
+		log.Fatalf("error: %v", err)
+	}
+	wg.Wait()
 }