Suave is a simple web development F# library providing a lightweight web server and a set of combinators to manipulate route flow and task composition.
ademar on master
Corrected default port from 808… Merge pull request #723 from Wa… (compare)
Hi everyone! I'm a newbie of both f# and Suave and I'm loving them both so far!
I have a question tho, which I'm struggling to find an answer to.
Is there a way I can read the request as a stream?
Something that would allow me to do things like:
let stream = ctx.request.Stream ()
and then consuming it incrementally?
thanks a lot in advance!
startWebServer
defaultConfig
(fun ctx ->
async {
let path = ctx.request.path
printfn "path: %s" path
let body =
ctx.request.rawForm
|> System.Text.Encoding.UTF8.GetString
let responseBody =
sprintf "my response to: %s" body
|> System.Text.Encoding.UTF8.GetBytes
let response =
{ status =
{ code = int HttpStatusCode.OK
reason = "resource found" }
headers = []
content = HttpContent.Bytes responseBody
writePreamble = false }
return Some { ctx with response = response }
})
writePreamble
do?
POST
>=> path "/ast-viewer/untyped-ast"
>=> request (fun req ctx ->
async {
let json = req.BodyText
match! getUntypedAST json with
| GetUntypedASTResponse.Ok body -> return! (applicationJson >=> OK body) ctx
| GetUntypedASTResponse.InvalidAST errors -> return! (applicationText >=> BAD_REQUEST errors) ctx
| _ -> return failwith "meh"
})
let applicationJson = setMimeType "application/json"
let applicationText = setMimeType "application/text"
>=> path "/ast-viewer/untyped-ast"
>=> request (fun req ->
let json = req.BodyText
match! getUntypedAST json with
| GetUntypedASTResponse.Ok body -> (applicationJson >=> OK body)
| GetUntypedASTResponse.InvalidAST errors -> (applicationText >=> BAD_REQUEST errors)
| _ -> return failwith "meh")
let corsConfig =
{ defaultCORSConfig with allowedUris = InclusiveOption.All
// InclusiveOption.Some [ "http://localhost:8080" ]
}
let webPart =
cors corsConfig
>=> choose
[ OPTIONS >=> OK "CORS accepted"
GET
>=> path "/ast-viewer/version"
>=> cors corsConfig
>=> setMimeType "text/plain"
>=> OK(getVersion ())
POST >=> path "/ast-viewer/untyped-ast" >=> untypedAst
POST >=> path "/ast-viewer/typed-ast" >=> typedAst
NOT_FOUND "Not found" ]
let setCORSHeaders hostname =
Writers.addHeader "Access-Control-Allow-Origin" hostname
>=> Writers.addHeader "Access-Control-Allow-Headers" "*"
>=> Writers.addHeader "Access-Control-Allow-Methods" "*"