Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ports in EndpointRouting #491

Open
pbiggar opened this issue Sep 2, 2021 · 3 comments
Open

Support ports in EndpointRouting #491

pbiggar opened this issue Sep 2, 2021 · 3 comments

Comments

@pbiggar
Copy link

pbiggar commented Sep 2, 2021

If seems right now that there is no way to add ports during endpoint routing. AFAICT, it should be straightforward to add by calling RequireHost($"*:{port}").

I want to ensure that my main routes (including fallbacks, authorization, redirects, etc) are not in the routing map for http calls to my control plane (healthcheck, kubernetes actions, etc) which are on a different port.

@Banashek
Copy link
Contributor

Banashek commented Jul 3, 2022

Would authorizeRequest work here?

let webApp =
    choose [
        route "/ping"   >=> text "pong"
        route "/"       >=> htmlView indexView
        authorizeRequest 
            (fun x -> x.Request.Host.Port = 2000) 
            (text "failed") >=> 
                route "/healthcheck" >=> text "healthy"]

@pbiggar
Copy link
Author

pbiggar commented Jul 4, 2022

I'm not using Giraffe anymore, though it looks to me that that wouldn't compile down to the efficient EndpointRouting stuff.

@Banashek
Copy link
Contributor

Banashek commented Jul 4, 2022

You're correct. That would not compile down (I was using the default routing).

Here's a version that does:

let validateApiKey (ctx : HttpContext) =
    match ctx.TryGetRequestHeader "X-API-Key" with
    | Some key -> "super-sercret-key".Equals key
    | None     -> false

let accessDenied   = setStatusCode 401 >=> text "Access Denied"
let requiresApiKey =
    authorizeRequest validateApiKey accessDenied


let endpoints : Endpoint list =
    [
        GET [
            route "/ping" (text "pong")
            route "/"     (htmlView indexView)
            route "/healthCheck" (requiresApiKey >=> (text "healthy"))]]

image

I assume swapping in a port check would work fine:

let validatePort (ctx : HttpContext) = ctx.Request.Host.Port = 2000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants