Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add access to webhostenvironment #327

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions src/Saturn/Application.fs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ module Application =
Task.Factory.StartNew(fun () -> tsk.Result)


/// IWebHostEnvironment is not accessible everywhere
let private mutable env : IWebHostEnvironment = null

/// Computation expression used to configure Saturn application.
/// Under the hood it's using ASP.NET application configurations interfaces such as `IWebHostBuilder`, `IServiceCollection`, `IApplicationBuilder` and others.
/// It aims to hide cumbersome ASP.NET application configuration and enable high level, declarative application configuration using feature toggles.
Expand All @@ -117,6 +120,11 @@ module Application =
clearResponse >=> Giraffe.HttpStatusCodeHandlers.ServerErrors.INTERNAL_ERROR ex.Message
{Router = None; EndpointRouter = None; ErrorHandler = Some errorHandler; Pipelines = []; Urls = []; MimeTypes = []; AppConfigs = []; HostConfigs = []; ServicesConfig = []; WebHostConfigs = []; CliArguments = None; CookiesAlreadyAdded = false; NoRouter = false; NoWebhost = false; Channels = [] }

static member WebHostEnvironment
with get() = env
and set(v) = env <- v


member __.Run(state: ApplicationState) : IHostBuilder =
// to build the app we have to separate our configurations and our pipelines.
// we can only call `Configure` once, so we have to apply our pipelines in the end
Expand All @@ -137,6 +145,31 @@ module Application =
Host.CreateDefaultBuilder(Option.toObj state.CliArguments)
|> List.foldBack (fun e acc -> e acc ) state.HostConfigs

if state.NoWebhost then
host
else
host.ConfigureWebHostDefaults(fun wbhst ->

// allow access to WebHostEnvironment to other CEs (e.g. configure services)
wbhst.ConfigureAppConfiguration(fun context config ->
ApplicationBuilder.WebHostEnvironment <- context.HostingEnvironment
)

let wbhst = wbhst |> List.foldBack (fun e acc -> e acc ) state.WebHostConfigs

let wbhst =
if not (state.Urls |> List.isEmpty) then
wbhst.UseUrls(state.Urls |> List.toArray)
else wbhst
let wbhst =
wbhst.Configure(fun ab ->
(ab, useParts)
||> Seq.fold (fun ab part -> part ab)
|> ignore
)
()
)

host.ConfigureServices(fun svcs ->
let services = svcs.AddGiraffe()
let services =
Expand Down Expand Up @@ -205,24 +238,6 @@ module Application =
|> ignore
)

if state.NoWebhost then
host
else
host.ConfigureWebHostDefaults(fun wbhst ->
let wbhst = wbhst |> List.foldBack (fun e acc -> e acc ) state.WebHostConfigs

let wbhst =
if not (state.Urls |> List.isEmpty) then
wbhst.UseUrls(state.Urls |> List.toArray)
else wbhst
let wbhst =
wbhst.Configure(fun ab ->
(ab, useParts)
||> Seq.fold (fun ab part -> part ab)
|> ignore
)
()
)

///Defines top-level router used for the application
[<CustomOperation("use_router")>]
Expand Down