Last active
February 4, 2024 09:33
-
-
Save usausa/b0aec48627954ee7c5e134abb14fb5a5 to your computer and use it in GitHub Desktop.
Command line server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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