Created
April 9, 2018 04:55
-
-
Save greentornado/5890ba5024c13287304eccad27f1d48d to your computer and use it in GitHub Desktop.
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
module WebsocketApp.App | |
open System | |
open System.IO | |
open Microsoft.AspNetCore.Builder | |
open Microsoft.AspNetCore.Cors.Infrastructure | |
open Microsoft.AspNetCore.Hosting | |
open Microsoft.Extensions.Logging | |
open Microsoft.Extensions.DependencyInjection | |
open Giraffe | |
// --------------------------------- | |
// Models | |
// --------------------------------- | |
type Message = | |
{ | |
Text : string | |
} | |
// --------------------------------- | |
// Views | |
// --------------------------------- | |
module Views = | |
open GiraffeViewEngine | |
let layout (content: XmlNode list) = | |
html [] [ | |
head [] [ | |
title [] [ encodedText "WebsocketApp" ] | |
link [ _rel "stylesheet" | |
_type "text/css" | |
_href "/main.css" ] | |
] | |
body [] content | |
] | |
let partial () = | |
h1 [] [ encodedText "WebsocketApp" ] | |
let index (model : Message) = | |
[ | |
partial() | |
p [] [ encodedText model.Text ] | |
] |> layout | |
// --------------------------------- | |
// Web app | |
// --------------------------------- | |
let indexHandler (name : string) = | |
let greetings = sprintf "Hello %s, from Giraffe!" name | |
let model = { Text = greetings } | |
let view = Views.index model | |
htmlView view | |
[<CLIMutable>] | |
type Car = | |
{ | |
Name : string | |
Make : string | |
Wheels : int | |
Built : DateTime | |
} | |
let submitCar : HttpHandler = | |
fun (next : HttpFunc) (ctx : HttpContext) -> | |
task { | |
// Binds a JSON payload to a Car object | |
let! car = ctx.BindJsonAsync<Car>() | |
// Sends the object back to the client | |
return! Successful.OK car next ctx | |
} | |
let webApp = | |
choose [ | |
GET >=> | |
choose [ | |
route "/" >=> indexHandler "world" | |
routef "/hello/%s" indexHandler | |
] | |
POST >=> route "/car" >=> submitCar | |
setStatusCode 404 >=> text "Not Found" ] | |
// --------------------------------- | |
// Error handler | |
// --------------------------------- | |
let errorHandler (ex : Exception) (logger : ILogger) = | |
logger.LogError(EventId(), ex, "An unhandled exception has occurred while executing the request.") | |
clearResponse >=> setStatusCode 500 >=> text ex.Message | |
// --------------------------------- | |
// Config and Main | |
// --------------------------------- | |
let configureCors (builder : CorsPolicyBuilder) = | |
builder.WithOrigins("http://0.0.0.0:8080") | |
.AllowAnyMethod() | |
.AllowAnyHeader() | |
|> ignore | |
let configureApp (app : IApplicationBuilder) = | |
let env = app.ApplicationServices.GetService<IHostingEnvironment>() | |
(match env.IsDevelopment() with | |
| true -> app.UseDeveloperExceptionPage() | |
| false -> app.UseGiraffeErrorHandler errorHandler) | |
.UseCors(configureCors) | |
.UseStaticFiles() | |
.UseGiraffe(webApp) | |
let configureServices (services : IServiceCollection) = | |
services.AddCors() |> ignore | |
services.AddGiraffe() |> ignore | |
let configureLogging (builder : ILoggingBuilder) = | |
let filter (l : LogLevel) = l.Equals LogLevel.Error | |
builder.AddFilter(filter).AddConsole().AddDebug() |> ignore | |
[<EntryPoint>] | |
let main _ = | |
let contentRoot = Directory.GetCurrentDirectory() | |
let webRoot = Path.Combine(contentRoot, "WebRoot") | |
WebHostBuilder() | |
.UseKestrel() | |
.UseContentRoot(contentRoot) | |
.UseIISIntegration() | |
.UseWebRoot(webRoot) | |
.Configure(Action<IApplicationBuilder> configureApp) | |
.ConfigureServices(configureServices) | |
.ConfigureLogging(configureLogging) | |
.UseUrls("http://0.0.0.0:5000") | |
.Build() | |
.Run() | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment