Infra changes
Moved projects around; API still builds successfully
This commit is contained in:
156
src/MyPrayerJournal.Api/Data.fs
Normal file
156
src/MyPrayerJournal.Api/Data.fs
Normal file
@@ -0,0 +1,156 @@
|
||||
namespace MyPrayerJournal
|
||||
|
||||
open FSharp.Control.Tasks.V2.ContextInsensitive
|
||||
open Microsoft.EntityFrameworkCore
|
||||
open Microsoft.FSharpLu
|
||||
|
||||
/// Entity Framework configuration for myPrayerJournal
|
||||
module internal EFConfig =
|
||||
|
||||
open FSharp.EFCore.OptionConverter
|
||||
open System.Collections.Generic
|
||||
|
||||
/// Configure EF properties for all entity types
|
||||
let configure (mb : ModelBuilder) =
|
||||
mb.Entity<History> (
|
||||
fun m ->
|
||||
m.ToTable "history" |> ignore
|
||||
m.HasKey ("requestId", "asOf") |> ignore
|
||||
m.Property(fun e -> e.requestId).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.asOf).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.status).IsRequired() |> ignore
|
||||
m.Property(fun e -> e.text) |> ignore)
|
||||
|> ignore
|
||||
mb.Model.FindEntityType(typeof<History>).FindProperty("text").SetValueConverter (OptionConverter<string> ())
|
||||
|
||||
mb.Entity<Note> (
|
||||
fun m ->
|
||||
m.ToTable "note" |> ignore
|
||||
m.HasKey ("requestId", "asOf") |> ignore
|
||||
m.Property(fun e -> e.requestId).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.asOf).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.notes).IsRequired () |> ignore)
|
||||
|> ignore
|
||||
|
||||
mb.Entity<Request> (
|
||||
fun m ->
|
||||
m.ToTable "request" |> ignore
|
||||
m.HasKey(fun e -> e.requestId :> obj) |> ignore
|
||||
m.Property(fun e -> e.requestId).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.enteredOn).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.userId).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.snoozedUntil).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.showAfter).IsRequired () |> ignore
|
||||
m.Property(fun e -> e.recurType).IsRequired() |> ignore
|
||||
m.Property(fun e -> e.recurCount).IsRequired() |> ignore
|
||||
m.HasMany(fun e -> e.history :> IEnumerable<History>)
|
||||
.WithOne()
|
||||
.HasForeignKey(fun e -> e.requestId :> obj)
|
||||
|> ignore
|
||||
m.HasMany(fun e -> e.notes :> IEnumerable<Note>)
|
||||
.WithOne()
|
||||
.HasForeignKey(fun e -> e.requestId :> obj)
|
||||
|> ignore)
|
||||
|> ignore
|
||||
|
||||
mb.Query<JournalRequest> (
|
||||
fun m ->
|
||||
m.ToView "journal" |> ignore
|
||||
m.Ignore(fun e -> e.history :> obj) |> ignore
|
||||
m.Ignore(fun e -> e.notes :> obj) |> ignore)
|
||||
|> ignore
|
||||
|
||||
|
||||
open System.Linq
|
||||
|
||||
/// Data context
|
||||
type AppDbContext (opts : DbContextOptions<AppDbContext>) =
|
||||
inherit DbContext (opts)
|
||||
|
||||
[<DefaultValue>]
|
||||
val mutable private history : DbSet<History>
|
||||
[<DefaultValue>]
|
||||
val mutable private notes : DbSet<Note>
|
||||
[<DefaultValue>]
|
||||
val mutable private requests : DbSet<Request>
|
||||
[<DefaultValue>]
|
||||
val mutable private journal : DbQuery<JournalRequest>
|
||||
|
||||
member this.History
|
||||
with get () = this.history
|
||||
and set v = this.history <- v
|
||||
member this.Notes
|
||||
with get () = this.notes
|
||||
and set v = this.notes <- v
|
||||
member this.Requests
|
||||
with get () = this.requests
|
||||
and set v = this.requests <- v
|
||||
member this.Journal
|
||||
with get () = this.journal
|
||||
and set v = this.journal <- v
|
||||
|
||||
override __.OnModelCreating (mb : ModelBuilder) =
|
||||
base.OnModelCreating mb
|
||||
EFConfig.configure mb
|
||||
|
||||
/// Register a disconnected entity with the context, having the given state
|
||||
member private this.RegisterAs<'TEntity when 'TEntity : not struct> state e =
|
||||
this.Entry<'TEntity>(e).State <- state
|
||||
|
||||
/// Add an entity instance to the context
|
||||
member this.AddEntry e =
|
||||
this.RegisterAs EntityState.Added e
|
||||
|
||||
/// Update the entity instance's values
|
||||
member this.UpdateEntry e =
|
||||
this.RegisterAs EntityState.Modified e
|
||||
|
||||
/// Retrieve all answered requests for the given user
|
||||
member this.AnsweredRequests userId : JournalRequest seq =
|
||||
upcast this.Journal
|
||||
.Where(fun r -> r.userId = userId && r.lastStatus = "Answered")
|
||||
.OrderByDescending(fun r -> r.asOf)
|
||||
|
||||
/// Retrieve the user's current journal
|
||||
member this.JournalByUserId userId : JournalRequest seq =
|
||||
upcast this.Journal
|
||||
.Where(fun r -> r.userId = userId && r.lastStatus <> "Answered")
|
||||
.OrderBy(fun r -> r.showAfter)
|
||||
|
||||
/// Retrieve a request by its ID and user ID
|
||||
member this.TryRequestById reqId userId =
|
||||
task {
|
||||
let! req = this.Requests.AsNoTracking().FirstOrDefaultAsync(fun r -> r.requestId = reqId && r.userId = userId)
|
||||
return Option.fromObject req
|
||||
}
|
||||
|
||||
/// Retrieve notes for a request by its ID and user ID
|
||||
member this.NotesById reqId userId =
|
||||
task {
|
||||
match! this.TryRequestById reqId userId with
|
||||
| Some _ -> return this.Notes.AsNoTracking().Where(fun n -> n.requestId = reqId) |> List.ofSeq
|
||||
| None -> return []
|
||||
}
|
||||
|
||||
/// Retrieve a journal request by its ID and user ID
|
||||
member this.TryJournalById reqId userId =
|
||||
task {
|
||||
let! req = this.Journal.FirstOrDefaultAsync(fun r -> r.requestId = reqId && r.userId = userId)
|
||||
return Option.fromObject req
|
||||
}
|
||||
|
||||
/// Retrieve a request, including its history and notes, by its ID and user ID
|
||||
member this.TryFullRequestById requestId userId =
|
||||
task {
|
||||
match! this.TryJournalById requestId userId with
|
||||
| Some req ->
|
||||
let! fullReq =
|
||||
this.Requests.AsNoTracking()
|
||||
.Include(fun r -> r.history)
|
||||
.Include(fun r -> r.notes)
|
||||
.FirstOrDefaultAsync(fun r -> r.requestId = requestId && r.userId = userId)
|
||||
match Option.fromObject fullReq with
|
||||
| Some _ -> return Some { req with history = List.ofSeq fullReq.history; notes = List.ofSeq fullReq.notes }
|
||||
| None -> return None
|
||||
| None -> return None
|
||||
}
|
||||
321
src/MyPrayerJournal.Api/Handlers.fs
Normal file
321
src/MyPrayerJournal.Api/Handlers.fs
Normal file
@@ -0,0 +1,321 @@
|
||||
/// HTTP handlers for the myPrayerJournal API
|
||||
[<RequireQualifiedAccess>]
|
||||
module MyPrayerJournal.Api.Handlers
|
||||
|
||||
open FSharp.Control.Tasks.V2.ContextInsensitive
|
||||
open Giraffe
|
||||
open MyPrayerJournal
|
||||
open System
|
||||
|
||||
/// Handler to return Vue files
|
||||
module Vue =
|
||||
|
||||
/// The application index page
|
||||
let app : HttpHandler = htmlFile "wwwroot/index.html"
|
||||
|
||||
|
||||
/// Handlers for error conditions
|
||||
module Error =
|
||||
|
||||
open Microsoft.Extensions.Logging
|
||||
|
||||
/// Handle errors
|
||||
let error (ex : Exception) (log : ILogger) =
|
||||
log.LogError (EventId(), ex, "An unhandled exception has occurred while executing the request.")
|
||||
clearResponse >=> setStatusCode 500 >=> json ex.Message
|
||||
|
||||
/// Handle 404s from the API, sending known URL paths to the Vue app so that they can be handled there
|
||||
let notFound : HttpHandler =
|
||||
fun next ctx ->
|
||||
[ "/journal"; "/legal"; "/request"; "/user" ]
|
||||
|> List.filter ctx.Request.Path.Value.StartsWith
|
||||
|> List.length
|
||||
|> function
|
||||
| 0 -> (setStatusCode 404 >=> json ([ "error", "not found" ] |> dict)) next ctx
|
||||
| _ -> Vue.app next ctx
|
||||
|
||||
|
||||
/// Handler helpers
|
||||
[<AutoOpen>]
|
||||
module private Helpers =
|
||||
|
||||
open Microsoft.AspNetCore.Http
|
||||
open System.Threading.Tasks
|
||||
open System.Security.Claims
|
||||
|
||||
/// Get the database context from DI
|
||||
let db (ctx : HttpContext) =
|
||||
ctx.GetService<AppDbContext> ()
|
||||
|
||||
/// Get the user's "sub" claim
|
||||
let user (ctx : HttpContext) =
|
||||
ctx.User.Claims |> Seq.tryFind (fun u -> u.Type = ClaimTypes.NameIdentifier)
|
||||
|
||||
/// Get the current user's ID
|
||||
// NOTE: this may raise if you don't run the request through the authorize handler first
|
||||
let userId ctx =
|
||||
((user >> Option.get) ctx).Value
|
||||
|
||||
/// Return a 201 CREATED response
|
||||
let created next ctx =
|
||||
setStatusCode 201 next ctx
|
||||
|
||||
/// The "now" time in JavaScript
|
||||
let jsNow () =
|
||||
DateTime.UtcNow.Subtract(DateTime (1970, 1, 1, 0, 0, 0)).TotalSeconds |> int64 |> (*) 1000L
|
||||
|
||||
/// Handler to return a 403 Not Authorized reponse
|
||||
let notAuthorized : HttpHandler =
|
||||
setStatusCode 403 >=> fun _ _ -> Task.FromResult<HttpContext option> None
|
||||
|
||||
/// Handler to require authorization
|
||||
let authorize : HttpHandler =
|
||||
fun next ctx -> match user ctx with Some _ -> next ctx | None -> notAuthorized next ctx
|
||||
|
||||
/// Flip JSON result so we can pipe into it
|
||||
let asJson<'T> next ctx (o : 'T) =
|
||||
json o next ctx
|
||||
|
||||
|
||||
/// Strongly-typed models for post requests
|
||||
module Models =
|
||||
|
||||
/// A history entry addition (AKA request update)
|
||||
[<CLIMutable>]
|
||||
type HistoryEntry =
|
||||
{ /// The status of the history update
|
||||
status : string
|
||||
/// The text of the update
|
||||
updateText : string
|
||||
}
|
||||
|
||||
/// An additional note
|
||||
[<CLIMutable>]
|
||||
type NoteEntry =
|
||||
{ /// The notes being added
|
||||
notes : string
|
||||
}
|
||||
|
||||
/// Recurrence update
|
||||
[<CLIMutable>]
|
||||
type Recurrence =
|
||||
{ /// The recurrence type
|
||||
recurType : string
|
||||
/// The recurrence cound
|
||||
recurCount : int16
|
||||
}
|
||||
|
||||
/// A prayer request
|
||||
[<CLIMutable>]
|
||||
type Request =
|
||||
{ /// The text of the request
|
||||
requestText : string
|
||||
/// The recurrence type
|
||||
recurType : string
|
||||
/// The recurrence count
|
||||
recurCount : int16
|
||||
}
|
||||
|
||||
/// Reset the "showAfter" property on a request
|
||||
[<CLIMutable>]
|
||||
type Show =
|
||||
{ /// The time after which the request should appear
|
||||
showAfter : int64
|
||||
}
|
||||
|
||||
/// The time until which a request should not appear in the journal
|
||||
[<CLIMutable>]
|
||||
type SnoozeUntil =
|
||||
{ /// The time at which the request should reappear
|
||||
until : int64
|
||||
}
|
||||
|
||||
|
||||
/// /api/journal URLs
|
||||
module Journal =
|
||||
|
||||
/// GET /api/journal
|
||||
let journal : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
userId ctx
|
||||
|> (db ctx).JournalByUserId
|
||||
|> asJson next ctx
|
||||
|
||||
|
||||
/// /api/request URLs
|
||||
module Request =
|
||||
|
||||
open NCuid
|
||||
|
||||
/// Ticks per recurrence
|
||||
let private recurrence =
|
||||
[ "immediate", 0L
|
||||
"hours", 3600000L
|
||||
"days", 86400000L
|
||||
"weeks", 604800000L
|
||||
]
|
||||
|> Map.ofList
|
||||
|
||||
/// POST /api/request
|
||||
let add : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
let! r = ctx.BindJsonAsync<Models.Request> ()
|
||||
let db = db ctx
|
||||
let reqId = Cuid.Generate ()
|
||||
let usrId = userId ctx
|
||||
let now = jsNow ()
|
||||
{ Request.empty with
|
||||
requestId = reqId
|
||||
userId = usrId
|
||||
enteredOn = now
|
||||
showAfter = now
|
||||
recurType = r.recurType
|
||||
recurCount = r.recurCount
|
||||
}
|
||||
|> db.AddEntry
|
||||
{ History.empty with
|
||||
requestId = reqId
|
||||
asOf = now
|
||||
status = "Created"
|
||||
text = Some r.requestText
|
||||
}
|
||||
|> db.AddEntry
|
||||
let! _ = db.SaveChangesAsync ()
|
||||
match! db.TryJournalById reqId usrId with
|
||||
| Some req -> return! (setStatusCode 201 >=> json req) next ctx
|
||||
| None -> return! Error.notFound next ctx
|
||||
}
|
||||
|
||||
/// POST /api/request/[req-id]/history
|
||||
let addHistory reqId : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
let db = db ctx
|
||||
match! db.TryRequestById reqId (userId ctx) with
|
||||
| Some req ->
|
||||
let! hist = ctx.BindJsonAsync<Models.HistoryEntry> ()
|
||||
let now = jsNow ()
|
||||
{ History.empty with
|
||||
requestId = reqId
|
||||
asOf = now
|
||||
status = hist.status
|
||||
text = match hist.updateText with null | "" -> None | x -> Some x
|
||||
}
|
||||
|> db.AddEntry
|
||||
match hist.status with
|
||||
| "Prayed" ->
|
||||
db.UpdateEntry { req with showAfter = now + (recurrence.[req.recurType] * int64 req.recurCount) }
|
||||
| _ -> ()
|
||||
let! _ = db.SaveChangesAsync ()
|
||||
return! created next ctx
|
||||
| None -> return! Error.notFound next ctx
|
||||
}
|
||||
|
||||
/// POST /api/request/[req-id]/note
|
||||
let addNote reqId : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
let db = db ctx
|
||||
match! db.TryRequestById reqId (userId ctx) with
|
||||
| Some _ ->
|
||||
let! notes = ctx.BindJsonAsync<Models.NoteEntry> ()
|
||||
{ Note.empty with
|
||||
requestId = reqId
|
||||
asOf = jsNow ()
|
||||
notes = notes.notes
|
||||
}
|
||||
|> db.AddEntry
|
||||
let! _ = db.SaveChangesAsync ()
|
||||
return! created next ctx
|
||||
| None -> return! Error.notFound next ctx
|
||||
}
|
||||
|
||||
/// GET /api/requests/answered
|
||||
let answered : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
userId ctx
|
||||
|> (db ctx).AnsweredRequests
|
||||
|> asJson next ctx
|
||||
|
||||
/// GET /api/request/[req-id]
|
||||
let get reqId : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
match! (db ctx).TryJournalById reqId (userId ctx) with
|
||||
| Some req -> return! json req next ctx
|
||||
| None -> return! Error.notFound next ctx
|
||||
}
|
||||
|
||||
/// GET /api/request/[req-id]/full
|
||||
let getFull reqId : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
match! (db ctx).TryFullRequestById reqId (userId ctx) with
|
||||
| Some req -> return! json req next ctx
|
||||
| None -> return! Error.notFound next ctx
|
||||
}
|
||||
|
||||
/// GET /api/request/[req-id]/notes
|
||||
let getNotes reqId : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
let! notes = (db ctx).NotesById reqId (userId ctx)
|
||||
return! json notes next ctx
|
||||
}
|
||||
|
||||
/// PATCH /api/request/[req-id]/show
|
||||
let show reqId : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
let db = db ctx
|
||||
match! db.TryRequestById reqId (userId ctx) with
|
||||
| Some req ->
|
||||
let! show = ctx.BindJsonAsync<Models.Show> ()
|
||||
{ req with showAfter = show.showAfter }
|
||||
|> db.UpdateEntry
|
||||
let! _ = db.SaveChangesAsync ()
|
||||
return! setStatusCode 204 next ctx
|
||||
| None -> return! Error.notFound next ctx
|
||||
}
|
||||
|
||||
/// PATCH /api/request/[req-id]/snooze
|
||||
let snooze reqId : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
let db = db ctx
|
||||
match! db.TryRequestById reqId (userId ctx) with
|
||||
| Some req ->
|
||||
let! until = ctx.BindJsonAsync<Models.SnoozeUntil> ()
|
||||
{ req with snoozedUntil = until.until; showAfter = until.until }
|
||||
|> db.UpdateEntry
|
||||
let! _ = db.SaveChangesAsync ()
|
||||
return! setStatusCode 204 next ctx
|
||||
| None -> return! Error.notFound next ctx
|
||||
}
|
||||
|
||||
/// PATCH /api/request/[req-id]/recurrence
|
||||
let updateRecurrence reqId : HttpHandler =
|
||||
authorize
|
||||
>=> fun next ctx ->
|
||||
task {
|
||||
let db = db ctx
|
||||
match! db.TryRequestById reqId (userId ctx) with
|
||||
| Some req ->
|
||||
let! recur = ctx.BindJsonAsync<Models.Recurrence> ()
|
||||
{ req with recurType = recur.recurType; recurCount = recur.recurCount }
|
||||
|> db.UpdateEntry
|
||||
let! _ = db.SaveChangesAsync ()
|
||||
return! setStatusCode 204 next ctx
|
||||
| None -> return! Error.notFound next ctx
|
||||
}
|
||||
37
src/MyPrayerJournal.Api/MyPrayerJournal.Api.fsproj
Normal file
37
src/MyPrayerJournal.Api/MyPrayerJournal.Api.fsproj
Normal file
@@ -0,0 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Data.fs" />
|
||||
<Compile Include="Handlers.fs" />
|
||||
<Compile Include="Program.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FSharp.EFCore.OptionConverter" Version="1.0.0" />
|
||||
<PackageReference Include="Giraffe" Version="3.6.0" />
|
||||
<PackageReference Include="Giraffe.TokenRouter" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||
<PackageReference Include="Microsoft.FSharpLu" Version="0.10.29" />
|
||||
<PackageReference Include="Microsoft.FSharpLu.Json" Version="0.10.29" />
|
||||
<PackageReference Include="NCuid.NetCore" Version="1.0.1" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.0" />
|
||||
<PackageReference Include="TaskBuilder.fs" Version="2.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="FSharp.Core" Version="4.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="wwwroot\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MyPrayerJournal.Domain\MyPrayerJournal.Domain.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
139
src/MyPrayerJournal.Api/Program.fs
Normal file
139
src/MyPrayerJournal.Api/Program.fs
Normal file
@@ -0,0 +1,139 @@
|
||||
namespace MyPrayerJournal.Api
|
||||
|
||||
open Microsoft.AspNetCore.Builder
|
||||
open Microsoft.AspNetCore.Hosting
|
||||
open System
|
||||
|
||||
/// Configuration functions for the application
|
||||
module Configure =
|
||||
|
||||
open Giraffe
|
||||
open Giraffe.Serialization
|
||||
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
|
||||
open Microsoft.FSharpLu.Json
|
||||
open MyPrayerJournal
|
||||
open Newtonsoft.Json
|
||||
|
||||
/// 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"
|
||||
|
||||
/// 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
|
||||
|
||||
/// 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)
|
||||
.AddSingleton<IJsonSerializer>(NewtonsoftJsonSerializer jsonSettings)
|
||||
|> ignore
|
||||
|
||||
/// Routes for the available URLs within myPrayerJournal
|
||||
let webApp =
|
||||
router Handlers.Error.notFound [
|
||||
route "/" Handlers.Vue.app
|
||||
subRoute "/api/" [
|
||||
GET [
|
||||
route "journal" Handlers.Journal.journal
|
||||
subRoute "request" [
|
||||
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
|
||||
]
|
||||
]
|
||||
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
|
||||
27
src/MyPrayerJournal.Api/Properties/launchSettings.json
Normal file
27
src/MyPrayerJournal.Api/Properties/launchSettings.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:61905",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"MyPrayerJournal.Api": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"applicationUrl": "http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/MyPrayerJournal.Api/appsettings.json
Normal file
9
src/MyPrayerJournal.Api/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Kestrel": {
|
||||
"EndPoints": {
|
||||
"Http": {
|
||||
"Url": "http://localhost:3000"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user