Skip to content

Instantly share code, notes, and snippets.

@usausa
Last active February 4, 2024 09:33
Show Gist options
  • Save usausa/b0aec48627954ee7c5e134abb14fb5a5 to your computer and use it in GitHub Desktop.
Save usausa/b0aec48627954ee7c5e134abb14fb5a5 to your computer and use it in GitHub Desktop.
Command line server
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
var host = Host.CreateDefaultBuilder(args)
.UseWindowsService()
.UseSystemd()
.ConfigureLogging(config =>
{
config.ClearProviders();
})
.ConfigureServices((context, services) =>
{
var setting = context.Configuration.GetSection("Server").Get<ServerSetting>()!;
// Logging
services.AddSerilog(options =>
{
options.ReadFrom.Configuration(context.Configuration);
});
// Health
services
.AddHealthChecks()
.AddCheck("test", () => HealthCheckResult.Healthy());
services.AddSingleton<IHealthCheckPublisher, HealthCheckPublisher>();
services.Configure<HealthCheckPublisherOptions>(options =>
{
options.Delay = TimeSpan.FromSeconds(5);
options.Period = TimeSpan.FromSeconds(15);
});
// OpenTelemetry
services
.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics
.AddRuntimeInstrumentation()
.AddProcessInstrumentation()
.AddApplicationInstrumentation();
metrics.AddPrometheusHttpListener();
});
// Handler
services.AddTcpServer(options =>
{
options.ListenAnyIP<CommandHandler>(setting.Port);
});
services.AddCommands();
// Job
services.AddJobScheduler(options =>
{
options.UseJob<ScheduleJob>(setting.Cron);
});
// Service
services.AddSingleton<DataService>();
})
.Build();
await host.RunAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment