more changes
Some checks failed
Build and Publish / Metadata (push) Successful in 10s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 49s
Build and Publish / BuildAndDeployArm64 (push) Has been cancelled

This commit is contained in:
matst80
2025-11-05 17:18:09 +01:00
parent 5533d241e9
commit 162a2638fa
2 changed files with 60 additions and 40 deletions

View File

@@ -5,12 +5,12 @@ import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"time"
"git.tornberg.me/go-cart-actor/pkg/actor"
@@ -159,16 +159,15 @@ func main() {
// go diskStorage.SaveLoop(10 * time.Second)
UseDiscovery(pool)
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGTERM)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
otelShutdown, err := setupOTelSDK(context.Background())
otelShutdown, err := setupOTelSDK(ctx)
if err != nil {
log.Fatalf("Unable to start otel %v", err)
}
mux.Handle("/cart/", http.StripPrefix("/cart", syncedServer.Serve()))
mux.Handle("/cart/", syncedServer.Serve())
// only for local
mux.HandleFunc("GET /add/remote/{host}", func(w http.ResponseWriter, r *http.Request) {
pool.AddRemote(r.PathValue("host"))
@@ -212,20 +211,41 @@ func main() {
mux.HandleFunc("/openapi.json", ServeEmbeddedOpenAPI)
go func() {
sig := <-sigs
fmt.Println("Shutting down due to signal:", sig)
srv := &http.Server{
Addr: ":8080",
BaseContext: func(net.Listener) context.Context { return ctx },
ReadTimeout: 10 * time.Second,
WriteTimeout: 20 * time.Second,
Handler: mux,
}
defer func() {
fmt.Println("Shutting down due to signal")
otelShutdown(context.Background())
diskStorage.Close()
pool.Close()
done <- true
}()
srvErr := make(chan error, 1)
go func() {
srvErr <- srv.ListenAndServe()
}()
log.Print("Server started at port 8080")
go http.ListenAndServe(":8080", mux)
go http.ListenAndServe(":8081", debugMux)
<-done
select {
case err = <-srvErr:
// Error when starting HTTP server.
log.Fatalf("Unable to start server: %v", err)
case <-ctx.Done():
// Wait for first CTRL+C.
// Stop receiving signal notifications as soon as possible.
stop()
}
}