Last active
May 9, 2026 20:21
-
-
Save romanov/74eeb082de5ecb5a94a85491b9eea404 to your computer and use it in GitHub Desktop.
F# Minimal Web Api with HTTPS certificate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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