forked from rflechner/Suave.Swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
168 lines (137 loc) · 5.47 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
open System
open Suave
open Suave.Operators
open Suave.Filters
open Suave.Successful
open Suave.Swagger
open Rest
open FunnyDsl
open Swagger
let now1 : WebPart =
fun (x : HttpContext) ->
async {
return! OK (DateTime.Now.ToString()) x
}
let now : WebPart =
fun (x : HttpContext) ->
async {
// The MODEL helper checks the "Accept" header
// and switches between XML and JSON format
return! MODEL DateTime.Now x
}
[<CLIMutable>]
type Pet =
{ Id:int
Uuid:Guid
Name:string
Category:PetCategory }
and [<CLIMutable>] PetCategory =
{ Id:int
Name:string }
[<CLIMutable>]
type SubtractionRequest =
{ First:int
Second:int
}
[<CLIMutable>]
type SubtractionResult = { Result:int }
let createCategory =
JsonBody<PetCategory>(fun model -> MODEL { model with Id=(Random().Next()) })
let subtract(a,b) = OK ((a-b).ToString())
let subtractObj =
JsonBody<SubtractionRequest>(fun {First=a;Second=b} -> MODEL {Result=(a-b)})
let findPetById id =
MODEL
{
Id=id; Name=(sprintf "pet_%d" id); Uuid=(Guid.NewGuid())
Category = { Id=id*100; Name=(sprintf "cat_%d" id) }
}
let findPetByUuid (id:string) =
MODEL
{
Id=1; Name="pet_1"; Uuid=(Guid.Parse(id))
Category = { Id=100; Name="cat_0" }
}
let findCategoryById id =
MODEL
{
Id=id; Name=(sprintf "cat_%d" id)
}
let time1 = GET >=> path "/time1" >=> now
let bye = GET >=> path "/bye" >=> OK "bye. @++"
let bye2 = GET >=> path "/bye2" >=> JSON "bye. @++"
let bye3 = GET >=> path "/bye3" >=> XML "bye. @++"
let api =
swagger {
// syntax 1
for route in getting (simpleUrl "/time" |> thenReturns now) do
yield description Of route is "What time is it ?"
yield route |> tag "time"
for route in getting (urlFormat "/bonjour/%s" (fun x -> OK (sprintf "Bonjour %s" x))) do
yield description Of route is "Say hello in french"
// another syntax
for route in getOf (path "/time2" >=> now) do
yield description Of route is "What time is it 2?"
yield urlTemplate Of route is "/time2"
yield route |> tag "time"
for route in getting <| urlFormat "/subtract/%d/%d" subtract do
yield description Of route is "Subtracts two numbers"
yield route |> tag "maths"
for route in posting <| simpleUrl "/subtract" |> thenReturns subtractObj do
yield description Of route is "Subtracts two numbers"
yield route |> addResponse 200 "Subtraction result" (Some typeof<SubtractionResult>)
yield parameter "subtraction request" Of route (fun p -> { p with Type = (Some typeof<SubtractionRequest>); In=Body })
yield route |> tag "maths"
for route in posting <| urlFormat "/subtract/%d/%d" subtract do
yield description Of route is "Subtracts two numbers"
yield route |> tag "maths"
for route in getting <| urlFormat "/pet/%d" findPetById do
yield description Of route is "Search a pet by id"
yield route |> addResponse 200 "The found pet" (Some typeof<Pet>)
yield route |> supportsJsonAndXml
yield route |> tag "pets"
for route in getOf (pathScan "/pet/byuuid/%s" findPetByUuid) do
yield description Of route is "Search a pet by uuid"
yield urlTemplate Of route is "/pet/byuuid/{uuid}"
yield parameter "UUID" Of route (fun p -> { p with Type = (Some typeof<Guid>); In=Path })
yield route |> addResponse 200 "The found pet" (Some typeof<Pet>)
yield route |> supportsJsonAndXml
yield route |> tag "pets"
for route in getting <| urlFormat "/category/%d" findCategoryById do
yield description Of route is "Search a category by id"
yield route |> addResponse 200 "The found category" (Some typeof<PetCategory>)
yield route |> tag "pets"
for route in posting <| simpleUrl "/category" |> thenReturns createCategory do
yield description Of route is "Create a category"
yield route |> addResponse 200 "returns the create model with assigned Id" (Some typeof<PetCategory>)
yield parameter "category model" Of route (fun p -> { p with Type = (Some typeof<PetCategory>); In=Body })
yield route |> tag "pets"
// Classic routes with manual documentation
for route in bye do
yield route.Documents(fun doc -> { doc with Description = "Say good bye." })
yield route.Documents(fun doc -> { doc with Template = "/bye"; Verb=Get })
for route in getOf (pathScan "/add/%d/%d" (fun (a,b) -> OK((a + b).ToString()))) do
yield description Of route is "Compute a simple addition"
yield urlTemplate Of route is "/add/{number1}/{number2}"
yield parameter "number1" Of route (fun p -> { p with Type = (Some typeof<int>); In=Path })
yield parameter "number2" Of route (fun p -> { p with Type = (Some typeof<int>); In=Path })
for route in getOf (path "/hello" >=> OK "coucou") do
yield description Of route is "Say hello"
yield urlTemplate Of route is "/hello"
}
|> fun a ->
a.Describes(
fun d ->
{
d with
Title = "Swagger and Suave.io"
Description = "A simple swagger with Suave.io example"
})
[<EntryPoint>]
let main argv =
async {
do! Async.Sleep 2000
System.Diagnostics.Process.Start "http://localhost:8080/swagger/v2/ui/index.html" |> ignore
} |> Async.Start
startWebServer defaultConfig api.App
0 // return an integer exit code