| | 1 | | using System.Reflection; |
| | 2 | | using EF.Blockchain.Domain; |
| | 3 | | using EF.Blockchain.Server.Middleware; |
| | 4 | | using Microsoft.AspNetCore.Http.Json; |
| | 5 | | using Microsoft.OpenApi.Models; |
| | 6 | |
|
| | 7 | | namespace EF.Blockchain.Server.Startup; |
| | 8 | |
|
| | 9 | | public static class ApplicationSetup |
| | 10 | | { |
| | 11 | | public static IServiceCollection AddAppServices(this IServiceCollection services, IConfiguration config) |
| 98 | 12 | | { |
| 112 | 13 | | var miner = config["Blockchain:MinerWallet:PrivateKey"] |
| 112 | 14 | | ?? Environment.GetEnvironmentVariable("BLOCKCHAIN_MINER") |
| 112 | 15 | | ?? "default-miner"; |
| 14 | 16 | |
|
| 98 | 17 | | var minerWallet = new Wallet(miner); |
| 112 | 18 | | services.AddSingleton<Domain.Blockchain>(_ => new Domain.Blockchain(minerWallet.PublicKey)); |
| 14 | 19 | |
|
| 196 | 20 | | services.Configure<JsonOptions>(options => options.SerializerOptions.WriteIndented = true); |
| 28 | 21 | |
|
| 98 | 22 | | services.AddEndpointsApiExplorer(); |
| 14 | 23 | |
|
| | 24 | | // Add CORS |
| 112 | 25 | | services.AddCors(options => |
| 112 | 26 | | { |
| 109 | 27 | | options.AddPolicy("AllowAll", policy => |
| 109 | 28 | | { |
| 109 | 29 | | policy.AllowAnyOrigin() |
| 109 | 30 | | .AllowAnyHeader() |
| 109 | 31 | | .AllowAnyMethod(); |
| 207 | 32 | | }); |
| 207 | 33 | | }); |
| 11 | 34 | |
|
| 109 | 35 | | services.AddSwaggerGen(); |
| 109 | 36 | | services.AddSwaggerGen(options => |
| 88 | 37 | | { |
| 88 | 38 | | options.SwaggerDoc("v1", new OpenApiInfo |
| 91 | 39 | | { |
| 91 | 40 | | Title = "EF Blockchain API", |
| 88 | 41 | | Version = "v1", |
| 88 | 42 | | Description = "Minimal API for educational blockchain project", |
| 91 | 43 | | Contact = new OpenApiContact |
| 88 | 44 | | { |
| 77 | 45 | | Name = "Éderson Fernandes", |
| 77 | 46 | | Url = new Uri("https://github.com/efernandes-tech") |
| 77 | 47 | | } |
| 91 | 48 | | }); |
| 112 | 49 | |
|
| 109 | 50 | | // Enable XML comments if available |
| 102 | 51 | | var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; |
| 77 | 52 | | var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFilename); |
| 112 | 53 | |
|
| 91 | 54 | | if (File.Exists(xmlPath)) |
| 0 | 55 | | { |
| 0 | 56 | | options.IncludeXmlComments(xmlPath, includeControllerXmlComments: true); |
| 14 | 57 | | } |
| 112 | 58 | |
|
| 112 | 59 | | // Enable enums as strings |
| 77 | 60 | | options.UseInlineDefinitionsForEnums(); |
| 175 | 61 | | }); |
| 14 | 62 | |
|
| 112 | 63 | | return services; |
| 112 | 64 | | } |
| 14 | 65 | |
|
| 14 | 66 | | public static WebApplication UseAppMiddleware(this WebApplication app) |
| 112 | 67 | | { |
| 14 | 68 | | // Enable CORS (must be first) |
| 112 | 69 | | app.UseCors("AllowAll"); |
| 14 | 70 | |
|
| 98 | 71 | | app.UseSwagger(); |
| 98 | 72 | | app.UseSwaggerUI(); |
| 14 | 73 | |
|
| 112 | 74 | | app.UseHttpsRedirection(); |
| 112 | 75 | | app.UseMiddleware<RequestLoggingMiddleware>(); |
| 14 | 76 | |
|
| 112 | 77 | | return app; |
| 112 | 78 | | } |
| 14 | 79 | | } |