Use Program cs style entry point
This commit is contained in:
@@ -3,69 +3,76 @@ using Electricity.Api;
|
||||
using Electricity.Api.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var rootCommand = new RootCommand("ElectricityServer is a small REST API to access the electricity log database");
|
||||
var connectionStringArgument = new Option<string>(
|
||||
name: "--connection-string",
|
||||
description: "Filepath to the Sqlite3 database file (*.db)");
|
||||
rootCommand.AddOption(connectionStringArgument);
|
||||
|
||||
var result = rootCommand.Parse(args);
|
||||
|
||||
if (result.Errors.Any())
|
||||
internal class Program
|
||||
{
|
||||
foreach (var error in result.Errors)
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(error.Message);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
var rootCommand = new RootCommand("ElectricityServer is a small REST API to access the electricity log database");
|
||||
var connectionStringArgument = new Option<string>(
|
||||
name: "--connection-string",
|
||||
description: "Filepath to the Sqlite3 database file (*.db)");
|
||||
rootCommand.AddOption(connectionStringArgument);
|
||||
|
||||
var sqlite3DatabaseFilePath = result.GetValueForOption(connectionStringArgument);
|
||||
if (!File.Exists(sqlite3DatabaseFilePath))
|
||||
{
|
||||
Console.WriteLine($"Sqlite3 database <{sqlite3DatabaseFilePath}> does not exist or is inaccessible");
|
||||
return 1;
|
||||
}
|
||||
var result = rootCommand.Parse(args);
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddDbContext<DatabaseContext>((DbContextOptionsBuilder builder) =>
|
||||
{
|
||||
builder.UseSqlite($"Data Source={sqlite3DatabaseFilePath}");
|
||||
});
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(
|
||||
name: "default",
|
||||
policy =>
|
||||
if (result.Errors.Any())
|
||||
{
|
||||
policy.WithOrigins("http://localhost:8080");
|
||||
foreach (var error in result.Errors)
|
||||
{
|
||||
Console.WriteLine(error.Message);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
var sqlite3DatabaseFilePath = result.GetValueForOption(connectionStringArgument);
|
||||
if (!File.Exists(sqlite3DatabaseFilePath))
|
||||
{
|
||||
Console.WriteLine($"Sqlite3 database <{sqlite3DatabaseFilePath}> does not exist or is inaccessible");
|
||||
return 1;
|
||||
}
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddDbContext<DatabaseContext>((builder) =>
|
||||
{
|
||||
builder.UseSqlite($"Data Source={sqlite3DatabaseFilePath}");
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(config =>
|
||||
{
|
||||
config.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
|
||||
config.JsonSerializerOptions.WriteIndented = true;
|
||||
});
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddScoped<ElectricityService>();
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(
|
||||
name: "default",
|
||||
policy =>
|
||||
{
|
||||
policy.WithOrigins("http://localhost:8080");
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseHttpsRedirection();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
app.UseCors("default");
|
||||
}
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(config =>
|
||||
{
|
||||
config.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
|
||||
config.JsonSerializerOptions.WriteIndented = true;
|
||||
});
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddScoped<ElectricityService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseHttpsRedirection();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
app.UseCors("default");
|
||||
}
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user