Giter Site home page Giter Site logo

legendaryb / cherry Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 72 KB

A simple http server implementation with a few goodies built around NET's HttpListener.

License: MIT License

C# 100.00%
csharp dotnet http http-server net60 rest-api rest-server

cherry's Introduction

Cherry

forthebadge forthebadge

GitHub license Nuget

A simple http server implementation with a few goodies built around NET's HttpListener.


๐ŸŽฏ Features

  • 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

๐Ÿ“ Usage

namespace Cherry.ConsoleApp
{
    public class User
    {
        public string Name => "Daniel";
        public string Mail => "[email protected]";
    }

    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();
        }
    }
}

cherry's People

Contributors

legendaryb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.