Skip to content

Instantly share code, notes, and snippets.

@ninjarobot
Last active May 23, 2016 17:46
Show Gist options
  • Save ninjarobot/949f2eb333a08fa8455984086dc0b076 to your computer and use it in GitHub Desktop.
Save ninjarobot/949f2eb333a08fa8455984086dc0b076 to your computer and use it in GitHub Desktop.
Simple kestrel-based ASP.NET server in FSharp on dotnet core RC2
(*
* To get started, `dotnet new --lang f#`
* Add these to the 'dependencies' section in project.json:
"System.Threading.Tasks": "4.0.11-rc2-*",
"Microsoft.AspNetCore.Hosting":"1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console":"1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel":"1.0.0-rc2-final"
* And then edit Program.fs with reqHandler for the RequestDelegate,
* wrapping an async workflow.
*)
open System
open System.Threading.Tasks
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Logging.Console
type Startup() =
member x.Configure (app:IApplicationBuilder, hosting:IHostingEnvironment, loggerFactory:ILoggerFactory) =
loggerFactory.AddConsole(LogLevel.Debug) |> ignore
let logger = loggerFactory.CreateLogger("MyApp")
let reqHandler (ctx: HttpContext) =
async {
sprintf "Received request at %O" ctx.Request.Path
|> logger.LogInformation
ctx.Response.ContentType <- "application/json"
do! ctx.Response.WriteAsync ("""{"hello":"world"}""") |> Async.AwaitTask
} |> Async.StartAsTask :> Task
app.Run(RequestDelegate(reqHandler))
[<EntryPoint>]
let main argv =
let h = WebHostBuilder().UseKestrel().UseStartup<Startup>().Build()
h.Run()
0
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true,
"compilerName": "fsc",
"compile": {
"includeFiles": [
"Program.fs"
]
}
},
"dependencies": {
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160316",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
},
"System.Threading.Tasks": "4.0.11-rc2-*",
"Microsoft.AspNetCore.Hosting":"1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console":"1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel":"1.0.0-rc2-final"
},
"tools": {
"dotnet-compile-fsc": {
"version": "1.0.0-*",
"imports": [
"dnxcore50",
"portable-net45+win81",
"netstandard1.3"
]
}
},
"frameworks": {
"netstandard1.5": {
"imports": [
"portable-net45+win8",
"dnxcore50"
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment