|
| 1 | +package io.cloudquery.server; |
| 2 | + |
| 3 | +import io.cloudquery.internal.servers.discovery.v1.DiscoverServer; |
| 4 | +import io.cloudquery.internal.servers.plugin.v3.PluginServer; |
| 5 | +import io.cloudquery.plugin.Plugin; |
| 6 | +import io.cloudquery.server.AddressConverter.Address; |
| 7 | +import io.grpc.Grpc; |
| 8 | +import io.grpc.InsecureServerCredentials; |
| 9 | +import io.grpc.Server; |
| 10 | +import io.grpc.protobuf.services.ProtoReflectionService; |
| 11 | +import lombok.ToString; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.Callable; |
| 15 | +import java.util.concurrent.Executors; |
| 16 | +import java.util.logging.Level; |
| 17 | +import java.util.logging.Logger; |
| 18 | + |
| 19 | +import static picocli.CommandLine.Command; |
| 20 | +import static picocli.CommandLine.Option; |
| 21 | + |
| 22 | +@Command |
| 23 | +@ToString |
| 24 | +public class ServeCommand implements Callable<Integer> { |
| 25 | + private static final Logger logger = Logger.getLogger(ServeCommand.class.getName()); |
| 26 | + public static final List<Integer> DISCOVERY_VERSIONS = List.of(3); |
| 27 | + |
| 28 | + @Option(names = "--address", converter = AddressConverter.class, description = "address to serve on. can be tcp: localhost:7777 or unix socket: `/tmp/plugin.rpc.sock` (default \"${DEFAULT-VALUE}\")") |
| 29 | + private Address address = new Address("localhost", 7777); |
| 30 | + |
| 31 | + @Option(names = "--log-format", description = "log format. one of: text,json (default \"${DEFAULT-VALUE}\")") |
| 32 | + private String logFormat = "text"; |
| 33 | + |
| 34 | + @Option(names = "--log-level", description = "log level. one of: trace,debug,info,warn,error (default \"${DEFAULT-VALUE}\")") |
| 35 | + private String logLevel = "info"; |
| 36 | + |
| 37 | + @Option(names = "--network", description = "the network must be \"tcp\", \"tcp4\", \"tcp6\", \"unix\" or \"unixpacket\" (default \"${DEFAULT-VALUE}\")") |
| 38 | + private String network = "tcp"; |
| 39 | + |
| 40 | + @Option(names = "--disable-sentry", description = "disable sentry") |
| 41 | + private Boolean disableSentry = false; |
| 42 | + |
| 43 | + @Option(names = "--otel-endpoint", description = "Open Telemetry HTTP collector endpoint") |
| 44 | + private String otelEndpoint = ""; |
| 45 | + |
| 46 | + @Option(names = "--otel-endpoint-insecure", description = "use Open Telemetry HTTP endpoint (for development only)") |
| 47 | + private Boolean otelEndpointInsecure = false; |
| 48 | + |
| 49 | + private final Plugin plugin; |
| 50 | + |
| 51 | + public ServeCommand(Plugin plugin) { |
| 52 | + this.plugin = plugin; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Integer call() throws Exception { |
| 57 | + // Initialize a logger |
| 58 | + |
| 59 | + // Configure open telemetry |
| 60 | + |
| 61 | + // Configure test listener |
| 62 | + |
| 63 | + // Configure gRPC server |
| 64 | + Server server = Grpc.newServerBuilderForPort(address.port(), InsecureServerCredentials.create()). |
| 65 | + addService(new DiscoverServer(DISCOVERY_VERSIONS)). |
| 66 | + addService(new PluginServer(plugin)). |
| 67 | + addService(ProtoReflectionService.newInstance()). |
| 68 | + executor(Executors.newFixedThreadPool(10)). |
| 69 | + build(); |
| 70 | + |
| 71 | + // Configure sentry |
| 72 | + |
| 73 | + // Log we are listening on address and port |
| 74 | + |
| 75 | + // Run gRPC server and block |
| 76 | + server.start(); |
| 77 | + logger.log(Level.INFO, "Started server on {0}", address); |
| 78 | + server.awaitTermination(); |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments