Skip to content

Instantly share code, notes, and snippets.

@romanov
Last active May 9, 2026 20:21
Show Gist options
  • Select an option

  • Save romanov/74eeb082de5ecb5a94a85491b9eea404 to your computer and use it in GitHub Desktop.

Select an option

Save romanov/74eeb082de5ecb5a94a85491b9eea404 to your computer and use it in GitHub Desktop.
F# Minimal Web Api with HTTPS certificate
open System
open System.IO
open System.Net
open System.Security.Cryptography.X509Certificates
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Connections
open Microsoft.Extensions.Hosting
open Microsoft.AspNetCore.Hosting;
let private loadCert (_: ConnectionContext) (_: string) : X509Certificate2 =
let cert = Path.Combine(AppContext.BaseDirectory, "wwwroot", "certs", "certificate.pem")
let key = Path.Combine(AppContext.BaseDirectory, "wwwroot", "certs", "key.pem")
X509Certificate2.CreateFromPemFile(cert, key)
[<EntryPoint>]
let main args =
let builder = WebApplication.CreateBuilder(args)
builder.WebHost.UseUrls("https://*:443") |> ignore
builder.WebHost.UseSetting("https_port", "443") |> ignore
builder.WebHost.ConfigureKestrel(fun serverOptions ->
serverOptions.Listen(IPAddress.Any, 443, fun listenOptions -> (
listenOptions.UseHttps(fun httpsOptions ->(
httpsOptions.ServerCertificateSelector <- loadCert
)) |> ignore
))) |> ignore
let app = builder.Build()
app.MapGet("/", Func<string>(fun () -> "Hello World with https")) |> ignore
app.Run()
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment