OoLunar.HyperSharp
0.3.0
dotnet add package OoLunar.HyperSharp --version 0.3.0
NuGet\Install-Package OoLunar.HyperSharp -Version 0.3.0
<PackageReference Include="OoLunar.HyperSharp" Version="0.3.0" />
paket add OoLunar.HyperSharp --version 0.3.0
#r "nuget: OoLunar.HyperSharp, 0.3.0"
// Install OoLunar.HyperSharp as a Cake Addin #addin nuget:?package=OoLunar.HyperSharp&version=0.3.0 // Install OoLunar.HyperSharp as a Cake Tool #tool nuget:?package=OoLunar.HyperSharp&version=0.3.0
HyperSharp Overview
HyperSharp is a C# implementation of the HTTP 1.1 protocol. It's designed with emphasis on speed, lightweight nature, and most importantly: user-friendliness.
To get started, you can install the NuGet package and follow the setup instructions.
To request support, you may open up a new GitHub issue, discussion or join the Discord.
To report bugs or request new features, please use GitHub issues.
Lastly, all API documentation and tutorials are available on the website, which is generated from the latest commit at the docs folder.
Table of Contents
Core Concept: Responders
- The foundation of HyperSharp relies on the concept of responders.
- A responder consists of a list of delegates.
- Each delegate is a function that takes a request and produces a response.
- Both request and response are generic types, allowing customization.
- All responders and responder dependencies are executed sequentially.
- If any delegate returns an error or value, all subsequent delegates are skipped and the response is returned.
Response Handling
- Responders use results and errors for handling responses.
- Synchronous (
void
) and asynchronous (Task
/ValueTask
) execution modes are supported, with no additional setup required from the user.
Setup
There are two ways to setup HyperSharp, depending on your needs. The recommended way is to use dependency injection, through IServiceCollection
:
serviceCollection.AddHyperSharp((serviceProvider, hyperConfiguration) =>
{
IConfiguration configuration = serviceProvider.GetRequiredService<IConfiguration>();
string? host = configuration.GetValue("server:address", "localhost")?.Trim();
if (!IPAddress.TryParse(host, out IPAddress? address))
{
IPAddress[] addresses = Dns.GetHostAddresses(host);
address = addresses.Length != 0 ? addresses[0] : throw new InvalidOperationException("The server address could not be resolved to an IP address.");
}
hyperConfiguration.ListeningEndpoint = new IPEndPoint(address, configuration.GetValue("server:port", 8080));
hyperConfiguration.AddResponders(new[] { typeof(HelloWorldResponder) });
});
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
HyperServer hyperServer = serviceProvider.GetRequiredService<HyperServer>();
hyperServer.Start();
We recommend using the service collection method because the responders use dependency injection to resolve their dependencies. This allows for a more modular and testable design.
However, we understand that not everyone wants to use dependency injection. If you don't want to use dependency injection, you can use the HyperServer
class directly:
HyperServer hyperServer = new(new HyperConfiguration(new HyperConfigurationBuilder()
{
ListeningEndpoint = new IPEndPoint(IPAddress.Loopback, 8080),
Responders = new[] { typeof(HelloWorldResponder) }
}));
hyperServer.Start();
Example: Hello World
using System;
using System.Threading;
using HyperSharp.Protocol;
using HyperSharp.Responders;
using HyperSharp.Results;
public class HelloWorldResponder : IResponder<HyperContext, HyperStatus>
{
// Specifies any required types for this responder (empty in this case)
public static Type[] Needs => Type.EmptyTypes;
// Generates a response indicating success with a message "Hello World!"
public Result<HyperStatus> Respond(HyperContext context, CancellationToken cancellationToken = default) => Result.Success(HyperStatus.OK("Hello World!"));
}
This example demonstrates the structure of a responder using HyperSharp to create a "Hello World!" response.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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 is compatible. 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. |
-
net7.0
- Microsoft.Extensions.Logging (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- Microsoft.Toolkit.HighPerformance (>= 7.1.2)
- OoLunar.HyperSharp.Responders (>= 0.3.0)
- OoLunar.HyperSharp.Results (>= 0.3.0)
- System.IO.Pipelines (>= 7.0.0)
- Ulid (>= 1.3.0)
-
net8.0
- Microsoft.Extensions.Logging (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- Microsoft.Toolkit.HighPerformance (>= 7.1.2)
- OoLunar.HyperSharp.Responders (>= 0.3.0)
- OoLunar.HyperSharp.Results (>= 0.3.0)
- System.IO.Pipelines (>= 7.0.0)
- Ulid (>= 1.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.