SharpCAT/Server/SharpCAT.Server/Program.cs
copilot-swe-agent[bot] c20d3a12bb Implement ASP.NET Core Web API server for SharpCAT with REST endpoints
Co-authored-by: ekinnee <1707617+ekinnee@users.noreply.github.com>
2025-08-06 23:26:53 +00:00

68 lines
1.9 KiB
C#

using SharpCAT.Server.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "SharpCAT Server API",
Version = "v1",
Description = "ASP.NET Core Web API for CAT (Computer Aided Transceiver) control using SharpCAT library",
Contact = new Microsoft.OpenApi.Models.OpenApiContact
{
Name = "SharpCAT Project"
}
});
// Include XML comments if available
var xmlFilename = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFilename);
if (File.Exists(xmlPath))
{
c.IncludeXmlComments(xmlPath);
}
});
// Register the serial communication service as a singleton
// to maintain connection state across requests
builder.Services.AddSingleton<ISerialCommunicationService, SerialCommunicationService>();
// Add CORS policy for cross-origin requests
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "SharpCAT Server API v1");
c.RoutePrefix = string.Empty; // Set Swagger UI at app's root
});
}
app.UseCors("AllowAll");
app.UseRouting();
app.MapControllers();
// Add a simple health check endpoint
app.MapGet("/health", () => new { status = "healthy", timestamp = DateTime.UtcNow })
.WithName("HealthCheck")
.WithOpenApi();
app.Run();