2022-11-19 21:34:49 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-18 15:51:39 +02:00
|
|
|
"context"
|
2022-11-19 21:34:49 +01:00
|
|
|
"os"
|
2023-09-18 11:04:59 +02:00
|
|
|
"os/signal"
|
2024-01-21 21:52:46 +01:00
|
|
|
"runtime/debug"
|
2023-09-18 15:51:39 +02:00
|
|
|
"time"
|
2022-11-19 21:34:49 +01:00
|
|
|
|
|
|
|
"github.com/alecthomas/kong"
|
2023-09-18 11:04:59 +02:00
|
|
|
|
2024-01-21 21:52:46 +01:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
2023-09-18 11:04:59 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
|
2023-10-17 22:41:33 +02:00
|
|
|
castorev1pb "code.tvl.fyi/tvix/castore-go"
|
2024-05-14 12:35:55 +02:00
|
|
|
narBridgeHttp "code.tvl.fyi/tvix/nar-bridge-go/pkg/http"
|
2023-10-17 22:41:33 +02:00
|
|
|
storev1pb "code.tvl.fyi/tvix/store-go"
|
2023-09-18 11:04:59 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-11-19 21:34:49 +01:00
|
|
|
)
|
|
|
|
|
2023-09-18 11:04:59 +02:00
|
|
|
// `help:"Expose a tvix-store gRPC Interface as HTTP NAR/NARinfo"`
|
2022-11-19 21:34:49 +01:00
|
|
|
var cli struct {
|
2023-09-18 11:04:59 +02:00
|
|
|
LogLevel string `enum:"trace,debug,info,warn,error,fatal,panic" help:"The log level to log with" default:"info"`
|
2023-09-24 15:23:11 +02:00
|
|
|
ListenAddr string `name:"listen-addr" help:"The address this service listens on" type:"string" default:"[::]:9000"` //nolint:lll
|
|
|
|
EnableAccessLog bool `name:"access-log" help:"Enable access logging" type:"bool" default:"true" negatable:""` //nolint:lll
|
|
|
|
StoreAddr string `name:"store-addr" help:"The address to the tvix-store RPC interface this will connect to" default:"localhost:8000"` //nolint:lll
|
2024-01-21 21:52:46 +01:00
|
|
|
EnableOtlp bool `name:"otlp" help:"Enable OpenTelemetry for logs, spans, and metrics" default:"true"` //nolint:lll
|
2022-11-19 21:34:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-09-18 11:04:59 +02:00
|
|
|
_ = kong.Parse(&cli)
|
|
|
|
|
2024-01-21 21:52:46 +01:00
|
|
|
logLevel, err := log.ParseLevel(cli.LogLevel)
|
2022-11-19 21:34:49 +01:00
|
|
|
if err != nil {
|
2023-09-18 11:04:59 +02:00
|
|
|
log.Panic("invalid log level")
|
|
|
|
return
|
2022-11-19 21:34:49 +01:00
|
|
|
}
|
2024-01-21 21:52:46 +01:00
|
|
|
log.SetLevel(logLevel)
|
2023-09-18 11:04:59 +02:00
|
|
|
|
2023-09-18 15:51:39 +02:00
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
defer stop()
|
2023-09-18 11:04:59 +02:00
|
|
|
|
2024-01-21 21:52:46 +01:00
|
|
|
if cli.EnableOtlp {
|
|
|
|
buildInfo, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
|
|
|
log.Fatal("failed to read build info")
|
|
|
|
}
|
|
|
|
|
2024-05-14 12:35:55 +02:00
|
|
|
shutdown, err := setupOpenTelemetry(ctx, "nar-bridge-http", buildInfo.Main.Version)
|
2024-01-21 21:52:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("failed to setup OpenTelemetry")
|
|
|
|
}
|
|
|
|
defer shutdown(context.Background())
|
|
|
|
}
|
|
|
|
|
2023-09-18 11:04:59 +02:00
|
|
|
// connect to tvix-store
|
|
|
|
log.Debugf("Dialing to %v", cli.StoreAddr)
|
2024-01-21 21:52:46 +01:00
|
|
|
conn, err := grpc.DialContext(ctx, cli.StoreAddr,
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
|
|
|
|
)
|
2022-11-19 21:34:49 +01:00
|
|
|
if err != nil {
|
2023-09-18 11:04:59 +02:00
|
|
|
log.Fatalf("did not connect: %v", err)
|
2022-11-19 21:34:49 +01:00
|
|
|
}
|
2023-09-18 11:04:59 +02:00
|
|
|
defer conn.Close()
|
|
|
|
|
2023-10-03 13:49:18 +02:00
|
|
|
s := narBridgeHttp.New(
|
2023-09-22 15:38:10 +02:00
|
|
|
castorev1pb.NewDirectoryServiceClient(conn),
|
|
|
|
castorev1pb.NewBlobServiceClient(conn),
|
2023-09-18 11:04:59 +02:00
|
|
|
storev1pb.NewPathInfoServiceClient(conn),
|
|
|
|
cli.EnableAccessLog,
|
|
|
|
30,
|
|
|
|
)
|
2022-11-19 21:34:49 +01:00
|
|
|
|
2023-10-03 14:23:36 +02:00
|
|
|
log.Printf("Starting nar-bridge-http at %v", cli.ListenAddr)
|
2023-09-18 15:51:39 +02:00
|
|
|
go s.ListenAndServe(cli.ListenAddr)
|
|
|
|
|
|
|
|
// listen for the interrupt signal.
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
// Restore default behaviour on the interrupt signal
|
|
|
|
stop()
|
|
|
|
log.Info("Received Signal, shutting down, press Ctl+C again to force.")
|
|
|
|
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-10-05 16:41:03 +02:00
|
|
|
if err := s.Shutdown(timeoutCtx); err != nil {
|
2023-09-18 15:51:39 +02:00
|
|
|
log.WithError(err).Warn("failed to shutdown")
|
2023-09-18 11:04:59 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-11-19 21:34:49 +01:00
|
|
|
}
|