v1.0 release

This commit is contained in:
Cameron
2024-08-27 03:29:38 -05:00
parent a4403ce17b
commit ef7c3f9dbd
38 changed files with 1172 additions and 73 deletions

View File

@@ -5,17 +5,54 @@ using Microsoft.Extensions.DependencyInjection;
using ComiServ.Background;
using Swashbuckle.AspNetCore.SwaggerGen;
using ComiServ.Entities;
var builder = WebApplication.CreateBuilder(args);
using ComiServ.Services;
using ComiServ.Middleware;
using ComiServ.Controllers;
using System.Text;
var CONFIG_FILEPATH = "config.json";
var configService = new ConfigService(CONFIG_FILEPATH);
var configService = new JsonConfigService(CONFIG_FILEPATH);
var config = configService.Config;
var ConnectionString = $"Data Source={config.DatabaseFile};Mode=ReadWriteCreate";
// Add services to the container.
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "--addadmin")
{
string username;
if (args.ElementAtOrDefault(i + 1) is string _username)
{
username = _username;
}
else
{
Console.Write("Username: ");
username = Console.ReadLine()
?? throw new Exception("must provide a username");
}
username = username.Trim();
Console.Write("Password: ");
string password = Console.ReadLine()?.Trim()
?? throw new Exception("must provide a username");
var salt = User.MakeSalt();
var hashed = User.Hash(Encoding.UTF8.GetBytes(password), salt);
using var context = new ComicsContext(
new DbContextOptionsBuilder<ComicsContext>()
.UseSqlite(ConnectionString).Options);
context.Users.Add(new User()
{
Username = username,
Salt = salt,
HashedPassword = hashed,
UserTypeId = UserTypeEnum.Administrator
});
context.SaveChanges();
return;
}
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
@@ -32,11 +69,11 @@ builder.Services.AddSingleton<IComicAnalyzer>(sp =>
logger: sp.GetRequiredService<ILogger<IComicAnalyzer>>()));
builder.Services.AddSingleton<IComicScanner>(sp =>
new ComicScanner(provider: sp));
builder.Services.AddSingleton<IPictureConverter>(
new ResharperPictureConverter(true));
builder.Services.AddHttpLogging(o => { });
//builder.Services.AddRazorPages().AddRazorPagesOptions(o =>
//{
// o.RootDirectory = "/Pages";
//});
builder.Services.AddScoped<IAuthenticationService>(
sp => new AuthenticationService());
builder.Services.AddLogging(config =>
{
config.AddConsole();
@@ -68,7 +105,23 @@ scanner.ScheduleRepeatedLibraryScans(TimeSpan.FromDays(1));
app.UseHttpsRedirection();
app.UseAuthorization();
//ensures that the user is authenticated (if auth is provided) but does not restrict access to any routes
app.UseBasicAuthentication([]);
//require user or admin account to access any comic resource (uses the authentication
app.UseWhen(context => context.Request.Path.StartsWithSegments(ComicController.ROUTE), appBuilder =>
{
appBuilder.UseBasicAuthentication([UserTypeEnum.User, UserTypeEnum.Administrator]);
});
//require user or admin account to access any user resource
app.UseWhen(context => context.Request.Path.StartsWithSegments(UserController.ROUTE), appBuilder =>
{
appBuilder.UseBasicAuthentication([UserTypeEnum.User, UserTypeEnum.Administrator]);
});
//require admin account to access any task resource
app.UseWhen(context => context.Request.Path.StartsWithSegments(TaskController.ROUTE), appBuilder =>
{
appBuilder.UseBasicAuthentication([UserTypeEnum.Administrator]);
});
app.MapControllers();