Created
July 22, 2018 03:58
-
-
Save aramkoukia/d032628100c6a972dcd2b94ee570bad8 to your computer and use it in GitHub Desktop.
program fs
This file contains 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 firstGiraffe.App | |
open System | |
open Microsoft.AspNetCore.Builder | |
open Microsoft.AspNetCore.Cors.Infrastructure | |
open Microsoft.AspNetCore.Hosting | |
open Microsoft.Extensions.Logging | |
open Microsoft.Extensions.DependencyInjection | |
open Giraffe | |
open firstGiraffe.HttpHandlers | |
// --------------------------------- | |
// Web app | |
// --------------------------------- | |
let webApp = | |
choose [ | |
subRoute "/api" | |
(choose [ | |
GET >=> choose [ | |
route "/hello" >=> handleGetHello | |
] | |
]) | |
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://localhost: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) | |
.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 _ = | |
WebHostBuilder() | |
.UseKestrel() | |
.UseIISIntegration() | |
.Configure(Action<IApplicationBuilder> configureApp) | |
.ConfigureServices(configureServices) | |
.ConfigureLogging(configureLogging) | |
.Build() | |
.Run() | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment