Skip to content

Instantly share code, notes, and snippets.

@ctaggart
Created November 22, 2017 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctaggart/f919607252c0cedeb79b0f75023e72c0 to your computer and use it in GitHub Desktop.
Save ctaggart/f919607252c0cedeb79b0f75023e72c0 to your computer and use it in GitHub Desktop.
module App
open Node
open Node.http2
open Node.net
open Fable.Core
open Fable.Core.JsInterop
let noObj: obj option = None
let server = http2.createSecureServer(jsOptions<SecureServerOptions>(fun opt ->
opt.key <- fs.readFileSync(PathLike.ofString "localhost-privkey.pem" |> U2.Case1, noObj) |> U4.Case3 |> Some
opt.cert <- fs.readFileSync(PathLike.ofString "localhost-cert.pem" |> U2.Case1, noObj) |> U4.Case3 |> Some
))
server.on_error (fun err -> printfn "%A" err) |> ignore
server.on_socketError (fun err -> printfn "%A" err) |> ignore
server.on_stream (fun stream _inHeaders _ ->
let outHeaders = createEmpty<OutgoingHttpHeaders>
outHeaders.["content-type"] <- "text/html" |> U3.Case2 |> Some
outHeaders.[":status"] <- 200. |> U3.Case1 |> Some
stream.respond outHeaders
let wstream = stream :> stream.Writable
wstream.``end``("<h1>HTTP/2 from F# !!!</h1>", "utf8")
) |> ignore
server.listen(jsOptions<ListenOptions>(fun opt ->
opt.port <- 8443. |> Some
)) |> ignore
dotnet new -i ctaggart.templates
dotnet new http2 -n myhttp2
cd myhttp2
.\build.ps1
.\localhost-cert.ps1
.\start.ps1
@alfonsogarciacaro
Copy link

You can use the !^ to instantiate erased unions. Not everybody likes it, but it makes the code less verbose and removes the need to specify the case while keeping type checking:

let noObj: obj option = None
let server = http2.createSecureServer(jsOptions(fun opt ->
    opt.key <- !^fs.readFileSync(!^(PathLike.ofString "localhost-privkey.pem"), noObj) |> Some
    opt.cert <- !^fs.readFileSync(!^(PathLike.ofString "localhost-cert.pem"), noObj) |> Some
))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment