Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinmoris committed Nov 30, 2017
2 parents 415c642 + 2ced774 commit 47b7f9e
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 259 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: csharp
sudo: required
dist: trusty

dotnet: 2.0.0
dotnet: 2.0.3
mono:
- 4.6.1
- 4.8.1
Expand Down
44 changes: 39 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The old NuGet package has been unlisted and will no longer receive any updates.
- [htmlFile](#htmlfile)
- [renderHtml](#renderhtml)
- [redirectTo](#redirectto)
- [portRoute](#portroute)
- [warbler](#warbler)
- [Additional HttpHandlers](#additional-httphandlers)
- [Giraffe.Razor](#girafferazor)
Expand Down Expand Up @@ -858,6 +859,34 @@ let app =
]
```

### portRoute

If your web server is listening to multiple ports through `WebHost.UseUrls` then you can use the `portRoute` HttpHandler to easily filter incoming requests based on their port by providing a list of port number and HttpHandler (`(int * HttpHandler) list`).

#### Example
```fsharp
let app9001 =
router notFound [
GET [
route "/index1" => text "index page1" ]
]
let app9002 =
router notFound [
POST [
subRoute "/api2" [
route "/newpassword2" => text "newpassword2" ]
]
]
let app = portRoute [
(9001, app9001)
(9002, app9002)
]
```

### warbler

If your route is not returning a static response, then you should wrap your function with a warbler.
Expand Down Expand Up @@ -1060,6 +1089,8 @@ When using the `Giraffe.TokenRouter` module the main routing functions have been

The `route` and `routef` handlers work the exact same way as before, except that the continuation handler needs to be enclosed in parentheses or captured by the `<|` or `=>` operators.

The http handlers `GET`, `POST`, `PUT` and `DELETE` are functions which take a list of nested http handler functions similar to before.

The `subRoute` handler has been altered in order to accept an additional parameter of child routing functions. All child routing functions will presume that the given sub path has been prepended.

### Example:
Expand All @@ -1074,12 +1105,15 @@ let app =
route "/about" => text "about"
routef "parsing/%s/%i" (fun (s,i) -> text (sprintf "Recieved %s & %i" s i))
subRoute "/api" [
route "/" <| text "api index"
route "/about" (text "api about")
subRoute "/v2" [
route "/" <| text "api v2 index"
route "/about" (text "api v2 about")
GET [
route "/" <| text "api index"
route "/about" (text "api about")
subRoute "/v2" [
route "/" <| text "api v2 index"
route "/about" (text "api v2 about")
]
]
]
]
```
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"projects": [ "src", "tests" ],
"sdk": {
"version": "2.0.0"
"version": "2.0.3"
}
}
2 changes: 1 addition & 1 deletion src/Giraffe/Giraffe.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<AssemblyName>Giraffe</AssemblyName>
<Version>0.1.0-beta-300</Version>
<Version>0.1.0-beta-310</Version>
<Description>A native functional ASP.NET Core web framework for F# developers.</Description>
<Copyright>Copyright 2017 Dustin Moris Gorski</Copyright>
<NeutralLanguage>en-GB</NeutralLanguage>
Expand Down
16 changes: 15 additions & 1 deletion src/Giraffe/HttpHandlers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,22 @@ let negotiate (responseObj : obj) : HttpHandler =
// response object
responseObj

///Redirects to a different location with a 302 or 301 (when permanent) HTTP status code.
/// Redirects to a different location with a 302 or 301 (when permanent) HTTP status code.
let redirectTo (permanent : bool) (location : string) : HttpHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
ctx.Response.Redirect(location, permanent)
Task.FromResult (Some ctx)

/// Filters an incoming HTTP request based on the port.
let portRoute (fns : (int * HttpHandler) list) : HttpHandler =
fun next ->
let portMap = Dictionary<_,_>(fns.Length)
fns |> List.iter (fun (p, h) -> portMap.Add(p, h next))
fun (ctx : HttpContext) ->
let port = ctx.Request.Host.Port
if port.HasValue then
match portMap.TryGetValue port.Value with
| true , func -> func ctx
| false, _ -> abort
else
abort
Loading

0 comments on commit 47b7f9e

Please sign in to comment.