Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added recipe to use appsettings on server side. #351

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions docs/recipes/patterns/add-application-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# How do I add Appliation configuration (appsettings.json)?

This recipe describe how to add application configuration to the server. It uses the regular dotnet middleware

## configure server project

### create appsettings.json

1. create the **appsettings.json** file und src/Server with following content

```json
{
"AppSettings" : {
"Setting1": "Value1",
"Setting2": "Value2"
}
}
```

1. update the **src/Server/Server.fsproj** file with the following entry
```xml
<ItemGroup>
<None Include="appsettings.json" CopyToPublishDirectory="Always" />
</ItemGroup>
```

### add middleware in **Server.fs**

1. open modules

```fs
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Configuration
open Microsoft.AspNetCore.Http
```

1. change addTodo function of todosApi like that

```fs
let todosApi (context: HttpContext) = {

addTodo = fun todo -> async {
// get configuration from appSettings (automatically loaded when appsettings.json present)
let configuration = context.GetService<IConfiguration>()

let appSettings = configuration.GetSection("AppSettings")
let setting1 = appSettings["Setting1"]
printfn $"Setting1 in addTodo: {setting1}"

return
match Storage.addTodo todo with
| Ok() -> todo
| Error e -> failwith e
}
}
```

1. add configureServices before application

```fs
let configureServices (services : IServiceCollection) =
// in case you need an environment specific settings file
// let settingsFile = "appsettings." + Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") + ".json"
let settingsFile = "appsettings.json"

let configBuilder = ConfigurationBuilder()
let config = configBuilder.AddJsonFile(settingsFile).Build()

// in case one wants to read configuration and use it while preparing services etc.

// test loaded settings
let s1 = config["AppSettings:Setting1"]
printfn $"Setting1: {s1}"

// load section and then get setting
let appSettings = config.GetSection("AppSettings")
let setting = appSettings["Setting1"]
printfn $"Setting1: {setting}"

services

```

1. change Remoting.fromValue in webApp function into Remoting.fromContext

```fs
let webApp =
Remoting.createApi ()
|> Remoting.withRouteBuilder Route.builder
|> Remoting.fromContext todosApi
|> Remoting.buildHttpHandler

```

1. add service_config in application

```fs
let app = application {
use_router webApp
service_config configureServices // only needed to prepare further services
memory_cache
use_static "public"
use_gzip
}
```


35 changes: 18 additions & 17 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ site_url: http://safe-stack.github.io/docs/
repo_name: SAFE-Stack/docs
repo_url: https://github.com/SAFE-Stack/docs
theme:
name: "material"
custom_dir: "theme"
palette:
primary: "light blue"
accent: "indigo"
logo: img/safe_favicon.png
name: "material"
custom_dir: "theme"
palette:
primary: "light blue"
accent: "indigo"
logo: img/safe_favicon.png

markdown_extensions:
- admonition
- codehilite
- pymdownx.tabbed:
alternate_style: true
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
format: !!python/name:pymdownx.superfences.fence_div_format
- admonition
- codehilite
- pymdownx.tabbed:
alternate_style: true
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
format: !!python/name:pymdownx.superfences.fence_div_format

extra_css:
- https://unpkg.com/[email protected]/dist/mermaid.css
- https://unpkg.com/[email protected]/dist/mermaid.css
extra_javascript:
- https://unpkg.com/[email protected]/dist/mermaid.min.js
- https://unpkg.com/[email protected]/dist/mermaid.min.js

extra:
social:
Expand Down Expand Up @@ -116,6 +116,7 @@ nav:
- Sync NuGet and NPM Packages: "recipes/package-management/sync-nuget-and-npm-packages.md"
- Patterns:
- Use Dependency Injection: "recipes/patterns/add-dependency-injection.md"
- Use Application Configuration: "recipes/patterns/add-application-configuration.md"
- Client / Server:
- Use Giraffe instead of Saturn: "recipes/client-server/saturn-to-giraffe.md"
- Handle server errors on the client: "recipes/client-server/server-errors-on-client.md"
Expand Down