Skip to content

Commit

Permalink
#1410 add UseWebSharperScriptRedirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Jand42 committed Jun 12, 2024
1 parent ca7c9b4 commit 35c354b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace WebSharper.AspNetCore

open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Configuration
#nowarn "44" // Internal calls to obsolete methods

open System
Expand Down Expand Up @@ -83,4 +84,25 @@ type ApplicationBuilderExtensions =
ApplicationBuilderExtensions.UseWebSharper(this, fun builder ->
builder.UseRemoting(false) |> ignore
if not (isNull build) then build.Invoke(builder)
)
)

/// Use the WebSharper server side.
[<Extension>]
static member UseWebSharperScriptRedirect
(
this: IApplicationBuilder,
[<Optional>] redirectUrlRoot: string
) =

let redirectUrlRoot =
if isNull redirectUrlRoot then
let config = this.ApplicationServices.GetRequiredService<IConfiguration>().GetSection("websharper")
let fromConfig = config.Item("ScriptRedirectUrl")
if isNull fromConfig then
"http://localhost:5173"
else
fromConfig
else
redirectUrlRoot
this.Use(ScriptRedirect.Middleware redirectUrlRoot) |> ignore
this
42 changes: 42 additions & 0 deletions src/sitelets/WebSharper.AspNetCore/ScriptRedirectHandler.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// $begin{copyright}
//
// This file is part of WebSharper
//
// Copyright (c) 2008-2018 IntelliFactory
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// $end{copyright}
module WebSharper.AspNetCore.ScriptRedirect

open System
open System.IO
open System.Threading.Tasks
open System.Text.Json
open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Routing
open Microsoft.AspNetCore.Mvc
open Microsoft.AspNetCore.Mvc.Abstractions
open Microsoft.Extensions.Options
open WebSharper.Sitelets
open Microsoft.Extensions.Logging

let Middleware (redirectUrlRoot: string) =
Func<_,_,_>(fun (httpCtx: HttpContext) (next: Func<Task>) ->
if httpCtx.Request.Path.StartsWithSegments("/Scripts") || httpCtx.Request.Path.StartsWithSegments("/@vite") then
let proxyRequest = httpCtx.Request.Path.Value
httpCtx.Response.Redirect(redirectUrlRoot + proxyRequest)
Task.CompletedTask
else
next.Invoke()
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Compile Include="Context.fs" />
<Compile Include="RemotingHandler.fs" />
<Compile Include="SiteletsHandler.fs" />
<Compile Include="ScriptRedirectHandler.fs" />
<Compile Include="ContentExtensions.fs" />
<Compile Include="WebContextExtensions.fs" />
<Compile Include="ApplicationBuilderExtensions.fs" />
Expand Down
9 changes: 1 addition & 8 deletions tests/Web.FSharp/Startup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ type Startup () =
app.UseDeveloperExceptionPage() |> ignore

app.UseAuthentication()
.Use(fun context (next: RequestDelegate) ->
if context.Request.Path.StartsWithSegments("/Scripts") || context.Request.Path.StartsWithSegments("/@vite") then
let proxyRequest = context.Request.Path.Value
context.Response.Redirect($"http://localhost:5173{proxyRequest}")
Task.CompletedTask
else
next.Invoke(context)
)
.UseWebSharperScriptRedirect()
.UseStaticFiles()
.UseWebSharper()
.Run(fun context ->
Expand Down

0 comments on commit 35c354b

Please sign in to comment.