about summary refs log tree commit diff stats
path: root/serve/main.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-02 23:18:19 +0200
committerAlan Pearce2024-05-02 23:20:30 +0200
commit73603079e29bc89c54296a9e12b5a779cd84c023 (patch)
tree3e5d0c0c87b81a007667fc4b533cb9403675a422 /serve/main.go
parent7ad48953a4d9470d2f4fe89343c0b09bff410c58 (diff)
downloadsearchix-73603079e29bc89c54296a9e12b5a779cd84c023.tar.lz
searchix-73603079e29bc89c54296a9e12b5a779cd84c023.tar.zst
searchix-73603079e29bc89c54296a9e12b5a779cd84c023.zip
feat: serve a very basic html template
Diffstat (limited to 'serve/main.go')
-rw-r--r--serve/main.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/serve/main.go b/serve/main.go
index 7905807..6b55977 100644
--- a/serve/main.go
+++ b/serve/main.go
@@ -1,5 +1,71 @@
 package main
 
+import (
+	"fmt"
+	"log"
+	"log/slog"
+	"os"
+	"os/signal"
+	"sync"
+
+	"searchix/internal/server"
+
+	"github.com/ardanlabs/conf/v3"
+	"github.com/pkg/errors"
+)
+
+var (
+	CommitSHA string
+	ShortSHA  string
+)
+
 func main() {
+	if os.Getenv("DEBUG") != "" {
+		slog.SetLogLoggerLevel(slog.LevelDebug)
+	}
+	log.SetFlags(log.LstdFlags | log.Lmsgprefix)
+	log.SetPrefix("searchix: ")
+
+	runtimeConfig := server.Config{}
+	help, err := conf.Parse("", &runtimeConfig)
+	if err != nil {
+		if errors.Is(err, conf.ErrHelpWanted) {
+			fmt.Println(help)
+			os.Exit(1)
+		}
+		log.Panicf("parsing runtime configuration: %v", err)
+	}
+
+	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()
 }