2019-08-01 00:13:36 +00:00
|
|
|
module MyPrayerJournal.Api
|
2018-08-07 02:21:28 +00:00
|
|
|
|
|
|
|
open Microsoft.AspNetCore.Builder
|
|
|
|
open Microsoft.AspNetCore.Hosting
|
2019-07-29 03:21:05 +00:00
|
|
|
open System.IO
|
2018-08-07 02:21:28 +00:00
|
|
|
|
|
|
|
/// Configuration functions for the application
|
|
|
|
module Configure =
|
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// Configure the content root
|
|
|
|
let contentRoot root (bldr : IWebHostBuilder) =
|
|
|
|
bldr.UseContentRoot root
|
|
|
|
|
2018-08-07 02:21:28 +00:00
|
|
|
open Microsoft.Extensions.Configuration
|
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// Configure the application configuration
|
|
|
|
let appConfiguration (bldr : IWebHostBuilder) =
|
|
|
|
let configuration (ctx : WebHostBuilderContext) (cfg : IConfigurationBuilder) =
|
|
|
|
cfg.SetBasePath(ctx.HostingEnvironment.ContentRootPath)
|
|
|
|
.AddJsonFile("appsettings.json", optional = true, reloadOnChange = true)
|
|
|
|
.AddJsonFile(sprintf "appsettings.%s.json" ctx.HostingEnvironment.EnvironmentName)
|
|
|
|
.AddEnvironmentVariables ()
|
|
|
|
|> ignore
|
|
|
|
bldr.ConfigureAppConfiguration configuration
|
2018-08-07 02:21:28 +00:00
|
|
|
|
2019-07-15 01:47:31 +00:00
|
|
|
open Microsoft.AspNetCore.Server.Kestrel.Core
|
|
|
|
|
2018-08-07 02:21:28 +00:00
|
|
|
/// Configure Kestrel from appsettings.json
|
2019-07-29 03:21:05 +00:00
|
|
|
let kestrel (bldr : IWebHostBuilder) =
|
2019-07-31 04:56:34 +00:00
|
|
|
let kestrelOpts (ctx : WebHostBuilderContext) (opts : KestrelServerOptions) =
|
2019-07-29 03:21:05 +00:00
|
|
|
(ctx.Configuration.GetSection >> opts.Configure >> ignore) "Kestrel"
|
2019-07-31 04:56:34 +00:00
|
|
|
bldr.UseKestrel().ConfigureKestrel kestrelOpts
|
2019-07-15 01:47:31 +00:00
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// Configure the web root directory
|
|
|
|
let webRoot pathSegments (bldr : IWebHostBuilder) =
|
|
|
|
(Path.Combine >> bldr.UseWebRoot) pathSegments
|
2019-02-23 19:59:32 +00:00
|
|
|
|
2019-07-15 01:47:31 +00:00
|
|
|
open Giraffe
|
2019-07-29 03:21:05 +00:00
|
|
|
open Giraffe.Serialization
|
2019-07-15 01:47:31 +00:00
|
|
|
open Microsoft.AspNetCore.Authentication.JwtBearer
|
|
|
|
open Microsoft.Extensions.DependencyInjection
|
2019-07-29 03:21:05 +00:00
|
|
|
open MyPrayerJournal.Indexes
|
|
|
|
open Newtonsoft.Json
|
2019-08-01 00:13:36 +00:00
|
|
|
open Newtonsoft.Json.Serialization
|
2019-07-15 01:47:31 +00:00
|
|
|
open Raven.Client.Documents
|
|
|
|
open Raven.Client.Documents.Indexes
|
|
|
|
open System.Security.Cryptography.X509Certificates
|
|
|
|
|
2018-08-07 02:21:28 +00:00
|
|
|
/// Configure dependency injection
|
2019-07-29 03:21:05 +00:00
|
|
|
let services (bldr : IWebHostBuilder) =
|
|
|
|
let svcs (sc : IServiceCollection) =
|
|
|
|
/// Custom settings for the JSON serializer (uses compact representation for options and DUs)
|
|
|
|
let jsonSettings =
|
|
|
|
let x = NewtonsoftJsonSerializer.DefaultSettings
|
2019-08-01 00:13:36 +00:00
|
|
|
Converters.all |> List.ofSeq |> List.iter x.Converters.Add
|
2019-07-29 03:21:05 +00:00
|
|
|
x.NullValueHandling <- NullValueHandling.Ignore
|
|
|
|
x.MissingMemberHandling <- MissingMemberHandling.Error
|
|
|
|
x.Formatting <- Formatting.Indented
|
2019-08-01 00:13:36 +00:00
|
|
|
x.ContractResolver <- DefaultContractResolver ()
|
2019-07-29 03:21:05 +00:00
|
|
|
x
|
|
|
|
|
|
|
|
use sp = sc.BuildServiceProvider ()
|
|
|
|
let cfg = sp.GetRequiredService<IConfiguration> ()
|
|
|
|
sc.AddGiraffe()
|
|
|
|
.AddAuthentication(
|
|
|
|
/// Use HTTP "Bearer" authentication with JWTs
|
|
|
|
fun opts ->
|
|
|
|
opts.DefaultAuthenticateScheme <- JwtBearerDefaults.AuthenticationScheme
|
|
|
|
opts.DefaultChallengeScheme <- JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
.AddJwtBearer(
|
|
|
|
/// Configure JWT options with Auth0 options from configuration
|
|
|
|
fun opts ->
|
|
|
|
let jwtCfg = cfg.GetSection "Auth0"
|
|
|
|
opts.Authority <- sprintf "https://%s/" jwtCfg.["Domain"]
|
|
|
|
opts.Audience <- jwtCfg.["Id"])
|
|
|
|
|> ignore
|
|
|
|
sc.AddSingleton<IJsonSerializer> (NewtonsoftJsonSerializer jsonSettings)
|
|
|
|
|> ignore
|
|
|
|
let config = sc.BuildServiceProvider().GetRequiredService<IConfiguration>().GetSection "RavenDB"
|
|
|
|
let store = new DocumentStore ()
|
|
|
|
store.Urls <- [| config.["URL"] |]
|
|
|
|
store.Database <- config.["Database"]
|
2019-08-01 00:13:36 +00:00
|
|
|
match isNull config.["Certificate"] with
|
|
|
|
| true -> ()
|
|
|
|
| false -> store.Certificate <- new X509Certificate2 (config.["Certificate"], config.["Password"])
|
|
|
|
store.Conventions.CustomizeJsonSerializer <- fun x -> Converters.all |> List.ofSeq |> List.iter x.Converters.Add
|
2019-07-29 03:21:05 +00:00
|
|
|
store.Initialize () |> (sc.AddSingleton >> ignore)
|
2019-08-01 00:13:36 +00:00
|
|
|
IndexCreation.CreateIndexes (typeof<Requests_AsJournal>.Assembly, store)
|
2019-07-29 03:21:05 +00:00
|
|
|
bldr.ConfigureServices svcs
|
2018-08-07 02:21:28 +00:00
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
open Microsoft.Extensions.Logging
|
|
|
|
|
|
|
|
/// Configure logging
|
|
|
|
let logging (bldr : IWebHostBuilder) =
|
|
|
|
let logz (log : ILoggingBuilder) =
|
|
|
|
let env = log.Services.BuildServiceProvider().GetService<IHostingEnvironment> ()
|
|
|
|
match env.IsDevelopment () with
|
|
|
|
| true -> log
|
|
|
|
| false -> log.AddFilter(fun l -> l > LogLevel.Information)
|
|
|
|
|> function l -> l.AddConsole().AddDebug()
|
|
|
|
|> ignore
|
|
|
|
bldr.ConfigureLogging logz
|
|
|
|
|
|
|
|
open System
|
2018-08-07 02:21:28 +00:00
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// Configure the web application
|
|
|
|
let application (bldr : IWebHostBuilder) =
|
|
|
|
let appConfig =
|
|
|
|
Action<IApplicationBuilder> (
|
|
|
|
fun (app : IApplicationBuilder) ->
|
|
|
|
let env = app.ApplicationServices.GetService<IHostingEnvironment> ()
|
|
|
|
match env.IsDevelopment () with
|
|
|
|
| true -> app.UseDeveloperExceptionPage ()
|
|
|
|
| false -> app.UseGiraffeErrorHandler Handlers.Error.error
|
|
|
|
|> function
|
|
|
|
| a ->
|
|
|
|
a.UseAuthentication()
|
|
|
|
.UseStaticFiles()
|
2019-08-21 02:41:49 +00:00
|
|
|
.UseGiraffe Handlers.webApp
|
2019-07-29 03:21:05 +00:00
|
|
|
|> ignore)
|
|
|
|
bldr.Configure appConfig
|
|
|
|
|
|
|
|
/// Compose all the configurations into one
|
|
|
|
let webHost appRoot pathSegments =
|
|
|
|
contentRoot appRoot
|
|
|
|
>> appConfiguration
|
|
|
|
>> kestrel
|
|
|
|
>> webRoot (Array.concat [ [| appRoot |]; pathSegments ])
|
|
|
|
>> services
|
|
|
|
>> logging
|
|
|
|
>> application
|
|
|
|
|
|
|
|
/// Build the web host from the given configuration
|
|
|
|
let buildHost (bldr : IWebHostBuilder) = bldr.Build ()
|
2018-08-07 02:21:28 +00:00
|
|
|
|
2019-08-01 00:13:36 +00:00
|
|
|
let exitCode = 0
|
|
|
|
|
|
|
|
[<EntryPoint>]
|
|
|
|
let main _ =
|
|
|
|
let appRoot = Directory.GetCurrentDirectory ()
|
|
|
|
use host = WebHostBuilder() |> (Configure.webHost appRoot [| "wwwroot" |] >> Configure.buildHost)
|
|
|
|
host.Run ()
|
|
|
|
exitCode
|