-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSAFE.fs
More file actions
52 lines (43 loc) · 2.67 KB
/
SAFE.fs
File metadata and controls
52 lines (43 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace SAFE
open Giraffe
open Fable.Remoting.Server
open Fable.Remoting.Giraffe
[<AutoOpen>]
module Constants =
open Microsoft.Extensions.Hosting
/// Tests if the current environment is development based on the presence of the ASPNETCORE_ENVIRONMENT envionment variable.
let isDev =
System.Environment.GetEnvironmentVariable "ASPNETCORE_ENVIRONMENT" = Environments.Development
/// Pipeline wrappers to add different kinds of dependencies into the DI container.
module DI =
open Microsoft.Extensions.DependencyInjection
/// Shortcut for services.AddSingleton<'T>
let singleton<'T when 'T: not struct> (services: IServiceCollection) = services.AddSingleton<'T>()
/// Shortcut for services.AddTransient<'T>
let transient<'T when 'T: not struct> (services: IServiceCollection) = services.AddTransient<'T>()
/// Shortcut for services.AddScoped<'T>
let scoped<'T when 'T: not struct> (services: IServiceCollection) = services.AddScoped<'T>()
[<AutoOpen>]
module Extensions =
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Configuration
type HttpContext with
/// Gets the current IConfiguration service from the IOC container.
member this.Configuration = this.GetService<IConfiguration>()
[<RequireQualifiedAccess>]
type Api =
/// <summary>Quickly creates a Fable Remoting API as an HTTP Handler for Giraffe. For reference documentation, see https://zaid-ajaj.github.io/Fable.Remoting/#/server-setup/saturn.</summary>
/// <param name="api">The Fable Remoting API. It must take in an `HTTPContext` as the only argument.</param>
/// <param name="routeBuilder">A function which takes the name of the API and the name of the method and returns the route to the server. Defaults to `/api/{api name}/{method name}` e.g. `/api/ITodoApi/GetTodos`.</param>
/// <param name="errorHandler">A function which takes an `Exception` and a `RouteInfo` and returns a Giraffe Handler. Defaults to `Ignore` the exception i.e. it will not be cascaded.</param>
/// <param name="customOptions">A function which takes the current Fable Remoting options and returns a new set of options. Useful for applying any custom options.</param>
static member make(api, ?routeBuilder, ?errorHandler, ?customOptions) =
let routeBuilder = defaultArg routeBuilder (sprintf "/api/%s/%s")
let errorHandler = defaultArg errorHandler (fun _ _ -> Ignore)
let customOptions = defaultArg customOptions id
Remoting.createApi ()
|> Remoting.withRouteBuilder routeBuilder
|> Remoting.withErrorHandler errorHandler
|> customOptions
|> Remoting.fromContext api
|> Remoting.buildHttpHandler