Skip to content

Commit 9fecd08

Browse files
committed
Added logging to reflection generation of APIs. Fixes #36
1 parent fb73e2f commit 9fecd08

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

Fritz.InstantAPIs/MapApiExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Http;
55
using System.ComponentModel.DataAnnotations;
66
using System.Reflection;
7+
using Microsoft.Extensions.Logging;
78

89
namespace Fritz.InstantAPIs;
910

@@ -12,12 +13,14 @@ internal class MapApiExtensions
1213

1314
// TODO: Authentication / Authorization
1415
private static Dictionary<Type, PropertyInfo> _IdLookup = new();
16+
private static ILogger Logger;
1517

16-
internal static void Initialize<D,C>()
18+
internal static void Initialize<D,C>(ILogger logger)
1719
where D: DbContext
1820
where C: class
1921
{
2022

23+
Logger = logger;
2124
var theType = typeof(C);
2225
var idProp = theType.GetProperty("id", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) ?? theType.GetProperties().FirstOrDefault(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(KeyAttribute)));
2326

@@ -33,6 +36,7 @@ internal static void MapInstantGetAll<D, C>(IEndpointRouteBuilder app, string ur
3336
where D : DbContext where C : class
3437
{
3538

39+
Logger.LogInformation($"Created API: HTTP GET\t{url}");
3640
app.MapGet(url, ([FromServices] D db) =>
3741
{
3842
return Results.Ok(db.Set<C>());
@@ -51,6 +55,8 @@ internal static void MapGetById<D,C>(IEndpointRouteBuilder app, string url)
5155

5256
if (idProp == null) return;
5357

58+
Logger.LogInformation($"Created API: HTTP GET\t{url}/{{id}}");
59+
5460
app.MapGet($"{url}/{{id}}", async ([FromServices] D db, [FromRoute] string id) =>
5561
{
5662

@@ -76,6 +82,7 @@ internal static void MapInstantPost<D, C>(IEndpointRouteBuilder app, string url)
7682
where D : DbContext where C : class
7783
{
7884

85+
Logger.LogInformation($"Created API: HTTP POST\t{url}");
7986

8087
app.MapPost(url, async ([FromServices] D db, [FromBody] C newObj) =>
8188
{
@@ -92,6 +99,7 @@ internal static void MapInstantPut<D, C>(IEndpointRouteBuilder app, string url)
9299
where D : DbContext where C : class
93100
{
94101

102+
Logger.LogInformation($"Created API: HTTP PUT\t{url}");
95103

96104
app.MapPut($"{url}/{{id}}", async ([FromServices] D db, [FromRoute] string id, [FromBody] C newObj) =>
97105
{
@@ -113,6 +121,7 @@ internal static void MapDeleteById<D, C>(IEndpointRouteBuilder app, string url)
113121
var idProp = _IdLookup[theType];
114122

115123
if (idProp == null) return;
124+
Logger.LogInformation($"Created API: HTTP DELETE\t{url}");
116125

117126
app.MapDelete($"{url}/{{id}}", async ([FromServices] D db, [FromRoute] string id) =>
118127
{

Fritz.InstantAPIs/WebApplicationExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using Microsoft.AspNetCore.Routing;
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
45
using Microsoft.Extensions.Options;
56
using System.Reflection;
67

78
namespace Microsoft.AspNetCore.Builder;
89

910
public static class WebApplicationExtensions
1011
{
12+
13+
internal const string LOGGER_CATEGORY_NAME = "InstantAPI";
1114

1215
private static InstantAPIsConfig Configuration { get; set; } = new();
1316

@@ -33,6 +36,9 @@ public static IEndpointRouteBuilder MapInstantAPIs<D>(this IEndpointRouteBuilder
3336
private static void MapInstantAPIsUsingReflection<D>(IEndpointRouteBuilder app, IEnumerable<TypeTable> requestedTables) where D : DbContext
3437
{
3538

39+
var loggerFactory = app.ServiceProvider.GetRequiredService<ILoggerFactory>();
40+
var logger = loggerFactory.CreateLogger(LOGGER_CATEGORY_NAME);
41+
3642
var allMethods = typeof(MapApiExtensions).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name.StartsWith("Map")).ToArray();
3743
var initialize = typeof(MapApiExtensions).GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Static);
3844
foreach (var table in requestedTables)
@@ -41,7 +47,7 @@ private static void MapInstantAPIsUsingReflection<D>(IEndpointRouteBuilder app,
4147
// The default URL for an InstantAPI is /api/TABLENAME
4248
var url = $"/api/{table.Name}";
4349

44-
initialize.MakeGenericMethod(typeof(D), table.InstanceType).Invoke(null, null);
50+
initialize.MakeGenericMethod(typeof(D), table.InstanceType).Invoke(null, new[] { logger });
4551

4652
// The remaining private static methods in this class build out the Mapped API methods..
4753
// let's use some reflection to get them

WorkingApi/MyContext.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ namespace WorkingApi;
44

55
public class MyContext : DbContext
66
{
7-
public MyContext()
8-
{
9-
10-
}
117
public MyContext(DbContextOptions<MyContext> options) : base(options) {}
128

139
public DbSet<Contact> Contacts => Set<Contact>();

WorkingApi/Program.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,15 @@
77
var builder = WebApplication.CreateBuilder(args);
88

99
builder.Services.AddSqlite<MyContext>("Data Source=contacts.db");
10-
//builder.Services.AddEndpointsApiExplorer();
11-
//builder.Services.AddSwaggerGen();
12-
builder.Services.AddInstantAPIs(options =>
13-
{
14-
options.EnableSwagger = Fritz.InstantAPIs.EnableSwagger.DevelopmentOnly;
15-
options.Swagger = config =>
16-
{
17-
config.SwaggerDoc("v1", new OpenApiInfo
18-
{
19-
Title = "My Working API",
20-
Description = "An ASP.NET Core Web API"
21-
});
22-
};
23-
});
10+
builder.Services.AddInstantAPIs();
2411

2512
var app = builder.Build();
2613

2714
var sw = Stopwatch.StartNew();
2815

29-
app.MapInstantAPIs<MyContext>(options =>
16+
app.MapInstantAPIs<MyContext>(config =>
3017
{
31-
options.IncludeTable(db => db.Contacts, (ApiMethodsToGenerate.GetById | ApiMethodsToGenerate.Get));
18+
config.IncludeTable(db => db.Contacts, ApiMethodsToGenerate.All);
3219
});
33-
Console.WriteLine($"Elapsed time to build InstantAPIs: {sw.Elapsed}");
34-
35-
//app.UseSwagger();
36-
//app.UseSwaggerUI();
3720

3821
app.Run();

0 commit comments

Comments
 (0)