|
1 | | -using System.Threading.Tasks; |
2 | | -using Microsoft.AspNetCore.Hosting; |
3 | | -using Microsoft.Extensions.DependencyInjection; |
4 | | -using Microsoft.Extensions.Hosting; |
| 1 | +using Microsoft.DotNet.PlatformAbstractions; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using Microsoft.OpenApi.Models; |
| 4 | +using MyServiceBroker; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using OpenServiceBroker; |
| 7 | +using OpenServiceBroker.Catalogs; |
| 8 | +using OpenServiceBroker.Instances; |
5 | 9 |
|
6 | | -namespace MyServiceBroker |
| 10 | +var builder = WebApplication.CreateBuilder(args); |
| 11 | + |
| 12 | +// Catalog of available services |
| 13 | +string catalogPath = File.ReadAllText(Path.Combine(ApplicationEnvironment.ApplicationBasePath, "catalog.json")); |
| 14 | +builder.Services.AddSingleton(JsonConvert.DeserializeObject<Catalog>(catalogPath)!); |
| 15 | + |
| 16 | +// Database for storing provisioned service instances |
| 17 | +builder.Services.AddDbContext<MyServiceBroker.DbContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("Database"))); |
| 18 | + |
| 19 | +// Implementations of OpenServiceBroker.Server interfaces |
| 20 | +builder.Services.AddTransient<ICatalogService, CatalogService>() |
| 21 | + .AddTransient<IServiceInstanceBlocking, ServiceInstanceService>(); |
| 22 | + |
| 23 | +// Open Service Broker REST API |
| 24 | +builder.Services.AddControllers() |
| 25 | + .AddOpenServiceBroker(); |
| 26 | + |
| 27 | +// Swagger/OpenAPI |
| 28 | +builder.Services.AddEndpointsApiExplorer(); |
| 29 | +builder.Services.AddSwaggerGen(options => |
7 | 30 | { |
8 | | - // Manage process lifecycle, configuration and logging |
9 | | - public static class Program |
| 31 | + options.SwaggerDoc("v1", new OpenApiInfo |
10 | 32 | { |
11 | | - public static async Task Main(string[] args) |
12 | | - { |
13 | | - var host = CreateHostBuilder(args).Build(); |
14 | | - using (var scope = host.Services.CreateScope()) |
15 | | - await scope.ServiceProvider.GetRequiredService<DbContext>().Database.EnsureCreatedAsync(); |
16 | | - await host.RunAsync(); |
17 | | - } |
18 | | - |
19 | | - public static IHostBuilder CreateHostBuilder(string[] args) => |
20 | | - Host.CreateDefaultBuilder(args) |
21 | | - .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>()); |
22 | | - } |
| 33 | + Title = "My Service Broker", |
| 34 | + Version = "v1" |
| 35 | + }); |
| 36 | +}).AddSwaggerGenNewtonsoftSupport(); |
| 37 | + |
| 38 | +var app = builder.Build(); |
| 39 | + |
| 40 | +// Configure the HTTP request pipeline. |
| 41 | +if (app.Environment.IsDevelopment()) |
| 42 | +{ |
| 43 | + app.UseSwagger(); |
| 44 | + app.UseSwaggerUI(); |
23 | 45 | } |
| 46 | + |
| 47 | +app.UseHttpsRedirection(); |
| 48 | + |
| 49 | +app.UseAuthorization(); |
| 50 | + |
| 51 | +app.MapControllers(); |
| 52 | + |
| 53 | +app.Run(); |
0 commit comments