A simple http server implementation with a few goodies built around NET's HttpListener.
- Built around .NET's HttpListener
- Setup of the
HttpServer
instance via a fluent api
- Lightweight. The only dependency is
Microsoft.Extensions.Logging.Abstractions
to make logging of internal events possible
- Built-in
HttpRouter
is interchangeable by implementing the interface IHttpRouter
and calling HttpServer.Use<MyRouter>()
- HTTP Controller support (limited to
POST
, GET
, PUT
, DELETE
, but extendable)
- Use
Func<THttpRequest, THttpRespoonse, Task>
to implement controllers
- Middleware support
- Use
Func<THttpContext, Task>
to implement custom middlewares
namespace Cherry.ConsoleApp
{
public class User
{
public string Name => "Daniel";
public string Mail => "daniel@github.com";
}
public class JsonContentTypeMiddleware : IMiddleware
{
public Task HandleRequestAsync(
HttpListenerRequest req,
HttpListenerResponse res)
{
res.ContentType = MediaTypeNames.Application.Json;
return Task.CompletedTask;
}
}
public class HelloWorldController : HttpController
{
public override Task HandleGetAsync(HttpListenerRequest req, HttpListenerResponse res)
{
return res.AnswerWithStatusCodeAsync(
"Hello World!",
HttpStatusCode.OK);
}
}
[Route("/api/v1/users")]
public class UserController : HttpController
{
public override Task HandleGetAsync(HttpListenerRequest req, HttpListenerResponse res)
{
return base.HandleGetAsync(req, res);
}
}
public class Program
{
static async Task Main()
{
var server = new HttpServer(NullLogger<HttpServer>.Instance, "http://localhost:8081/")
.RegisterMiddleware<JsonContentTypeMiddleware>() // installs a global middleware because no specific route was provided
.RegisterController<HelloWorldController>("/api/v1/hello") // Path must be provided because the class is not decorated with the route attribute
.RegisterController<UserController>(); // No path must be provided, because the class is decorated with the Route attribute
// server.AutoRegisterControllers(); // Can be used to automatically discover controllers from a given assembly. Uses reflection and ignores all types which are not decorated with the Route attribute and inheriting from the HttpController base class
await server.RunAsync();
}
}
}