This commit is contained in:
@@ -2,10 +2,16 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"go.ntppool.org/common/logger"
|
||||||
"go.ntppool.org/data-api/server"
|
"go.ntppool.org/data-api/server"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
@@ -25,25 +31,36 @@ func (cli *CLI) serverCmd() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cli *CLI) serverCLI(cmd *cobra.Command, args []string) error {
|
func (cli *CLI) serverCLI(cmd *cobra.Command, args []string) error {
|
||||||
|
log := logger.Setup()
|
||||||
|
|
||||||
// cfg := cli.Config
|
// cfg := cli.Config
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
g, ctx := errgroup.WithContext(ctx)
|
g, ctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
g.Go(func() error {
|
|
||||||
srv, err := server.NewServer(ctx, cfgFile)
|
srv, err := server.NewServer(ctx, cfgFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("srv setup: %s", err)
|
return fmt.Errorf("srv setup: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.Go(func() error {
|
||||||
return srv.Run()
|
return srv.Run()
|
||||||
})
|
})
|
||||||
|
|
||||||
err := g.Wait()
|
g.Go(func() error {
|
||||||
if err != nil {
|
<-ctx.Done()
|
||||||
log.Printf("server error: %s", err)
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
return srv.Shutdown(shutdownCtx)
|
||||||
|
})
|
||||||
|
|
||||||
|
err = g.Wait()
|
||||||
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
log.Error("server error", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel()
|
// don't tell cobra something went wrong as it'll just
|
||||||
return err
|
// print usage information
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package server
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -14,7 +15,6 @@ import (
|
|||||||
slogecho "github.com/samber/slog-echo"
|
slogecho "github.com/samber/slog-echo"
|
||||||
|
|
||||||
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
|
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
|
||||||
otrace "go.opentelemetry.io/otel/trace"
|
|
||||||
|
|
||||||
"go.ntppool.org/common/health"
|
"go.ntppool.org/common/health"
|
||||||
"go.ntppool.org/common/logger"
|
"go.ntppool.org/common/logger"
|
||||||
@@ -32,7 +32,7 @@ type Server struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
|
||||||
metrics *metricsserver.Metrics
|
metrics *metricsserver.Metrics
|
||||||
tracer otrace.Tracer
|
tpShutdown []tracing.TpShutdownFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(ctx context.Context, configFile string) (*Server, error) {
|
func NewServer(ctx context.Context, configFile string) (*Server, error) {
|
||||||
@@ -52,7 +52,7 @@ func NewServer(ctx context.Context, configFile string) (*Server, error) {
|
|||||||
metrics: metricsserver.New(),
|
metrics: metricsserver.New(),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tracing.InitTracer(ctx, &tracing.TracerConfig{
|
tpShutdown, err := tracing.InitTracer(ctx, &tracing.TracerConfig{
|
||||||
ServiceName: "data-api",
|
ServiceName: "data-api",
|
||||||
Environment: "",
|
Environment: "",
|
||||||
})
|
})
|
||||||
@@ -60,7 +60,8 @@ func NewServer(ctx context.Context, configFile string) (*Server, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.tracer = tracing.Tracer()
|
srv.tpShutdown = append(srv.tpShutdown, tpShutdown)
|
||||||
|
// srv.tracer = tracing.Tracer()
|
||||||
return srv, nil
|
return srv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +85,8 @@ func (srv *Server) Run() error {
|
|||||||
e.Use(otelecho.Middleware("data-api"))
|
e.Use(otelecho.Middleware("data-api"))
|
||||||
e.Use(slogecho.New(log))
|
e.Use(slogecho.New(log))
|
||||||
|
|
||||||
|
srv.tpShutdown = append(srv.tpShutdown, e.Shutdown)
|
||||||
|
|
||||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||||
AllowOrigins: []string{
|
AllowOrigins: []string{
|
||||||
"http://localhost", "http://localhost:5173", "http://localhost:8080",
|
"http://localhost", "http://localhost:5173", "http://localhost:8080",
|
||||||
@@ -114,6 +117,18 @@ func (srv *Server) Run() error {
|
|||||||
return g.Wait()
|
return g.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv *Server) Shutdown(ctx context.Context) error {
|
||||||
|
logger.Setup().Info("Shutting down")
|
||||||
|
errs := []error{}
|
||||||
|
for _, fn := range srv.tpShutdown {
|
||||||
|
err := fn(ctx)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
|
|
||||||
func (srv *Server) userCountryData(c echo.Context) error {
|
func (srv *Server) userCountryData(c echo.Context) error {
|
||||||
|
|
||||||
ctx := c.Request().Context()
|
ctx := c.Request().Context()
|
||||||
|
|||||||
Reference in New Issue
Block a user