Upload of large files using Suave?

131 Views Asked by At

I would like to upload files using Suave. I understand that small files will automatically get written to a /tmp folder, but my files are too large for that. What I would like to do is process them as a stream and send them to long term storage, via the Suave server.

Here is my server:

#r "nuget: Suave"

open System.IO
open Suave
open Suave.Sockets
open Suave.Sockets.Control

let config = defaultConfig

let socketTask (conn : Connection, httpResult : HttpResult) =
  socket {
    printfn "Started socket task"

    use stream = new TransportStream (conn.transport)
    use reader = new StreamReader (stream)

    while not reader.EndOfStream do
      printfn "Reading... "

      let! line = SocketOp.ofAsync (async {
        let! line = reader.ReadLineAsync ()
        return line
      })

      printfn "%s" line

    printfn "Finished socket task"

    return conn
  }

let app : WebPart =
  (fun ctx -> async {
    printfn "Received request"

    return
      Some
        {
          ctx with
            response =
              {
                ctx.response with
                  content = SocketTask socketTask
              }
        }
  })

startWebServer config app

But when I run this curl command, it just hangs:

$ curl -X POST --data-binary @big-file.csv localhost:8080
$ dotnet fsi ./Suave.fsx
[17:32:05 INF] Smooth! Suave listener started in 14.279ms with binding 127.0.0.1:8080
Received request
Started socket task

I would expect the uploaded file to be printed line-by-line.

What am I missing here?

0

There are 0 best solutions below