-
Notifications
You must be signed in to change notification settings - Fork 13
/
Program.fs
196 lines (162 loc) · 6.68 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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 }
type OrderFile =
{ OrderNumber: string
Canceled : string option
Packages : string option
RejectedItems : RejectedItem option }
and RejectedItem =
{ SerialNumber: string
NotShippedType: int
NotShippedNote: string option }
let createCategory =
JsonBody<PetCategory>(fun model -> MODEL { model with Id=(Random().Next()) })
let createOrderFile =
JsonBody<OrderFile>(
fun _ ->
MODEL (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"
for route in getting (pathTemplate "/hello/%s/%s" ["lastname";"firstname"] (fun (lastname, firstname) -> OK (sprintf "Bonjour %s %s" lastname firstname))) do
yield description Of route is "Say hello"
// 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 operationId Of route is "Create a category"
yield description Of route is "Create a category"
yield route |> addResponse 200 "returns the create model with assigned Id" (Some typeof<PetCategory>)
yield parameter "body" Of route (fun p -> { p with Type = (Some typeof<PetCategory>); In=Body })
yield route |> supportsJsonAndXml
yield route |> tag "pets"
for route in posting <| simpleUrl "/orderFile" |> thenReturns createOrderFile do
yield operationId Of route is "Create a OrderFile"
yield description Of route is "Create a OrderFile"
yield route |> addResponse 200 "returns the create model with assigned Id" (Some typeof<OrderFile>)
yield parameter "body" Of route (fun p -> { p with Type = (Some typeof<OrderFile>); In=Body })
yield route |> supportsJsonAndXml
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:8082/swagger/v3/ui/index.html" |> ignore
} |> Async.Start
startWebServer { defaultConfig with bindings = [ HttpBinding.createSimple HTTP "127.0.0.1" 8082 ] } api.App
0 // return an integer exit code