-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
225 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
open System | ||
open Microsoft.AspNetCore.Builder | ||
open Microsoft.AspNetCore.Http | ||
open Microsoft.Extensions.DependencyInjection | ||
open Microsoft.Extensions.Hosting | ||
open Giraffe | ||
open Giraffe.EndpointRouting | ||
|
||
let expensiveOperation () : DateTime = | ||
let fiveSeconds = 5000 // ms | ||
Threading.Thread.Sleep fiveSeconds | ||
DateTime.Now | ||
|
||
let dateTimeHandler : HttpHandler = | ||
fun (_next : HttpFunc) (ctx : HttpContext) -> | ||
let now = expensiveOperation () | ||
ctx.WriteTextAsync $"Hello World -> DateTime: {now}" | ||
|
||
let endpoints: Endpoint list = | ||
[ | ||
subRoute "/cached" [ | ||
GET [ | ||
route "/public" (publicResponseCaching 30 None >=> dateTimeHandler) | ||
route "/private" (privateResponseCaching 30 None >=> dateTimeHandler) | ||
route "/not" (noResponseCaching >=> dateTimeHandler) | ||
] | ||
] | ||
] | ||
|
||
let notFoundHandler = | ||
"Not Found" | ||
|> text | ||
|> RequestErrors.notFound | ||
|
||
let configureServices (services : IServiceCollection) = | ||
services | ||
.AddRouting() | ||
.AddResponseCaching() | ||
.AddGiraffe() | ||
|> ignore | ||
|
||
let configureApp (appBuilder : IApplicationBuilder) = | ||
appBuilder | ||
.UseRouting() | ||
.UseResponseCaching() | ||
.UseEndpoints(fun e -> e.MapGiraffeEndpoints(endpoints)) | ||
.UseGiraffe(notFoundHandler) | ||
|
||
[<EntryPoint>] | ||
let main args = | ||
let builder = WebApplication.CreateBuilder(args) | ||
configureServices builder.Services | ||
|
||
let app = builder.Build() | ||
|
||
if app.Environment.IsDevelopment() then | ||
app.UseDeveloperExceptionPage() |> ignore | ||
|
||
configureApp app | ||
app.Run() | ||
|
||
0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Response Caching App | ||
|
||
The purpose of this sample is to show how one can configure the Giraffe server to use the ASP.NET [response caching](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/response?view=aspnetcore-7.0) feature. Notice that we're leveraging the middlewares which are offered by Giraffe. | ||
|
||
You can find their documentation here: [Giraffe Docs - Response Caching](https://giraffe.wiki/docs#response-caching). | ||
|
||
## How to test | ||
|
||
First, start the server at the terminal using: | ||
|
||
```bash | ||
# Assuming that you're at the top level of this repository | ||
dotnet run --project samples/ResponseCachingApp/ | ||
|
||
# It will start the server listening to port 5000 | ||
``` | ||
|
||
Now, you can use the `test-run.sh` script (Linux): | ||
|
||
```bash | ||
# Add execution permission to the script | ||
chmod +x samples/ResponseCachingApp/test-run.sh | ||
|
||
./samples/ResponseCachingApp/test-run.sh | ||
``` | ||
|
||
And the expected result: | ||
|
||
```bash | ||
# ----------------------------------- | ||
# Testing the /cached/not endpoint | ||
|
||
# Hello World -> DateTime: 8/29/2023 8:00:28 PM | ||
# Hello World -> DateTime: 8/29/2023 8:00:34 PM | ||
# Hello World -> DateTime: 8/29/2023 8:00:40 PM | ||
# Hello World -> DateTime: 8/29/2023 8:00:46 PM | ||
# Hello World -> DateTime: 8/29/2023 8:00:52 PM | ||
|
||
# real 0m30,126s | ||
# user 0m0,063s | ||
# sys 0m0,052s | ||
# ----------------------------------- | ||
# Testing the /cached/public endpoint | ||
|
||
# Hello World -> DateTime: 8/29/2023 8:00:58 PM | ||
# Hello World -> DateTime: 8/29/2023 8:00:58 PM | ||
# Hello World -> DateTime: 8/29/2023 8:00:58 PM | ||
# Hello World -> DateTime: 8/29/2023 8:00:58 PM | ||
# Hello World -> DateTime: 8/29/2023 8:00:58 PM | ||
|
||
# real 0m10,072s | ||
# user 0m0,025s | ||
# sys 0m0,040s | ||
# ----------------------------------- | ||
# Testing the /cached/private endpoint | ||
|
||
# Hello World -> DateTime: 8/29/2023 8:01:09 PM | ||
# Hello World -> DateTime: 8/29/2023 8:01:15 PM | ||
# Hello World -> DateTime: 8/29/2023 8:01:21 PM | ||
# Hello World -> DateTime: 8/29/2023 8:01:27 PM | ||
# Hello World -> DateTime: 8/29/2023 8:01:33 PM | ||
|
||
# real 0m30,120s | ||
# user 0m0,052s | ||
# sys 0m0,060s | ||
``` | ||
|
||
Notice that at this example, the cache worked only for the `/cached/public` endpoint, as expected. You can read the documentation presented before to understand why. | ||
|
||
One last information, notice that the server will inform whenever the response was cached or not, just check the logs. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Program.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../../src/Giraffe/Giraffe.fsproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
# ============================================== | ||
# Warning: | ||
# | ||
# Make sure that the server is already running in a different terminal. | ||
|
||
function test_not_cached { | ||
for counter in {1..5} | ||
do | ||
curl localhost:5000/cached/not | ||
echo | ||
sleep 1 | ||
done | ||
} | ||
|
||
function test_public_cached { | ||
for counter in {1..5} | ||
do | ||
curl localhost:5000/cached/public | ||
echo | ||
sleep 1 | ||
done | ||
} | ||
|
||
function test_private_cached { | ||
for counter in {1..5} | ||
do | ||
curl localhost:5000/cached/private | ||
echo | ||
sleep 1 | ||
done | ||
} | ||
|
||
echo "-----------------------------------" | ||
echo "Testing the /cached/not endpoint" | ||
echo | ||
time test_not_cached | ||
|
||
|
||
echo "-----------------------------------" | ||
echo "Testing the /cached/public endpoint" | ||
echo | ||
time test_public_cached | ||
|
||
echo "-----------------------------------" | ||
echo "Testing the /cached/private endpoint" | ||
echo | ||
time test_private_cached |