Mastersign.MicroHttpServer
0.1.0
dotnet add package Mastersign.MicroHttpServer --version 0.1.0
NuGet\Install-Package Mastersign.MicroHttpServer -Version 0.1.0
<PackageReference Include="Mastersign.MicroHttpServer" Version="0.1.0" />
paket add Mastersign.MicroHttpServer --version 0.1.0
#r "nuget: Mastersign.MicroHttpServer, 0.1.0"
// Install Mastersign.MicroHttpServer as a Cake Addin #addin nuget:?package=Mastersign.MicroHttpServer&version=0.1.0 // Install Mastersign.MicroHttpServer as a Cake Tool #tool nuget:?package=Mastersign.MicroHttpServer&version=0.1.0
Mastersign.MicroHttpServer
A very lightweight & simple embedded HTTP server for C#
This library is a derivative of the µHttpSharp by jcaillon, which is a fork of µHttpSharp. This project has a large number of forks. It seems a lot of folks like to have their own customized tiny embedded HTTP server 🙂
In that tradition, Mastersign.MicroHttpServer is another twist on the subject.
Goals
- .NET Standard 2.0 as only requirement and therefore, usable in .NET Framework ≥ 4.6.1 and .NET ≥ 5
- SSL/TLS support
- Reasonably fast
- Full support for asynchronous handlers
- Fluent API with composable routing inspired by ExpressJS, koa, and others
- Class and function handlers
- Heavily overloaded methods for a variety of simple use cases
Limitations
- Only basic HTTP features
- NOT battle tested
- Only recommended for non-public services
Example
The most simple example, binding to http://127.0.0.1:8080:
using Mastersign.MicroHttpServer;
using var svr = new HttpServer().ListenToLoopback();
svr.Get("/", "Hello World!");
svr.Start();
Console.WriteLine("Press ESC to stop...");
while (Console.ReadKey(true).Key != ConsoleKey.Escape) { }
Another example with a couple of different ways of defining routes and sub-routes:
using Mastersign.MicroHttpServer;
using var svr = new HttpServer()
.LogToConsole(LogLevel.Debug)
.ListenTo("127.0.0.1", 8080);
svr
// register default middleware for simple exception report
.Use(new ExceptionHandler())
// register middleware for logging request timing
.Use(new TimingMiddleware(LogLevel.Information))
// register middleware for content compression
.Use(new CompressionMiddelware(new GZipCompressor(), new DeflateCompressor()))
// register GET handler, returning a constant string
.Get("/", "Index")
// register GET handler, returning text composed by using query arguments
.Get("about", ctx => $"About (Locale = {ctx.Request.Query.GetByNameOrDefault("l", "en")})")
// register HTTP method agnostic handler, redirecting to another route
.UseWhen("redirect", (ctx, _) => ctx.RedirectTemporarily("about"))
// start a branch /files/...
.Branch("files")
// registering a static file handler for all sub-routes
.GetAll(new FileHandler(@"F:\"))
// end the branch with a 404 for everything, the file handler handle
.EndWith(new NotFoundHandler())
// start another branch /api/...
.Branch("api")
// register an anonymous delegate as handler, responding with a constant text
.Get("info", (ctx, _) => ctx.Respond(StringHttpResponse.Text("Info")))
// end the branch by responding with a constant string to anything other then /api/info
.EndWith("API")
// register a route with a placeholder
.Get("item/{No}", ctx => $"Item No. {ctx.RouteParameters.GetByName("No")}")
// register an app (sub-router) under a prefix route
.UseWhen("my-app", MyApp())
// register fallback handler
.EndWith(new NotFoundHandler());
svr.Start();
Console.WriteLine("Press ESC to stop...");
while (Console.ReadKey(true).Key != ConsoleKey.Escape) { }
static HttpApp MyApp()
{
var app = new HttpApp("my app");
app
.Post("/", async ctx => {
string text;
using (var r = new StreamReader(ctx.Request.ContentStream))
{
text = await r.ReadToEndAsync();
}
return "<h1>Got It!</h1><pre><code>" + text + "</code></pre>";
})
.Get("/", "My App");
return app;
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.0 | 126 | 7/2/2024 |