diff --git a/src/MyWebLog/MyWebLog.fsproj b/src/MyWebLog/MyWebLog.fsproj index e63cb8d..cad7156 100644 --- a/src/MyWebLog/MyWebLog.fsproj +++ b/src/MyWebLog/MyWebLog.fsproj @@ -1,4 +1,4 @@ - + Exe @@ -7,7 +7,6 @@ - diff --git a/src/MyWebLog/Program.fs b/src/MyWebLog/Program.fs index 029fbdb..e5cb948 100644 --- a/src/MyWebLog/Program.fs +++ b/src/MyWebLog/Program.fs @@ -1,7 +1,4 @@ -open Microsoft.AspNetCore.Hosting -open Microsoft.AspNetCore.Http -open Microsoft.Data.Sqlite -open Microsoft.Extensions.Configuration +open Microsoft.AspNetCore.Http open Microsoft.Extensions.Logging open MyWebLog @@ -30,6 +27,7 @@ type WebLogMiddleware(next: RequestDelegate, log: ILogger) = } +open System open Giraffe.Htmx /// Middleware to check redirects for the current web log @@ -37,7 +35,7 @@ type RedirectRuleMiddleware(next: RequestDelegate, _log: ILogger( - fun (opts: ForwardedHeadersOptions) -> - opts.ForwardedHeaders <- ForwardedHeaders.XForwardedFor ||| ForwardedHeaders.XForwardedProto) - let _ = - builder.Services - .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) - .AddCookie(fun opts -> - opts.ExpireTimeSpan <- TimeSpan.FromMinutes 60. - opts.SlidingExpiration <- true - opts.AccessDeniedPath <- "/forbidden") - let _ = builder.Services.AddLogging() - let _ = builder.Services.AddAuthorization() - let _ = builder.Services.AddAntiforgery() + let _ = builder.WebHost.UseStaticWebAssets() + builder.Services + .Configure( + fun (opts: ForwardedHeadersOptions) -> + opts.ForwardedHeaders <- ForwardedHeaders.XForwardedFor ||| ForwardedHeaders.XForwardedProto) + .AddLogging() + .AddAuthorization() + .AddAntiforgery() + .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) + .AddCookie(fun opts -> + opts.ExpireTimeSpan <- TimeSpan.FromMinutes 60. + opts.SlidingExpiration <- true + opts.AccessDeniedPath <- "/forbidden") + |> ignore let sp = builder.Services.BuildServiceProvider() let data = DataImplementation.get sp - let _ = builder.Services.AddSingleton data.Serializer + let _ = builder.Services.AddSingleton data.Serializer task { do! data.StartUp() @@ -178,37 +179,37 @@ let main args = match data with | :? RethinkDbData as rethink -> // A RethinkDB connection is designed to work as a singleton - let _ = builder.Services.AddSingleton data - let _ = - builder.Services.AddDistributedRethinkDBCache(fun opts -> + builder.Services + .AddSingleton(data) + .AddDistributedRethinkDBCache(fun opts -> opts.TableName <- "Session" opts.Connection <- rethink.Conn) - () + |> ignore | :? SQLiteData -> - // ADO.NET connections are designed to work as per-request instantiation - let cfg = sp.GetRequiredService() - let _ = builder.Services.AddScoped(fun sp -> Sqlite.Configuration.dbConn ()) - let _ = builder.Services.AddScoped() - // Use SQLite for caching as well + // ADO.NET connections are designed to work as per-request instantiation; use SQLite for caching as well + let cfg = sp.GetRequiredService() let cachePath = defaultArg (Option.ofObj (cfg.GetConnectionString "SQLiteCachePath")) "./data/session.db" - let _ = builder.Services.AddSqliteCache(fun o -> o.CachePath <- cachePath) - () + builder.Services + .AddScoped(fun sp -> Sqlite.Configuration.dbConn ()) + .AddScoped() + .AddSqliteCache(fun o -> o.CachePath <- cachePath) + |> ignore | :? PostgresData as postgres -> // ADO.NET Data Sources are designed to work as singletons - let _ = builder.Services.AddSingleton(Postgres.Configuration.dataSource ()) - let _ = builder.Services.AddSingleton postgres - let _ = - builder.Services.AddSingleton(fun _ -> - Postgres.DistributedCache() :> IDistributedCache) - () + builder.Services + .AddSingleton(Postgres.Configuration.dataSource ()) + .AddSingleton(postgres) + .AddSingleton(fun _ -> Postgres.DistributedCache() :> IDistributedCache) + |> ignore | _ -> () - let _ = builder.Services.AddSession(fun opts -> - opts.IdleTimeout <- TimeSpan.FromMinutes 60. - opts.Cookie.HttpOnly <- true - opts.Cookie.IsEssential <- true) - let _ = builder.Services.AddGiraffe() - let _ = builder.WebHost.UseStaticWebAssets() + builder.Services + .AddSession(fun opts -> + opts.IdleTimeout <- TimeSpan.FromMinutes 60. + opts.Cookie.HttpOnly <- true + opts.Cookie.IsEssential <- true) + .AddGiraffe() + |> ignore // Set up DotLiquid DotLiquidBespoke.register () @@ -243,14 +244,15 @@ let main args = |> isNotNull |> function true -> app.UseCanonicalDomains() |> ignore | false -> () - let _ = app.UseCookiePolicy(CookiePolicyOptions(MinimumSameSitePolicy = SameSiteMode.Strict)) - let _ = app.UseMiddleware() - let _ = app.UseMiddleware() - let _ = app.UseAuthentication() - let _ = app.UseRouting() - let _ = app.UseSession() - let _ = app.UseGiraffe Handlers.Routes.endpoints - let _ = app.UseStaticFiles() + app .UseCookiePolicy(CookiePolicyOptions(MinimumSameSitePolicy = SameSiteMode.Strict)) + .UseMiddleware() + .UseMiddleware() + .UseAuthentication() + .UseStaticFiles() + .UseRouting() + .UseSession() + .UseGiraffe Handlers.Routes.endpoints + |> ignore do! app.RunAsync() }