2018-08-07 02:21:28 +00:00
|
|
|
namespace MyPrayerJournal.Api
|
|
|
|
|
|
|
|
open Microsoft.AspNetCore.Builder
|
|
|
|
open Microsoft.AspNetCore.Hosting
|
|
|
|
open System
|
|
|
|
|
|
|
|
/// Configuration functions for the application
|
|
|
|
module Configure =
|
|
|
|
|
|
|
|
open Giraffe
|
2019-02-23 19:59:32 +00:00
|
|
|
open Giraffe.Serialization
|
2018-08-07 02:21:28 +00:00
|
|
|
open Giraffe.TokenRouter
|
|
|
|
open Microsoft.AspNetCore.Authentication.JwtBearer
|
|
|
|
open Microsoft.AspNetCore.Server.Kestrel.Core
|
|
|
|
open Microsoft.EntityFrameworkCore
|
|
|
|
open Microsoft.Extensions.Configuration
|
|
|
|
open Microsoft.Extensions.DependencyInjection
|
|
|
|
open Microsoft.Extensions.Logging
|
2019-02-23 19:59:32 +00:00
|
|
|
open Microsoft.FSharpLu.Json
|
2018-08-07 02:21:28 +00:00
|
|
|
open MyPrayerJournal
|
2019-02-23 19:59:32 +00:00
|
|
|
open Newtonsoft.Json
|
2018-08-07 02:21:28 +00:00
|
|
|
|
|
|
|
/// Set up the configuration for the app
|
|
|
|
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
|
|
|
|
|
|
|
|
/// Configure Kestrel from appsettings.json
|
|
|
|
let kestrel (ctx : WebHostBuilderContext) (opts : KestrelServerOptions) =
|
|
|
|
(ctx.Configuration.GetSection >> opts.Configure >> ignore) "Kestrel"
|
|
|
|
|
2019-02-23 19:59:32 +00:00
|
|
|
/// Custom settings for the JSON serializer (uses compact representation for options and DUs)
|
|
|
|
let jsonSettings =
|
|
|
|
let x = NewtonsoftJsonSerializer.DefaultSettings
|
|
|
|
x.Converters.Add (CompactUnionJsonConverter (true))
|
|
|
|
x.NullValueHandling <- NullValueHandling.Ignore
|
|
|
|
x.MissingMemberHandling <- MissingMemberHandling.Error
|
|
|
|
x.Formatting <- Formatting.Indented
|
|
|
|
x
|
|
|
|
|
2018-08-07 02:21:28 +00:00
|
|
|
/// Configure dependency injection
|
|
|
|
let services (sc : IServiceCollection) =
|
|
|
|
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.AddDbContext<AppDbContext>(fun opts -> opts.UseNpgsql(cfg.GetConnectionString "mpj") |> ignore)
|
2019-02-23 19:59:32 +00:00
|
|
|
.AddSingleton<IJsonSerializer>(NewtonsoftJsonSerializer jsonSettings)
|
2018-08-07 02:21:28 +00:00
|
|
|
|> ignore
|
|
|
|
|
|
|
|
/// Routes for the available URLs within myPrayerJournal
|
|
|
|
let webApp =
|
|
|
|
router Handlers.Error.notFound [
|
2018-08-15 02:01:21 +00:00
|
|
|
route "/" Handlers.Vue.app
|
2018-08-07 02:21:28 +00:00
|
|
|
subRoute "/api/" [
|
|
|
|
GET [
|
|
|
|
route "journal" Handlers.Journal.journal
|
|
|
|
subRoute "request" [
|
2018-08-19 00:32:48 +00:00
|
|
|
route "s/answered" Handlers.Request.answered
|
|
|
|
routef "/%s/full" Handlers.Request.getFull
|
|
|
|
routef "/%s/notes" Handlers.Request.getNotes
|
|
|
|
routef "/%s" Handlers.Request.get
|
|
|
|
]
|
|
|
|
]
|
|
|
|
PATCH [
|
|
|
|
subRoute "request" [
|
|
|
|
routef "/%s/recurrence" Handlers.Request.updateRecurrence
|
|
|
|
routef "/%s/show" Handlers.Request.show
|
|
|
|
routef "/%s/snooze" Handlers.Request.snooze
|
2018-08-07 02:21:28 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
POST [
|
|
|
|
subRoute "request" [
|
|
|
|
route "" Handlers.Request.add
|
|
|
|
routef "/%s/history" Handlers.Request.addHistory
|
|
|
|
routef "/%s/note" Handlers.Request.addNote
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
/// Configure the web application
|
|
|
|
let application (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()
|
|
|
|
.UseGiraffe webApp
|
|
|
|
|> ignore
|
|
|
|
|
|
|
|
/// Configure logging
|
|
|
|
let logging (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
|
|
|
|
|
|
|
|
|
|
|
|
module Program =
|
|
|
|
|
|
|
|
open System.IO
|
|
|
|
|
|
|
|
let exitCode = 0
|
|
|
|
|
|
|
|
let CreateWebHostBuilder _ =
|
|
|
|
let contentRoot = Directory.GetCurrentDirectory ()
|
|
|
|
WebHostBuilder()
|
|
|
|
.UseContentRoot(contentRoot)
|
|
|
|
.ConfigureAppConfiguration(Configure.configuration)
|
|
|
|
.UseKestrel(Configure.kestrel)
|
|
|
|
.UseWebRoot(Path.Combine (contentRoot, "wwwroot"))
|
|
|
|
.ConfigureServices(Configure.services)
|
|
|
|
.ConfigureLogging(Configure.logging)
|
|
|
|
.Configure(Action<IApplicationBuilder> Configure.application)
|
|
|
|
|
|
|
|
[<EntryPoint>]
|
|
|
|
let main args =
|
|
|
|
CreateWebHostBuilder(args).Build().Run()
|
|
|
|
exitCode
|