Created
November 3, 2020 15:49
-
-
Save jkone27/874b3c6e1c1f3ddb10d4319deb6561e8 to your computer and use it in GitHub Desktop.
WebApiComparisonStartupFsharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TerseIgnore = | |
//readability trick | |
let (!) a = a |> ignore | |
open TerseIgnore | |
type Startup(configuration: IConfiguration) = | |
member _.ConfigureServices(services: IServiceCollection) = | |
!services.AddSingleton<INumbersService, NumbersService>() | |
!services.AddSingleton<PersonsRepository>() | |
!services.AddTransient<IStartupFilter, DbMigrationStartup>() | |
//configuration using FSharp.Data type provider | |
let settingsFile = "appsettings." + Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") + ".json" | |
let config = AppSettingsProvider.Load(settingsFile) | |
//bind does not work with provided types, for options need to use normal classes | |
//!configuration.GetSection("DbConfiguration").Bind(config.DbConfiguration) | |
//add once and never change (just add changes in appsettings.json!!!) | |
!services.AddSingleton<AppSettings>(config) | |
!services.AddHttpClient<PetsApiClient>() | |
!services.AddHostedService<PetsBackgroundJob>() | |
//needed for swagger | |
!services.AddMvc() | |
!services.AddSwaggerGen(fun c -> | |
c.SwaggerDoc("v1", new OpenApiInfo( Title = "Persons API", Version = "v1" )) | |
) | |
!services.AddControllers() | |
member _.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) = | |
if env.IsDevelopment() then | |
!app.UseDeveloperExceptionPage() | |
!app.UseHttpsRedirection() | |
!app.UseRouting() | |
!app.UseAuthorization() | |
!app.UseEndpoints(fun endpoints -> | |
!endpoints.MapControllers() | |
!endpoints.MapGet("/", fun context -> context.Response.WriteAsync("Welcome to F#!")) | |
) | |
//needed for swagger | |
!app.UseSwagger() | |
!app.UseSwaggerUI(fun c -> | |
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment