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