Skip to content

Commit

Permalink
Add abstraction for ASP.NET initialization, Add AutoOpen to several c…
Browse files Browse the repository at this point in the history
…ommon submodules
  • Loading branch information
plt-joey committed Sep 3, 2021
1 parent 2316912 commit 552ea0e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
63 changes: 61 additions & 2 deletions src/Dash.NET/DashApp.fs
Expand Up @@ -5,8 +5,17 @@ open Giraffe.ViewEngine
open Views
open Plotly.NET
open System
open System.Collections.Generic
open System.Runtime.InteropServices
open System.IO

open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Cors.Infrastructure
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection

[<assembly:AutoOpen("Dash.NET.DCC")>]
do ()

type DashApp =
{
Expand Down Expand Up @@ -153,3 +162,53 @@ type DashApp =
]
setStatusCode 404 >=> text "Not Found"
]

static member run (args: string []) (app: DashApp) =
//TODO: make this customizable
let errorHandler (ex : Exception) (logger : ILogger) =
logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
clearResponse >=> setStatusCode 500 >=> (app.Config.ErrorHandler ex)

let configureCors (builder : CorsPolicyBuilder) =
builder.WithOrigins(sprintf "http://%s:8080" app.Config.HostName)
.AllowAnyMethod()
.AllowAnyHeader()
|> ignore

let configureApp (appBuilder : IApplicationBuilder) =
let env = appBuilder.ApplicationServices.GetService<IWebHostEnvironment>()
(match env.EnvironmentName with
| "Development" -> appBuilder.UseDeveloperExceptionPage()
| _ -> appBuilder.UseGiraffeErrorHandler(errorHandler))
.UseHttpsRedirection()
.UseCors(configureCors)
.UseStaticFiles()
.UseGiraffe(DashApp.toHttpHandler app)

let configureServices (services : IServiceCollection) =
services.AddCors() |> ignore
services.AddGiraffe() |> ignore

let configureLogging (builder : ILoggingBuilder) =
builder.AddFilter(fun l -> l.Equals app.Config.LogLevel)
.AddConsole()
.AddDebug() |> ignore

// This folder has to be "<path-to-exe>/WebRoot" for the way generated component javascript injection currently works
let contentRoot = Reflection.Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
let webRoot = Path.Combine(contentRoot, "WebRoot")

Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
fun webHostBuilder ->
webHostBuilder
.UseContentRoot(contentRoot)
.UseWebRoot(webRoot)
.Configure(Action<IApplicationBuilder> configureApp)
.ConfigureServices(configureServices)
.ConfigureLogging(configureLogging)
|> ignore)
.Build()
.Run()

0
1 change: 1 addition & 0 deletions src/Dash.NET/DashComponents/ComponentBase.fs
Expand Up @@ -33,6 +33,7 @@ type DashComponentStyle() = inherit DynamicObj()

type DashComponentProps() = inherit DynamicObj()

[<AutoOpen>]
module ComponentPropTypes =

type InputType =
Expand Down
2 changes: 2 additions & 0 deletions src/Dash.NET/DashComponents/HTMLComponents/Html.fs
Expand Up @@ -7,13 +7,15 @@ open Plotly.NET
/// https://github.com/alfonsogarciacaro/Feliz.Engine
/// and are available under the Dash.NET.Html module.
[<AutoOpen>]
module Css =
type Style = StyleProperty of string * obj

/// Additional Css helpers are available under the Feliz namespace
let Css =
Feliz.CssEngine(fun k v -> StyleProperty(k, v))

[<AutoOpen>]
module Html =
open Css

Expand Down
34 changes: 33 additions & 1 deletion src/Dash.NET/DashConfig.fs
Expand Up @@ -3,6 +3,10 @@
open Newtonsoft.Json
open Plotly.NET

open System
open Giraffe
open Microsoft.Extensions.Logging

type HotReloadSettings =
{
[<JsonProperty("intervall")>]
Expand All @@ -14,6 +18,7 @@ type HotReloadSettings =
//todo: use standard casing and add serialization flags instead.
type DashConfig =
{
//Dash Specific
[<JsonProperty("url_base_pathname")>]
UrlBasePathname: string Option
[<JsonProperty("requests_pathname_prefix")>]
Expand All @@ -30,6 +35,14 @@ type DashConfig =
SuppressCallbackExceptions: bool
[<JsonProperty("update_title")>]
UpdateTitle: string

//Giraffe, Logging and ASP.NET specific
[<JsonIgnore()>]
HostName: string
[<JsonIgnore()>]
LogLevel: LogLevel
[<JsonIgnore()>]
ErrorHandler: Exception -> HttpHandler
}
static member create
urlBasePathname
Expand All @@ -40,6 +53,10 @@ type DashConfig =
showUndoRedo
suppressCallbackExceptions
updateTitle

hostName
logLevel
errorHandler
=
{
UrlBasePathname = urlBasePathname
Expand All @@ -51,9 +68,24 @@ type DashConfig =
SuppressCallbackExceptions = suppressCallbackExceptions
UpdateTitle = updateTitle
//hot_reload = {Intervall = 3000; MaxRetry = 8}

HostName = hostName
LogLevel = logLevel
ErrorHandler = errorHandler
}

static member initDefault() =
DashConfig.create None "/" false true true false false "Updating..."
DashConfig.create
None
"/"
false
true
true
false
false
"Updating..."
"localhost"
LogLevel.Debug
((fun ex -> ex.Message) >> text)

static member initDefaultWith(initializer: DashConfig -> DashConfig) = DashConfig.initDefault () |> initializer
1 change: 1 addition & 0 deletions src/Dash.NET/Operators.fs
@@ -1,5 +1,6 @@
namespace Dash.NET

[<AutoOpen>]
module Operators =

/// Shorthand for creation of a dependency (binding of the property of a component)
Expand Down

0 comments on commit 552ea0e

Please sign in to comment.