Skip to content

Commit 40f0780

Browse files
authored
Merge pull request #305 from SAFE-Stack/peter-branch-rescue
Bits of Peter's PR that should be merged in.
2 parents 0390bac + 6b12015 commit 40f0780

File tree

5 files changed

+289
-111
lines changed

5 files changed

+289
-111
lines changed

docs/recipes/build/bundle-app.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# How do I bundle my SAFE application?
2+
3+
When developing your SAFE application, the local runtime experience uses Vite to run the client and redirect API calls to the server on a [different port](../../../faq-build). However, when you *deploy* your application, you'll need to run your Saturn server which will serve up statically-built client resources (HTML, JavaScript, CSS etc.).
4+
5+
#### 1. Run the FAKE script
6+
If you created your SAFE app using the recommended defaults, your application already has a FAKE script which will do the bundling for you. You can create a bundle using the following command:
7+
8+
```cmd
9+
dotnet run Bundle
10+
```
11+
12+
This will build and package up both the client and server and place them into the `/deploy` folder at the root of the repository.
13+
14+
> See [here](../../../template-safe-commands) for more details on this build target.
15+
16+
## Testing the bundle
17+
1. Navigate to the `deploy` folder at the root of your repository.
18+
2. Run the `Server.exe` application.
19+
3. Navigate in your browser to `http://localhost:5000`.
20+
21+
You should now see your SAFE application.
22+
23+
## Further reading
24+
See [this article](/docs/faq-build) for more information on architectural concerns regarding the move from dev to production and bundling SAFE Stack applications.

docs/recipes/build/remove-fake.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# How do I remove the use of FAKE?
2+
[FAKE](https://fake.build/) is a tool for build automation. The standard SAFE template comes with a [ready-made build project](../../../template-safe-commands) at the root of the solution that provides support for many common SAFE tasks.
3+
4+
If you would prefer not to use FAKE, you can of course simply ignore it, but this recipes shows how to completely remove it from your repository. It is important to note that having removed FAKE, you will have to follow a more manual approach to each of these processes. This recipe will only include instructions on how to run the application after removing FAKE.
5+
6+
> Note that the minimal template does not use FAKE by default, and **this recipe only applies to the standard template**.
7+
8+
#### 1. Build project
9+
Delete `Build.fs`, `Build.fsproj`, `Helpers.fs`, `paket.references` at the root of the solution.
10+
11+
#### 2. Dependencies
12+
Remove the following dependencies
13+
```fsharp
14+
dotnet paket remove Fake.Core.Target
15+
dotnet paket remove Fake.IO.FileSystem
16+
dotnet paket remove Farmer
17+
```
18+
19+
## Running the App
20+
Now that you have removed the FAKE application dependencies, you will have to separately run the server and the client.
21+
22+
#### 1. Start the Server
23+
Navigate to `src/Server` inside a terminal and execute `dotnet run`.
24+
25+
#### 2. Start the Client
26+
Navigate to `src/Client` inside a terminal and execute the following:
27+
28+
```bash
29+
dotnet tool restore
30+
npm install
31+
dotnet fable watch -o output -s --run npx vite
32+
```
33+
34+
---
35+
36+
The app will now be running at `http://localhost:8080/`. Navigate to this address in a browser to see your app running.
37+
38+
## Bundling the App
39+
See [this guide](../build/bundle-app.md#2-im-using-the-minimal-template) to learn how to package a SAFE application for deployment to e.g. Azure.
40+
41+
---

docs/recipes/storage/use-litedb.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# How Do I Use LiteDB?
2+
The default template uses in-memory storage. This recipe will show you how to replace the in-memory storage with [LiteDB](https://github.com/mbdavid/LiteDB) in the form of [LiteDB.FSharp](https://github.com/Zaid-Ajaj/LiteDB.FSharp).
3+
4+
#### 1. Add LiteDB.FSharp
5+
Add the [LiteDB.FSharp](https://www.nuget.org/packages/LiteDB.FSharp/) NuGet package to the [server project](./../package-management/add-nuget-package-to-server.md).
6+
7+
#### 2. Create the database
8+
Replace the use of the `ResizeArray` in the `Storage` type with a database and collection:
9+
10+
```fsharp
11+
open LiteDB.FSharp
12+
open LiteDB
13+
14+
type Storage () =
15+
let database =
16+
let mapper = FSharpBsonMapper()
17+
let connStr = "Filename=Todo.db;mode=Exclusive"
18+
new LiteDatabase (connStr, mapper)
19+
let todos = database.GetCollection<Todo> "todos"
20+
```
21+
22+
> LiteDb is a file-based database, and will create the file if it does not exist automatically.
23+
24+
This will create a database file `Todo.db` in the `Server` folder. The option `mode=Exclusive` is added for MacOS support (see this [issue](https://github.com/mbdavid/LiteDB/issues/787)).
25+
26+
> See [here](https://www.litedb.org/docs/connection-string/) for more information on connection string arguments.
27+
28+
> See the [official docs](https://www.litedb.org/docs) for details on constructor arguments.
29+
30+
#### 3. Implement the rest of the repository
31+
Replace the implementations of `GetTodos` and `AddTodo` as follows:
32+
33+
```fsharp
34+
/// Retrieves all todo items.
35+
member _.GetTodos () =
36+
todos.FindAll () |> List.ofSeq
37+
38+
/// Tries to add a todo item to the collection.
39+
member _.AddTodo (todo:Todo) =
40+
if Todo.isValid todo.Description then
41+
todos.Insert todo |> ignore
42+
Ok ()
43+
else
44+
Error "Invalid todo"
45+
```
46+
47+
#### 4. Initialise the database
48+
Modify the existing "priming" so that it first checks if there are any records in the database before inserting data:
49+
50+
```fsharp
51+
if storage.GetTodos() |> Seq.isEmpty then
52+
storage.AddTodo(Todo.create "Create new SAFE project") |> ignore
53+
storage.AddTodo(Todo.create "Write your app") |> ignore
54+
storage.AddTodo(Todo.create "Ship it !!!") |> ignore
55+
```
56+
57+
#### 5. Make Todo compatible with LiteDb
58+
Add the [CLIMutable](https://github.com/MicrosoftDocs/visualfsharpdocs/blob/master/docs/conceptual/core.climutableattribute-class-%5Bfsharp%5D.md) attribute to the `Todo` record in `Shared.fs`
59+
60+
```fsharp
61+
[<CLIMutable>]
62+
type Todo =
63+
{ Id : Guid
64+
Description : string }
65+
```
66+
67+
> This is required to allow LiteDB to hydrate (read) data into F# records.
68+
69+
#### All Done!
70+
* Run the application.
71+
* You will see that a database has been created in the Server folder and that you are presented with the standard TODO list.
72+
* Add an item and restart the application; observe that your data is still there.

docs/recipes/ui/add-fontawesome.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# How Do I Use FontAwesome?
2+
[FontAwesome](https://fontawesome.com/) is the most popular icon set out there and will provide you with a handful of free icons as well as a multitude of premium icons. The standard SAFE template has out-of-the-box support for FontAwesome. You can just start using it in your Client code like so:
3+
4+
```fsharp
5+
open Feliz
6+
7+
Html.i [ prop.className "fas fa-star" ]
8+
```
9+
This will display a solid star icon.
10+
11+
## I'm not using the standard SAFE template!
12+
If you don't need the full features of Feliz we suggest using `Fable.FontAwesome.Free`.
13+
14+
#### 1. The NuGet Package
15+
Add [Fable.FontAwesome.Free NuGet Package](https://www.nuget.org/packages/Fable.FontAwesome.Free/) to the Client project.
16+
> See [How do I add a Nuget package to the Client?](../package-management/add-nuget-package-to-client.md).
17+
18+
#### 2. The CDN Link
19+
Open the `index.html` file and add the following line to the `head` element:
20+
```html
21+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css">
22+
```
23+
24+
#### 3. Code snippet
25+
26+
```fsharp
27+
open Fable.FontAwesome
28+
29+
Icon.icon [
30+
Fa.i [ Fa.Solid.Star ] [ ]
31+
]
32+
```
33+
34+
#### All Done!
35+
Now you can use FontAwesome in your code

0 commit comments

Comments
 (0)