https://github.com/horse-framework/horse-http
Lightweight and fast HTTP Server with MVC Architecture
https://github.com/horse-framework/horse-http
http http-server mvc-framework
Last synced: 2 months ago
JSON representation
Lightweight and fast HTTP Server with MVC Architecture
- Host: GitHub
- URL: https://github.com/horse-framework/horse-http
- Owner: horse-framework
- License: apache-2.0
- Created: 2020-07-12T16:49:49.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-23T10:08:00.000Z (over 1 year ago)
- Last Synced: 2025-02-04T06:35:36.794Z (11 months ago)
- Topics: http, http-server, mvc-framework
- Language: C#
- Homepage:
- Size: 182 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Horse HTTP
[](https://www.nuget.org/packages/Horse.Mvc)
[](https://www.nuget.org/packages/Horse.Mvc.Auth.Jwt)
[](https://www.nuget.org/packages/Horse.Protocols.Http)
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();
});
mvc.Use(app =>
{
app.UseMiddleware();
});
//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 Get([FromRoute] int? id)
{
return StringAsync("Hello world: " + id);
}
[HttpPost("get2")]
public async Task 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 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.
[](https://www.jetbrains.com/?from=twino-framework)