diff options
Diffstat (limited to 'src/FundLab.Api/App.fs')
| -rw-r--r-- | src/FundLab.Api/App.fs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/FundLab.Api/App.fs b/src/FundLab.Api/App.fs new file mode 100644 index 0000000..44628ab --- /dev/null +++ b/src/FundLab.Api/App.fs @@ -0,0 +1,53 @@ +namespace FundLab.Api + +open System +open Giraffe +open Microsoft.AspNetCore.Http + +type EmptyPortfolioResponse = + { + status: string + message: string + } + +module App = + let private unauthorized : HttpHandler = + setStatusCode 401 + >=> setHttpHeader "WWW-Authenticate" "Bearer" + >=> text "Unauthorized" + + let private requireBearer (next: HttpFunc) (ctx: HttpContext) = + let expectedToken = + Environment.GetEnvironmentVariable("FUND_LAB_AUTH_TOKEN") + |> Option.ofObj + |> Option.defaultValue "" + + let authorizationHeader = ctx.Request.Headers.Authorization.ToString() + + match Authentication.authorize expectedToken authorizationHeader with + | AuthDecision.Authorized -> next ctx + | AuthDecision.Unauthorized -> unauthorized next ctx + + let private health : HttpHandler = + json ({ + service = "fund-lab-api" + status = "ok" + } : HealthResponse) + + let private emptyPortfolio : HttpHandler = + json { + status = "empty" + message = "尚未创建基金/尚未选择投资" + } + + let createApplication () : HttpHandler = + choose [ + GET >=> route "/health" >=> health + subRoute "/api" ( + requireBearer + >=> choose [ + GET >=> route "/portfolio/summary" >=> emptyPortfolio + ] + ) + setStatusCode 404 >=> text "Not Found" + ] |
