test(osiris): test osiris features and ensure it works

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso
2025-07-18 22:58:03 +00:00
parent d9c4e37978
commit 2af731033c
17 changed files with 823 additions and 93 deletions

View File

@@ -2,20 +2,13 @@ package entrypoint
import (
"context"
"crypto/tls"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/TecharoHQ/anubis/cmd/osiris/internal/config"
"github.com/TecharoHQ/anubis/internal"
"github.com/TecharoHQ/anubis/internal/fingerprint"
"github.com/hashicorp/hcl/v2/hclsimple"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
healthv1 "google.golang.org/grpc/health/grpc_health_v1"
)
@@ -24,12 +17,9 @@ type Options struct {
ConfigFname string
}
func Main(opts Options) error {
func Main(ctx context.Context, opts Options) error {
internal.SetHealth("osiris", healthv1.HealthCheckResponse_NOT_SERVING)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
var cfg config.Toplevel
if err := hclsimple.DecodeFile(opts.ConfigFname, nil, &cfg); err != nil {
return fmt.Errorf("can't read configuration file %s:\n\n%w", opts.ConfigFname, err)
@@ -57,13 +47,11 @@ func Main(opts Options) error {
go func(ctx context.Context) {
<-ctx.Done()
ln.Close()
}(gCtx)
}(ctx)
slog.Info("listening", "for", "http", "bind", cfg.Bind.HTTP)
srv := http.Server{Handler: rtr, ErrorLog: internal.GetFilteredHTTPLogger()}
return srv.Serve(ln)
return rtr.HandleHTTP(gCtx, ln)
})
// HTTPS
@@ -77,70 +65,16 @@ func Main(opts Options) error {
go func(ctx context.Context) {
<-ctx.Done()
ln.Close()
}(gCtx)
tc := &tls.Config{
GetCertificate: rtr.GetCertificate,
}
srv := &http.Server{
Addr: cfg.Bind.HTTPS,
Handler: rtr,
ErrorLog: internal.GetFilteredHTTPLogger(),
TLSConfig: tc,
}
fingerprint.ApplyTLSFingerprinter(srv)
}(ctx)
slog.Info("listening", "for", "https", "bind", cfg.Bind.HTTPS)
return srv.ServeTLS(ln, "", "")
return rtr.HandleHTTPS(gCtx, ln)
})
// Metrics
g.Go(func() error {
ln, err := net.Listen("tcp", cfg.Bind.Metrics)
if err != nil {
return fmt.Errorf("(metrics) can't bind to tcp %s: %w", cfg.Bind.Metrics, err)
}
defer ln.Close()
go func(ctx context.Context) {
<-ctx.Done()
ln.Close()
}(gCtx)
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
st, ok := internal.GetHealth("osiris")
if !ok {
slog.Error("health service osiris does not exist, file a bug")
}
switch st {
case healthv1.HealthCheckResponse_NOT_SERVING:
http.Error(w, "NOT OK", http.StatusInternalServerError)
return
case healthv1.HealthCheckResponse_SERVING:
fmt.Fprintln(w, "OK")
return
default:
http.Error(w, "UNKNOWN", http.StatusFailedDependency)
return
}
})
slog.Info("listening", "for", "metrics", "bind", cfg.Bind.Metrics)
srv := http.Server{
Addr: cfg.Bind.Metrics,
Handler: mux,
ErrorLog: internal.GetFilteredHTTPLogger(),
}
return srv.Serve(ln)
return rtr.ListenAndServeMetrics(gCtx, cfg.Bind.Metrics)
})
internal.SetHealth("osiris", healthv1.HealthCheckResponse_SERVING)