/horse-http

Lightweight and fast HTTP Server with MVC Architecture

Primary LanguageC#Apache License 2.0Apache-2.0

Horse HTTP

NuGet NuGet NuGet

Horse HTTP is an HTTP Server implementation of Horse Server. Horse MVC is MVC architecture layer of http server. It's usage is very similar to ASP.NET Core and faster. You can also ise core Protocols. Http library for basic HTTP server without MVC.

Basic Example

class Program
{
    static void Main(string[] args)
    {
        HorseMvc mvc = new HorseMvc();
        mvc.Init(m =>
        {
            //initialization
            m.Services.AddTransient<ISampleService, SampleService>();
        });
        mvc.Use(app =>
        {
            app.UseMiddleware<SampleMiddleware>();
        });

        //create new twino server
        HorseServer server = new HorseServer();
        
        //bind MVC to the server
        server.UseMvc(mvc, HttpOptions.CreateDefault());
        
        //start server
        server.Run();
    }
}

[Route("[controller]")]
public class DemoController : HorseController
{
    [HttpGet("get/{?id}")]
    public Task<IActionResult> Get([FromRoute] int? id)
    {
        return StringAsync("Hello world: " + id);
    }

    [HttpPost("get2")]
    public async Task<IActionResult> Get2([FromBody] CustomModel model)
    {
        return await JsonAsync(new {Message = "Hello World Json"});
    }
}

JWT Example

class Program
{
    static void Main(string[] args)
    {
        HorseMvc mvc = new HorseMvc();
        mvc.Init(m =>
        {
            //initialization
            mvc.AddJwt(o =>
            {
                o.Audience = "your_company";
                o.Issuer = "yoursite.com";
                o.ValidateAudience = false;
                o.ValidateIssuer = true;
                o.ValidateLifetime = true;
                o.Key = "your_secret";
                o.Lifetime = TimeSpan.FromHours(1);
            });
        });
        
        mvc.Use(app =>
        {
            app.UseMiddleware(cors);
        });

        //create new horse server
        HorseServer server = new HorseServer();
        
        //bind MVC to the server
        server.UseMvc(mvc, HttpOptions.CreateDefault());
        
        //start server
        server.Run();
    }
}

[Route("[controller]")]
public class DemoController : HorseController
{
    [HttpGet]
    [Authorize]
    public Task<IActionResult> Get()
    {
        //you can use User property of HorseController
        
        return StringAsync("Hello, World!");
    }
}

CORS and Some Useful Options

class Program
{
    static void Main(string[] args)
    {
        HorseMvc mvc = new HorseMvc();
        
        mvc.Init();
        
        //create cors service and allow all (not related to jwt)
        CorsMiddleware cors = new CorsMiddleware();
        cors.AllowAll();
        
        mvc.Use(app =>
        {
            app.UseMiddleware(cors);
        });

        //use global result for internal errors
        mvc.StatusCodeResults.Add(HttpStatusCode.InternalServerError, new JsonResult(new SampleResult {Ok = false, Code = "Error"}));
        
        //use global result for unauthorized results
        mvc.StatusCodeResults.Add(HttpStatusCode.Unauthorized, new JsonResult(new SampleResult {Ok = false, Code = "Unauthorized"}));

        //use custom handler for errors (CustomErrorHandler implements IErrorHandler)
        mvc.ErrorHandler = new CustomErrorHandler();
        
        //create new twino server
        HorseServer server = new HorseServer();
        
        //bind MVC to the server
        server.UseMvc(mvc, HttpOptions.CreateDefault());
        
        //start server
        server.Run();
    }
}

Thanks

Thanks to JetBrains for open source license to use on this project.

jetbrains