Skip to content

Commit e77c872

Browse files
0x53AKrzysztof-Cieslak
andauthoredMay 18, 2020
Documentation: add Hello World and How to add it to Giraffe (#245)
* Documentation: add Hello World and How to add it to Giraffe * documentation: adding saturn to giraffe * docs: add links to explanations * doc: link to SAFE Dojo from getting started Co-authored-by: Krzysztof Cieślak <krzysztof_cieslak@windowslive.com>
1 parent d8541ba commit e77c872

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed
 

‎docs/content/tutorials/adding-pages.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Adding Pages
33
category: tutorial
4-
menu_order: 2
4+
menu_order: 3
55
---
66

77
# Adding Pages
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Getting started
3+
category: tutorial
4+
menu_order: 2
5+
---
6+
7+
# Adding Saturn to an existing Giraffe application
8+
9+
The previous chapter showed how to get started with a new application.
10+
11+
If you already have a working Giraffe webserver, you can gradually opt-in to Saturn.
12+
13+
For example, if your existing app looks like this:
14+
15+
```f#
16+
17+
type Customer = {
18+
Name : string
19+
Address : string
20+
}
21+
22+
let customers =
23+
choose [
24+
GET >=> (json { Name = "Mr. Smith"; Address = "Santa Monika"})
25+
PUT >=> (bindJson<Customer> (fun customer -> printfn "Adding customer %A" customer; setStatusCode 200))
26+
]
27+
28+
29+
let webApp =
30+
choose [
31+
route "/" >=> htmlFile "/pages/index.html"
32+
route "/api/customers" >=> customers
33+
]
34+
```
35+
36+
and you need to add "vendor" functionality, you could implement it as a Saturn ``router`` while keeping everything else intact:
37+
38+
39+
```f#
40+
// the new Saturn router
41+
let vendors = router {
42+
getf "/%i" (fun vendorId -> (json (readVendorFromDb vendorId)))
43+
post "/" (bindJson<Vendor> (fun customer -> addVendor vendor; setStatusCode 200))
44+
}
45+
46+
let webApp =
47+
choose [
48+
route "/" >=> htmlFile "/pages/index.html"
49+
route "/api/customers" >=> customers
50+
// plug the new Saturn router into the Giraffe app
51+
route "/api/vendors" >=> vendors
52+
]
53+
```
54+
55+
# Embedding Giraffe Handlers into Saturn
56+
57+
Of course the other way around also works.
58+
59+
For example, [Elmish.Bridge](https://github.com/Nhowka/Elmish.Bridge) does not provide a specialized implementation for Saturn. And it doesn't need to, because we can just use the Giraffe implementation!
60+
61+
```f#
62+
63+
open Elmish
64+
open Elmish.Bridge
65+
66+
let elmishBridgeHandler : HttpHandler =
67+
Bridge.mkServer Shared.endpoint init update
68+
|> Bridge.run Giraffe.server
69+
70+
// our existing Saturn router
71+
let router = router {
72+
73+
// ...
74+
75+
forward "" elmishBridgeHandler
76+
}
77+
```

‎docs/content/tutorials/how-to-start.md

+31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Requirements:
1010

1111
* `dotnet` SDK 3.1 [https://dotnet.microsoft.com/download/dotnet-core/3.1](https://dotnet.microsoft.com/download/dotnet-core/3.1)
1212

13+
## Template
14+
15+
The easiest way to get started is to use the provided template:
1316

1417
1. Install the `dotnet` template with `dotnet new -i Saturn.Template`
1518
2. Create a new folder and move into it - `mkdir SaturnSample && cd SaturnSample`
@@ -20,3 +23,31 @@ Requirements:
2023
8. Open the folder in your favourite editor (VSCode) and insert the line (`forward "/books" Books.Controller.resource`) into `browserRouter` in `Router.fs` file
2124
9. Start the application by running `dotnet fake build -t run` from the root of the solution. This will start the application in watch mode (automatic recompilation on changes) and open your browser on [http://localhost:8085](http://localhost:8085) which should display the index page.
2225
10. Go to [http://localhost:8085/books](http://localhost:8085/books) to see the generated view. All buttons should be working; you can add new entries, and remove or edit old ones.
26+
27+
28+
## Hello World
29+
30+
If you want to start from scratch with a minimal Saturn webserver:
31+
32+
1. Create a new F# Project (for example with `dotnet new console -lang F#`)
33+
2. Add the `Saturn` NuGet Package
34+
35+
```f#
36+
open Saturn
37+
open Giraffe
38+
39+
let app = application {
40+
use_router (text "Hello World from Saturn")
41+
}
42+
43+
run app
44+
```
45+
46+
If you compile and run this application, it will unconditionally return the text regardless of the path.
47+
48+
From here on out you can add [routers](../explanations/routing.md), [controllers](../explanations/controller.md) and [views](../explanations/view.md).
49+
50+
## Deep Dive
51+
52+
To get a better understanding of Saturn and the whole SAFE Stack you can look at the [SAFE-Dojo](https://github.com/CompositionalIT/SAFE-Dojo).
53+

0 commit comments

Comments
 (0)
Please sign in to comment.