Red
vements
Un marco de aplicación web .NET Standard construido sobre ASP.NET Core con Kestrel e inspirado en la simplicidad de Express.js
RedHttpServer se puede instalar desde NuGet: Install-Package RHttpServer
RedHttpServer está creado para que sea fácil de construir. El servidor admite tanto módulos de middleware como módulos de extensión.
Más extensiones y middleware
var server = new RedHttpServer ( 5000 , "public" ) ;
server . Use ( new EcsRenderer ( ) ) ;
server . Use ( new CookieSessions < MySession > ( TimeSpan . FromDays ( 1 ) ) ) ;
// Authentication middleware
async Task < HandlerType > Auth ( Request req , Response res )
{
if ( req . GetData < MySession > ( ) != null )
{
return HandlerType . Continue ;
}
await res . SendStatus ( HttpStatusCode . Unauthorized ) ;
return HandlerType . Final ;
}
var startTime = DateTime . UtcNow ;
// Url parameters
server . Get ( "profile/:username" , Auth , ( req , res ) =>
{
var username = req . Context . ExtractUrlParameter ( "username" ) ;
// ... lookup username in database or similar and fetch profile ...
var user = new { FirstName = "John" , LastName = "Doe" , Username = username } ;
return res . SendJson ( user ) ;
} ) ;
// Using forms
server . Post ( "/login" , async ( req , res ) =>
{
var form = await req . GetFormDataAsync ( ) ;
// ... some validation and authentication ...
await res . OpenSession ( new MySession { Username = form [ "username" ] } ) ;
return await res . SendStatus ( HttpStatusCode . OK ) ;
} ) ;
server . Post ( "/logout" , Auth , async ( req , res ) =>
{
var session = req . GetData < MySession > ( ) ;
await res . CloseSession ( session ) ;
return await res . SendStatus ( HttpStatusCode . OK ) ;
} ) ;
// Simple redirects
server . Get ( "/redirect" , Auth , ( req , res ) => res . Redirect ( "/redirect/test/here" ) ) ;
// File uploads
Directory . CreateDirectory ( "uploads" ) ;
server . Post ( "/upload" , async ( req , res ) =>
{
if ( await req . SaveFiles ( "uploads" ) )
return await res . SendString ( "OK" ) ;
else
return await res . SendString ( "Error" , status : HttpStatusCode . NotAcceptable ) ;
} ) ;
server . Get ( "/file" , ( req , res ) => res . SendFile ( "somedirectory/animage.jpg" ) ) ;
// Using url queries
server . Get ( "/search" , Auth , ( req , res ) =>
{
string searchQuery = req . Queries [ "query" ] ;
string format = req . Queries [ "format" ] ;
// ... perform search using searchQuery and return results ...
var results = new [ ] { "Apple" , "Pear" } ;
if ( format == "xml" )
return res . SendXml ( results ) ;
else
return res . SendJson ( results ) ;
} ) ;
// Markdown rendering
server . Get ( "/markdown" , ( req , res ) => res . RenderFile ( "markdown.md" ) ) ;
// Esc rendering
server . Get ( "/serverstatus" , async ( req , res ) => await res . RenderPage ( "pages/statuspage.ecs" ,
new RenderParams
{
{ "uptime" , ( int ) DateTime . UtcNow . Subtract ( startTime ) . TotalSeconds } ,
{ "version" , Red . RedHttpServer . Version }
} ) ) ;
// Using websockets
server . WebSocket ( "/echo" , async ( req , wsd ) =>
{
wsd . SendText ( "Welcome to the echo test server" ) ;
wsd . OnTextReceived += ( sender , eventArgs ) => { wsd . SendText ( "you sent: " + eventArgs . Text ) ; } ;
return HandlerType . Final ;
} ) ;
// Keep the program running easily (async Program.Main - C# 7.1+)
await server . RunAsync ( ) ;
Porque me gusta C# y .NET Core, pero muy a menudo necesito un servidor web simple pero potente para algún proyecto. Express.js es conciso y sencillo de utilizar, por lo que la API se inspira en eso.
RedHttpServer se publica bajo licencia MIT, por lo que su uso es gratuito, incluso en proyectos comerciales.