summaryrefslogtreecommitdiff
path: root/Program.cs
blob: e78f0d4d0a045109bb2a91aa5ae06330c4c145fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
using HyperBooru.Services;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace HyperBooru;

public class Program {
    public static void Main(string[] args) {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddHttpContextAccessor();
        builder.Services.AddAuthentication(
            CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
        builder.Services.AddControllers().AddJsonOptions(o => {
            var converter = new JsonStringEnumConverter();
            o.JsonSerializerOptions.Converters.Add(converter);
        });
        builder.Services.AddRazorPages();
        builder.Services.AddServerSideBlazor();

        // Add our custom services
        builder.Services.AddSingleton<IConfigService, ConfigService>();
        builder.Services.AddDbContextFactory<HBContext>();
        builder.Services.AddScoped<ISearchService, SearchService>();
        builder.Services.AddScoped<ITagService, TagService>();
        builder.Services.AddScoped<IMediaService, MediaService>();
        builder.Services.AddScoped<IUserService, UserService>();
        builder.Services.AddHostedService<OcrService>();

        var app = builder.Build();

        // Ensure database is created and it's schema is up to date
        using var scope = app.Services.CreateScope();
        using var db    = scope.ServiceProvider.GetRequiredService<HBContext>();
        db.Database.Migrate();

        app.UseHsts();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.MapBlazorHub();
        app.MapControllers();
        app.MapFallbackToPage("/_Host");

        app.Run();
    }
}