-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
executable file
·166 lines (132 loc) · 4.9 KB
/
Program.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System.Reflection;
using CodeMechanic.Diagnostics;
using CodeMechanic.Embeds;
using CodeMechanic.FileSystem;
using CodeMechanic.RazorHAT;
using CodeMechanic.RazorHAT.Services;
using CodeMechanic.Shargs;
using CodeMechanic.Types;
using Hydro.Configuration;
using nugsnet6;
using nugsnet6.Services.Sqlite;
using ILogger = Serilog.ILogger;
internal static class Program
{
static async Task Main(string[] args)
{
DotEnv.Load();
var arguments = new ArgsMap(args);
if (arguments.HasFlag("--debug"))
{
// foreach (var arg in args)
// {
// Console.WriteLine(arg);
// }
arguments.Dump(ignoreNulls: true);
}
// var logger = new LoggerConfiguration()
// .MinimumLevel.Information()
// .WriteTo.Console()
// .WriteTo.File("./nugs/nugs.log",
// rollingInterval: RollingInterval.Day,
// rollOnFileSizeLimit: true
// )
// .CreateLogger();
// arguments.Dump();
(bool run_as_web, bool run_as_cli) = arguments.GetRunModes();
Console.WriteLine($"{nameof(run_as_web)} {run_as_web}");
Console.WriteLine($"{nameof(run_as_cli)} {run_as_cli}");
if (run_as_cli)
await RunAsCli(arguments);
if (run_as_web)
RunAsWeb(args);
}
private static async Task RunAsCli(IArgsMap arguments)
{
Console.WriteLine(nameof(RunAsCli));
var services = CreateServices(arguments);
Application app = services.GetRequiredService<Application>();
app.Run();
}
private static ServiceProvider CreateServices(IArgsMap arguments)
{
var serviceProvider = new ServiceCollection()
.AddSingleton<IArgsMap>(arguments)
.AddSingleton<Application>()
.BuildServiceProvider();
return serviceProvider;
}
static void RunAsWeb(string[] args)
{
Console.WriteLine(nameof(RunAsWeb));
var builder = WebApplication.CreateBuilder(args);
// Read .env values for setting up services
bool dev_mode =
Environment.GetEnvironmentVariable("DEVMODE").ToBoolean();
Console.WriteLine("Developer mode (all debugs enabled)? " + dev_mode);
// Add services to the container.
builder.Services.AddRazorPages();
var props_service = new PropertyCache();
builder.Services.AddSingleton<ILocalLogger, LocalLoggerService>();
builder.Services.AddSingleton<IPropertyCache>(props_service);
// builder.Services.AddSingleton<IDriver, Neo4jDriver>();
builder.Services.AddSingleton<IMarkdownService, MarkdownService>();
builder.Services.AddSingleton<ICsvService>(
new CsvService(props_service, dev_mode));
builder.Services
.AddSingleton<ISqliteInsightsService, SqliteInsightsService>();
builder.Services.AddScoped<IFakerService, FakerService>();
builder.Services.AddScoped<IImageService, ImageService>();
// TODO: Fix the httpclient factory
// TODO: move to razorhat library
// builder.Services.AddHttpClient<IndexModel>(client =>
// {
// client.BaseAddress = new Uri(builder.Configuration["BaseUrl"]);
// });
// .AddPolicyHandler(GetRetryPolicy())
// .AddPolicyHandler(GetCircuitBreakerPolicy());
var main_assembly = Assembly.GetExecutingAssembly();
builder.Services.AddSingleton<IEmbeddedResourceQuery>(
new EmbeddedResourceService(
new Assembly[] { main_assembly },
debugMode: false
).CacheAllEmbeddedFileContents()
);
builder.Services.ConfigureAirtable();
builder.Services.ConfigureNeo4j();
builder.Services.AddHydro();
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();
builder.Services.AddHttpClient();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.UseExceptionHandler();
app.MapControllerRoute(name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.UseHydro(builder.Environment);
app.Run();
}
}
public class Application
{
private readonly ILogger _logger;
public Application(ILogger logger)
{
_logger = logger;
}
public void Run()
{
// _logger.Information("Hello, World!");
}
}