Skip to content

Commit

Permalink
Added sample configuration code for IOptions pattern usage
Browse files Browse the repository at this point in the history
  • Loading branch information
xperiandri committed Jul 1, 2021
1 parent 4deaf14 commit dcc4b94
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 49 deletions.
53 changes: 53 additions & 0 deletions src/Templates/SolutionTemplate/SolutionTemplate/Configuration.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module SolutionTemplate.Configuration

open System
open System.Linq
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.Hosting
open Xamarin.Essentials

module Environments =
let LocalAPI = "LocalAPI"

let configure (ctx : HostBuilderContext) (builder : IConfigurationBuilder) =

let environmentName = ctx.HostingEnvironment.EnvironmentName

let inMemoryCollection = seq {

yield!
match environmentName with
| name when name.Equals(Environments.LocalAPI, StringComparison.CurrentCultureIgnoreCase) ->
let endpoint =
if DeviceInfo.Platform = DevicePlatform.Android
then "http://10.0.2.2:7071/GraphQL"
else "http://localhost:7071/GraphQL"
seq {
yield struct("GraphQL:EndPoint", endpoint)
}
| name when name.Equals(Environments.Development, StringComparison.CurrentCultureIgnoreCase) ->
seq {
yield struct("GraphQL:EndPoint", "https://host.net/GraphQL")
}
| name when name.Equals(Environments.Production, StringComparison.CurrentCultureIgnoreCase) ->
seq {
yield struct("GraphQL:EndPoint", "https://host.net/GraphQL")
}
| _ -> raise <| NotSupportedException ()

//yield struct("AzureMaps:Key", BingAutocomplete.key)
}

builder.AddInMemoryCollection (inMemoryCollection.ToDictionary(fstv, sndv)) |> ignore
()

[<CLIMutable>]
type GraphQLSettings =
{ EndPoint : string }

open Microsoft.Extensions.DependencyInjection

type IServiceCollection with
member services.ConfigureSolutionTemplateOptions (configuration : IConfiguration) =
services.Configure<GraphQLSettings>(configuration.GetSection("GraphQL"))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SolutionTemplate.Elmish1
namespace SolutionTemplate.ElmishProgram

[<AutoOpen>]
module State =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SolutionTemplate

module ValueOption =
let toOption value =
match value with | ValueSome v -> Some v | _ -> None

module Option =
let toVOption value =
match value with | Some v -> ValueSome v | _ -> ValueNone

[<AutoOpen>]
module ValueTuple =

let fstv struct (a,_) = a
let sndv struct (_,b) = b
62 changes: 18 additions & 44 deletions src/Templates/SolutionTemplate/SolutionTemplate/Host.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,46 @@ open System.Linq
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Options
open Xamarin.Essentials

open SolutionTemplate.Configuration
open SolutionTemplate.Programs

let configureHost (builder : IConfigurationBuilder) =

if DeviceInfo.Platform <> DevicePlatform.UWP then
let root = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "root")
Directory.CreateDirectory(root) |> ignore
let root = Path.Combine (System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "root")
Directory.CreateDirectory root |> ignore
let inMemoryCollection = seq {
struct(HostDefaults.ContentRootKey, root)
}
builder.AddInMemoryCollection (inMemoryCollection.ToDictionary((fun struct(k,_) -> k), (fun struct(_,v) -> v))) |> ignore

let configureConfiguration (ctx : HostBuilderContext) (builder : IConfigurationBuilder) =

// TODO: Use some other way usable for conditional builds
let environmentName = Environments.Development
ctx.HostingEnvironment.EnvironmentName <- environmentName
//let environmentName = ctx.HostingEnvironment.EnvironmentName
//let inMemoryCollection = seq {

// //if not (DeviceInfo.Platform = DevicePlatform.UWP) then
// // let root = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "root")
// // Directory.CreateDirectory(root)
// // yield struct(HostDefaults.ContentRootKey, root)

// yield!
// match environmentName with
// | name when name.Equals(Environments.DevelopmentLocal, StringComparison.CurrentCultureIgnoreCase) ->
// let endpoint =
// if DeviceInfo.Platform = DevicePlatform.Android
// then "http://10.0.2.2:7071/GraphQL"
// else "http://localhost:7071/GraphQL"
// seq {
// yield struct("GraphQL:Endpoint", endpoint)
// }
// | name when name.Equals(Environments.Development, StringComparison.CurrentCultureIgnoreCase) ->
// seq {
// yield struct("GraphQL:Endpoint", "https://host.net/GraphQL")
// }
// | name when name.Equals("Production", StringComparison.CurrentCultureIgnoreCase) ->
// seq {
// yield struct("GraphQL:Endpoint", "https://host.net/GraphQL")
// }
// | _ -> raise <| NotSupportedException ()

// yield struct("AzureMaps:Key", BingAutocomplete.key)
//}

//builder.AddInMemoryCollection (inMemoryCollection.ToDictionary((fun struct(k,_) -> k), (fun struct(_,v) -> v))) |> ignore
//()
builder.AddInMemoryCollection (inMemoryCollection.ToDictionary(fstv, sndv)) |> ignore

let configureServices (ctx : HostBuilderContext) (services : IServiceCollection) =
services
.ConfigureSolutionTemplateOptions(ctx.Configuration)
.AddScoped<Notification.Program>()
.AddScoped<App.Program>()
|> ignore

[<CompiledName("CreateDefaultBuilder")>]
let createDefaultBuilder () =

let environmentName =
#if PRODUCTION
Environments.Production
#elif STAGING
Environments.Staging
#elif LOCALAPI
Environments.LocalAPI
#else
Environments.Development
#endif

HostBuilder()
.UseEnvironment(environmentName)
.ConfigureHostConfiguration(Action<_>(configureHost))
.ConfigureAppConfiguration(configureConfiguration)
.ConfigureAppConfiguration(Configuration.configure)
.ConfigureServices(configureServices)

[<CompiledName("ElmConfig")>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ open System
open Elmish
open Elmish.Uno

open SolutionTemplate.Configuration
open SolutionTemplate.Models
open SolutionTemplate.Programs.Messages
open SolutionTemplate.Programs
open Microsoft.Extensions.Options

type Model = {
Text: string
Expand All @@ -18,11 +20,11 @@ type Msg =
| SetSearchText of string
| NotificationMsg of Notification.Msg

type Program(notificationProgram: Notification.Program) =
type Program(notificationProgram: Notification.Program, graphQLOptions: IOptions<GraphQLSettings>) =

let init () =
let notificationModel = notificationProgram.Initial
{ Text = "Привет от Elmish.Uno"
{ Text = $"Привет от Elmish.Uno. Endpoint '{graphQLOptions.Value.EndPoint}'"
Notifications = notificationModel },
Cmd.none

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Elmish.fs" />
<Compile Include="Helpers\Elmish.fs" />
<Compile Include="Helpers\ObjAndStructConversions.fs" />
<Compile Include="Pages.fs" />
<Compile Include="Models\Models.fs" />
<Compile Include="Configuration.fs" />
<Compile Include="Programs\Messages.fs" />
<Compile Include="Programs\Notification.Program.fs" />
<Compile Include="Programs\App.Program.fs" />
Expand All @@ -30,7 +32,6 @@
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="Uno.Core" Version="$(UnoCoreVersion)" />
<PackageReference Include="Uno.UI" Version="$(UnoUIVersion)" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.*" />
</ItemGroup>

Expand Down

0 comments on commit dcc4b94

Please sign in to comment.