Skip to content

Commit

Permalink
Allow minimal=false server=false using HttpClient instead of remoting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmil committed Apr 11, 2021
1 parent ba42fcd commit a39b523
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 22 deletions.
32 changes: 20 additions & 12 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@ let buildOutputDir = slnDir </> "build"
let packageOutputFile o = buildOutputDir </> sprintf "Bolero.Templates.%s.nupkg" (version o)
let variantsToTest =
[
"NoRazor.Reload", "--server --hotreload --minimal=false --razor=false"
"NoRazor.NoReload", "--server --hotreload=false --minimal=false --razor=false"
"NoRazor.NoHtml", "--server --html=false --minimal=false --razor=false"
"NoRazor.Reload.Pwa", "--server --hotreload --minimal=false --razor=false --pwa"
"NoRazor.NoReload.Pwa", "--server --hotreload=false --minimal=false --razor=false --pwa"
"NoRazor.NoHtml.Pwa", "--server --html=false --minimal=false --razor=false --pwa"
"Razor.Reload", "--server --hotreload --minimal=false --razor"
"Razor.NoReload", "--server --hotreload=false --minimal=false --razor"
"Razor.NoHtml", "--server --html=false --minimal=false --razor"
"Razor.Reload.Pwa", "--server --hotreload --minimal=false --razor --pwa"
"Razor.NoReload.Pwa", "--server --hotreload=false --minimal=false --razor --pwa"
"Razor.NoHtml.Pwa", "--server --html=false --minimal=false --razor --pwa"
"NoRazor.Server.Reload", "--server --hotreload --minimal=false --razor=false"
"NoRazor.Server.NoReload", "--server --hotreload=false --minimal=false --razor=false"
"NoRazor.Server.NoHtml", "--server --html=false --minimal=false --razor=false"
"NoRazor.Server.Reload.Pwa", "--server --hotreload --minimal=false --razor=false --pwa"
"NoRazor.Server.NoReload.Pwa", "--server --hotreload=false --minimal=false --razor=false --pwa"
"NoRazor.Server.NoHtml.Pwa", "--server --html=false --minimal=false --razor=false --pwa"
"NoRazor.NoServer.NoReload", "--server=false --hotreload=false --minimal=false --razor=false"
"NoRazor.NoServer.NoHtml", "--server=false --html=false --minimal=false --razor=false"
"NoRazor.NoServer.NoReload.Pwa", "--server=false --hotreload=false --minimal=false --razor=false --pwa"
"NoRazor.NoServer.NoHtml.Pwa", "--server=false --html=false --minimal=false --razor=false --pwa"
"Razor.Server.Reload", "--server --hotreload --minimal=false --razor"
"Razor.Server.NoReload", "--server --hotreload=false --minimal=false --razor"
"Razor.Server.NoHtml", "--server --html=false --minimal=false --razor"
"Razor.Server.Reload.Pwa", "--server --hotreload --minimal=false --razor --pwa"
"Razor.Server.NoReload.Pwa", "--server --hotreload=false --minimal=false --razor --pwa"
"Razor.Server.NoHtml.Pwa", "--server --html=false --minimal=false --razor --pwa"
"Razor.NoServer.NoReload", "--server --hotreload=false --minimal=false --razor"
"Razor.NoServer.NoHtml", "--server --html=false --minimal=false --razor"
"Razor.NoServer.NoReload.Pwa", "--server --hotreload=false --minimal=false --razor --pwa"
"Razor.NoServer.NoHtml.Pwa", "--server --html=false --minimal=false --razor --pwa"
"Minimal.Server.Reload", "--server --hotreload --minimal --razor=false"
"Minimal.Server.NoReload", "--server --hotreload=false --minimal --razor=false"
"Minimal.NoServer.NoReload", "--server=false --hotreload=false --minimal"
Expand Down
10 changes: 8 additions & 2 deletions content/application/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"server": {
"type": "parameter",
"isRequired": false,
"description": "Include an ASP.NET Core server-side host (ignored if minimal=false)",
"description": "Include an ASP.NET Core server-side host",
"dataType": "bool",
"defaultValue": "true"
},
Expand Down Expand Up @@ -71,7 +71,7 @@
"server_actual": {
"type": "computed",
"dataType": "bool",
"value": "(server || !minimal)"
"value": "(server)"
},
"hotreload_actual": {
"type": "computed",
Expand All @@ -93,6 +93,12 @@
"nuget.config"
],
"modifiers": [
{
"condition": "(server_actual)",
"rename": {
"src/Bolero.Template.Client/wwwroot/books.json": "src/Bolero.Template.Server/data/books.json"
}
},
{
"condition": "(!server_actual)",
"exclude": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<PackageReference Include="Bolero.HotReload" Version="0.*" />
<!-- //#endif -->
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.*" />
<!-- //#if (server_actual) -->
<PackageReference Include="System.Net.Http.Json" Version="5.0.*" />
<!-- //#endif -->
</ItemGroup>
<!-- //#if (pwa) -->
<ItemGroup>
Expand Down
80 changes: 72 additions & 8 deletions content/application/src/Bolero.Template.Client/Main.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
module Bolero.Template.Client.Main

open System
//#if (!server_actual)
open System.Net.Http
open System.Net.Http.Json
open Microsoft.AspNetCore.Components
//#endif
open Elmish
open Bolero
open Bolero.Html
//#if (server_actual)
open Bolero.Remoting
open Bolero.Remoting.Client
//#endif
//#if (hotreload_actual)
open Bolero.Templating.Client
//#endif
Expand All @@ -23,10 +30,12 @@ type Model =
counter: int
books: Book[] option
error: string option
//#if (server_actual)
username: string
password: string
signedInAs: option<string>
signInFailed: bool
//#endif
}

and Book =
Expand All @@ -43,12 +52,15 @@ let initModel =
counter = 0
books = None
error = None
//#if (server_actual)
username = ""
password = ""
signedInAs = None
signInFailed = false
//#endif
}

//#if (server_actual)
/// Remote service definition.
type BookService =
{
Expand All @@ -73,6 +85,7 @@ type BookService =

interface IRemoteService with
member this.BasePath = "/books"
//#endif

/// The Elmish application's update messages.
type Message =
Expand All @@ -82,6 +95,7 @@ type Message =
| SetCounter of int
| GetBooks
| GotBooks of Book[]
//#if (server_actual)
| SetUsername of string
| SetPassword of string
| GetSignedInAs
Expand All @@ -90,13 +104,18 @@ type Message =
| RecvSignIn of option<string>
| SendSignOut
| RecvSignOut
//#endif
| Error of exn
| ClearError

//#if (server_actual)
let update remote message model =
let onSignIn = function
| Some _ -> Cmd.ofMsg GetBooks
| None -> Cmd.none
//#else
let update (http: HttpClient) message model =
//#endif
match message with
| SetPage page ->
{ model with page = page }, Cmd.none
Expand All @@ -109,11 +128,17 @@ let update remote message model =
{ model with counter = value }, Cmd.none

| GetBooks ->
//#if (server_actual)
let cmd = Cmd.OfAsync.either remote.getBooks () GotBooks Error
//#else
let getBooks() = http.GetFromJsonAsync<Book[]>("/books.json")
let cmd = Cmd.OfTask.either getBooks () GotBooks Error
//#endif
{ model with books = None }, cmd
| GotBooks books ->
{ model with books = Some books }, Cmd.none

//#if (server_actual)
| SetUsername s ->
{ model with username = s }, Cmd.none
| SetPassword s ->
Expand All @@ -133,6 +158,7 @@ let update remote message model =

| Error RemoteUnauthorizedException ->
{ model with error = Some "You have been logged out."; signedInAs = None }, Cmd.none
//#endif
| Error exn ->
{ model with error = Some exn.Message }, Cmd.none
| ClearError ->
Expand All @@ -154,11 +180,17 @@ let counterPage model dispatch =
.Value(model.counter, fun v -> dispatch (SetCounter v))
.Elt()

//#if (server_actual)
let dataPage model (username: string) dispatch =
//#else
let dataPage model dispatch =
//#endif
Main.Data()
.Reload(fun _ -> dispatch GetBooks)
//#if (server_actual)
.Username(username)
.SignOut(fun _ -> dispatch SendSignOut)
//#endif
.Rows(cond model.books <| function
| None ->
Main.EmptyData().Elt()
Expand All @@ -172,6 +204,7 @@ let dataPage model (username: string) dispatch =
])
.Elt()

//#if (server_actual)
let signInPage model dispatch =
Main.SignIn()
.Username(model.username, fun s -> dispatch (SetUsername s))
Expand All @@ -187,6 +220,7 @@ let signInPage model dispatch =
.Elt()
)
.Elt()
//#endif

let menuItem (model: Model) (page: Page) (text: string) =
Main.MenuItem()
Expand All @@ -207,9 +241,13 @@ let view model dispatch =
| Home -> homePage model dispatch
| Counter -> counterPage model dispatch
| Data ->
//#if (server_actual)
cond model.signedInAs <| function
| Some username -> dataPage model username dispatch
| None -> signInPage model dispatch
//#else
dataPage model dispatch
//#endif
)
.Error(
cond model.error <| function
Expand Down Expand Up @@ -256,12 +294,16 @@ let homePage model dispatch =
li [] [
text "The "
a [router.HRef Data] [text "Download data"]
//#if (server_actual)
text " page demonstrates the use of "
a [
attr.target "_blank"
attr.href "https://fsbolero.github.io/docs/Remoting"
] [text "remote functions"]
text "."
//#else
text " page demonstrates the use of HTTP requests to the server."
//#endif
]
p [] [text "Enjoy writing awesome apps!"]
]
Expand All @@ -288,7 +330,11 @@ let counterPage model dispatch =
]
]

//#if (server_actual)
let dataPage model (username: string) dispatch =
//#else
let dataPage model dispatch =
//#endif
concat [
h1 [attr.``class`` "title"] [
text "Download data "
Expand All @@ -297,13 +343,15 @@ let dataPage model (username: string) dispatch =
on.click (fun _ -> dispatch GetBooks)
] [text "Reload"]
]
//#if (server_actual)
p [] [
textf "Signed in as %s. " username
button [
attr.``class`` "button"
on.click (fun _ -> dispatch SendSignOut)
] [text "Sign out"]
]
//#endif
table [attr.``class`` "table is-fullwidth"] [
thead [] [
tr [] [
Expand Down Expand Up @@ -331,6 +379,15 @@ let dataPage model (username: string) dispatch =
]
]

let errorNotification errorText closeCallback =
div [attr.``class`` "notification is-warning"] [
cond closeCallback <| function
| None -> empty
| Some closeCallback -> button [attr.``class`` "delete"; on.click closeCallback] []
text errorText
]

//#if (server_actual)
let field content = div [attr.``class`` "field"] content
let control content = div [attr.``class`` "control"] content

Expand All @@ -340,14 +397,6 @@ let inputField fieldLabel inputAttrs =
control [input (attr.``class`` "input" :: inputAttrs)]
]

let errorNotification errorText closeCallback =
div [attr.``class`` "notification is-warning"] [
cond closeCallback <| function
| None -> empty
| Some closeCallback -> button [attr.``class`` "delete"; on.click closeCallback] []
text errorText
]

let signInPage model dispatch =
concat [
h1 [attr.``class`` "title"] [text "Sign in"]
Expand All @@ -370,6 +419,7 @@ let signInPage model dispatch =
]
]

//#endif
let menuItem (model: Model) (page: Page) (itemText: string) =
li [] [
a [
Expand Down Expand Up @@ -397,9 +447,13 @@ let view model dispatch =
| Home -> homePage model dispatch
| Counter -> counterPage model dispatch
| Data ->
//#if (server_actual)
cond model.signedInAs <| function
| Some username -> dataPage model username dispatch
| None -> signInPage model dispatch
//#else
dataPage model dispatch
//#endif
div [attr.id "notification-area"] [
cond model.error <| function
| None -> empty
Expand All @@ -413,10 +467,20 @@ let view model dispatch =
type MyApp() =
inherit ProgramComponent<Model, Message>()

//#if (!server_actual)
[<Inject>]
member val HttpClient = Unchecked.defaultof<HttpClient> with get, set

//#endif
override this.Program =
//#if (server_actual)
let bookService = this.Remote<BookService>()
let update = update bookService
Program.mkProgram (fun _ -> initModel, Cmd.ofMsg GetSignedInAs) update view
//#else
let update = update this.HttpClient
Program.mkProgram (fun _ -> initModel, Cmd.ofMsg GetBooks) update view
//#endif
|> Program.withRouter router
//#if (hotreload_actual)
#if DEBUG
Expand Down
11 changes: 11 additions & 0 deletions content/application/src/Bolero.Template.Client/Startup.fs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
namespace Bolero.Template.Client

open Microsoft.AspNetCore.Components.WebAssembly.Hosting
//#if (server_actual)
open Bolero.Remoting.Client
//#else
open Microsoft.Extensions.DependencyInjection
open System
open System.Net.Http
//#endif

module Program =

[<EntryPoint>]
let Main args =
let builder = WebAssemblyHostBuilder.CreateDefault(args)
builder.RootComponents.Add<Main.MyApp>("#main")
//#if (server_actual)
builder.Services.AddRemoting(builder.HostEnvironment) |> ignore
//#else
builder.Services.AddScoped<HttpClient>(fun _ ->
new HttpClient(BaseAddress = Uri builder.HostEnvironment.BaseAddress)) |> ignore
//#endif
builder.Build().RunAsync() |> ignore
0
Loading

0 comments on commit a39b523

Please sign in to comment.