Skip to content

Commit 13eb93d

Browse files
committed
App Settings recipe
1 parent 11723b0 commit 13eb93d

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# How do I add custom configuration?
2+
There are many ways to supply configuration settings e.g. connection strings to your application, and the [official ASP .NET documentation](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0) explains in great detail the various options you have available.
3+
4+
## Configuration of the Server
5+
In this recipe, we show how to add configuration using an `appsettings.json` configuration file.
6+
7+
> Never store secrets in plain text files such as `appsettings.json` - our recommendation is to use it for non-secret configuration data that is shared across your development team; see [here](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-8.0&tabs=windows) for more guidance.
8+
9+
1. In the Server folder, add a file `appsettings.json`. *It does not need to be added to the project file.*
10+
2. Add the following content to it:
11+
```json
12+
{
13+
"MyKey": "My appsettings.json Value"
14+
}
15+
```
16+
3. In `Server.fs`, ensure that your API builder functions take in an `HTTPContext` as an argument and change the construction of your Fable Remoting endpoint to supply one:
17+
```diff
18+
++open Microsoft.AspNetCore.Http
19+
20+
--let todosApi = {
21+
++let todosApi (context: HttpContext) = {
22+
23+
...
24+
25+
-- |> Remoting.fromValue todosApi
26+
++ |> Remoting.fromContext todosApi
27+
```
28+
4. Use the `context` to get a handle to the `IConfiguration` object, which allows you to access settings regardless of what infrastructure you are using to store them e.g. `appsettings.json`, environment variables etc.
29+
```diff
30+
++open Giraffe
31+
++open Microsoft.Extensions.Configuration
32+
33+
let todosApi (context: HttpContext) =
34+
++ let cfg = context.GetService<IConfiguration>()
35+
++ let value = cfg["MyKey"] // "My appsettings.json Value"
36+
```
37+
> Note that the `todosApi` function will be called on every single ASP .NET request. It is safe to "capture" the `cfg` value and use it across multiple API methods.
38+
39+
## Working with User Secrets
40+
User Secrets are an alternative way of storing secrets which, although still stored in plain text files, are not stored in your repository folder and therefore less at risk to accidentally committing into source control. However, Saturn currently disables User Secrets as part of its startup routine, and you must manually turn them back on:
41+
42+
```diff
43+
++type DummyType() = class end
44+
45+
let app = application {
46+
++ host_config (fun hostBuilder ->
47+
++ hostBuilder.ConfigureAppConfiguration(fun _ configBuilder ->
48+
++ configBuilder.AddUserSecrets<DummyType>()
49+
++ |> ignore
50+
++ )
51+
++ )
52+
}
53+
```
54+
55+
You can then access the `IConfiguration` as before, and user secrets values will be accessible.
56+
57+
## Configuration of the client
58+
Configuration of the client can be done in many ways, but generally a simple strategy is to have an API endpoint which is called on startup that provides any settings required by the client.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ nav:
8989
- Test the Client: "recipes/developing-and-testing/testing-the-client.md"
9090
- Test the Server: "recipes/developing-and-testing/testing-the-server.md"
9191
- Debug a SAFE app: "recipes/developing-and-testing/debug-safe-app.md"
92+
- Application Configuration: "recipes/developing-and-testing/app-configuration.md"
9293
- UI:
9394
- Add Tailwind support: "recipes/ui/add-tailwind.md"
9495
- Remove Tailwind support: "recipes/ui/remove-tailwind.md"

0 commit comments

Comments
 (0)