Skip to content
/ Hydra Public

A simple streaming webserver that runs anywhere .NET 6 runs

License

Notifications You must be signed in to change notification settings

raftario/Hydra

Repository files navigation

Hydra

A simple streaming webserver that runs anywhere .NET 6 runs

Why

ASP.NET Core doesn't support running on MAUI targets (Android, iOS, etc.). It's also pretty bulky and, in my opinion, extremely annoying to use for small to medium sized projects. After trying alternatives for a project without finding anyting satisfying, a friend made a joke about writing our own. Two days of hyperfocusing later I'm writing this readme.

Unlike ASP, Hydra is very simple and barebones. I like to think it exists at an abstraction level where it's both usable as is and for building frameworks upon. However, it doesn't support HTTP/2.0 and is nowhere near as feature packed as ASP.

Unlike most alternatives, Hydra is a fully streaming server. Request and response bodies are streams, and handling can start as soon as the method and URI parsed without waiting for the full headers and body. Under the hood, it uses a streaming parser based on httparse and is built on top of System.IO.Pipelines.

Finally, Hydra is unsurprising. It never tries to be smart and only does the bare minimum required to follow the spec.

Features

  • Simple and lightweight
  • Truly cross-platform
  • Streaming request and response bodies
  • Streaming request headers
  • TLS encryption
  • Request pipelining
  • Chunked encoding
  • Unix socket support
  • WebSockets (in progress)

Usage

You can take a look at usage examples in the example project and run them using dotnet run --project Hydra.Example -- <EXAMPLE> [<HOSTNAME>] [<PORT>]

Basic

using Hydra;
using System.Text;

using var server = await Server.At("localhost", 8080, async (req) =>
{
    var hello = "Hello, Hydra!";
    var body = new MemoryStream(Encoding.UTF8.GetBytes(hello));
    var res = new HttpResponse(200, body);
    return res;
});
await server.Run();

TLS

using var server = await Server.At(hostname, port, handler);
await server.Tls("./tls/cert.pem", "./tls/key.rsa");
await server.Run();

Unix

var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
socket.Bind(new UnixDomainSocketEndPoint(path));
socket.Listen();

using var server = new Server(socket, handler);
await server.Run();

About

A simple streaming webserver that runs anywhere .NET 6 runs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages