Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
adding sinle file application
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Escolar committed May 5, 2021
1 parent fbdd135 commit 0513d59
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 4 deletions.
89 changes: 85 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RoutingRecords is a small set of tools that help Asp.Net Core developers to program cool and simple APIs in .Net 5. It relies on `record` type objects to define inline endpoints:

```csharp
public record Hello()
record Hello()
: Get("/", (req, res) =>
res.SendAsync("Welcome to RoutingRecords"));
```
Expand All @@ -19,7 +19,7 @@ Main features:
Take a look at this example:

```csharp
public record UpdateItem(IItemStore store)
record UpdateItem(IItemStore store)
: Put("items/{id:int}", async (req, res) =>
{
var id = req.FromRoute<int>("id");
Expand All @@ -37,6 +37,15 @@ public record UpdateItem(IItemStore store)

Isn't it cool?

## Why records?

The `record` keyword is a new language feature in C# 9. It declares an awesome object because:

- It is inmutable by default.
- You can declare it in a sigle line.
- You can inject external dependencies in the constructor.
- They are very readable.

## Quick start

Create a new web project:
Expand All @@ -53,7 +62,31 @@ First of all, you have to add `RoutingRecords` package reference to your project
dotnet add package RoutingRecords
```

Then you have to call `AddRouteRecords` in the `ConfigureServices` and `MapRouteRecords` in the enpoints mapping section:
Then you have two choices:

### Single source code file

Delete all .cs files and add a new one with this content:

```csharp
using RoutingRecords;

new ApiApp().Run();

record Hello() : Get("/", (req, res) =>
res.SendAsync("Hello World!"));
```

And test your application:

```bash
dotnet run
```

> **Disclaimer**: this solution is great for quick little APIs.But its use is not recommended in production applications.
### Traditional way

In the StartUp file you have to call `AddRouteRecords` in the `ConfigureServices` and `MapRouteRecords` in the enpoints mapping section:

```csharp
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -87,7 +120,7 @@ namespace SampleApp.Api
{
public record Hello()
: Get("/", (req, res) =>
res.SendAsync("Welcome to RoutingRecords"));
res.SendAsync("Hello World!"));
}
```

Expand Down Expand Up @@ -175,6 +208,54 @@ HttpResponse Status(HttpStatusCode statusCode);
- **JsonAsync**: Writes an objet serialized as json in the response body. If you don't specify `cancellationToken` it uses the current `HttpContext.RequestAborted` value.
- **Status**: Sets the response HTTP status code.

## ApiApp

It is an object that creates a web application host with a default confiuration. It allows you write a fast single file API.

When you write:

```csharp
new ApiApp()
.ConfigureServices(services => /* Configure your services */)
.Configure( (app, env) => /* Confiure your application */)
.Run();
```

Is the same than:

```csharp
CreateHostBuilder(args).Build().Run();

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

class Startup
{
public void ConfigureServices(IServiceCollection services)
{
/* Configure your services */
services.AddRouteRecords();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

/* Confiure your application */

app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapRouteRecords());
}
}
```

## Authorization

RoutingRoutes is full integrated with Asp.Net Authorization framework. You can add a global policy with an authorization requirement for all enpoints:
Expand Down
15 changes: 15 additions & 0 deletions RoutingRecords.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoutingRecords.UnitTests",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizedApp", "samples\AuthorizedApp\AuthorizedApp.csproj", "{24ACA220-28EE-4013-87C2-7EE82E0907E9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SingleFileApi", "samples\SingleFileApi\SingleFileApi.csproj", "{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -150,6 +152,18 @@ Global
{24ACA220-28EE-4013-87C2-7EE82E0907E9}.Release|x64.Build.0 = Release|Any CPU
{24ACA220-28EE-4013-87C2-7EE82E0907E9}.Release|x86.ActiveCfg = Release|Any CPU
{24ACA220-28EE-4013-87C2-7EE82E0907E9}.Release|x86.Build.0 = Release|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Debug|x64.ActiveCfg = Debug|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Debug|x64.Build.0 = Debug|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Debug|x86.ActiveCfg = Debug|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Debug|x86.Build.0 = Debug|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Release|Any CPU.Build.0 = Release|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Release|x64.ActiveCfg = Release|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Release|x64.Build.0 = Release|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Release|x86.ActiveCfg = Release|Any CPU
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{FF607DE7-B7A2-4B01-B650-D2D5209489B4} = {429EEE00-AB74-11EB-9B8F-3F35E74019DC}
Expand All @@ -161,5 +175,6 @@ Global
{DF29C4B6-5A3B-4555-A36C-8F5357305266} = {4AA93010-AB74-11EB-9B8F-3F35E74019DC}
{6688E2FA-A23A-43A3-896C-768D360B49E5} = {4AA93010-AB74-11EB-9B8F-3F35E74019DC}
{24ACA220-28EE-4013-87C2-7EE82E0907E9} = {468A31F0-AB74-11EB-9B8F-3F35E74019DC}
{BD9F6F41-0CD8-4BC6-8FC8-3D52E0E07A46} = {468A31F0-AB74-11EB-9B8F-3F35E74019DC}
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions samples/SingleFileApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using RoutingRecords;

new ApiApp().Run();

record Hello()
: Get("/", (req, res) =>
res.SendAsync("Welcome to one file RoutingRecords"));
11 changes: 11 additions & 0 deletions samples/SingleFileApi/SingleFileApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\RoutingRecords\RoutingRecords.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions samples/SingleFileApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
10 changes: 10 additions & 0 deletions samples/SingleFileApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}

0 comments on commit 0513d59

Please sign in to comment.