EmbedIO Extras

Additional Modules showing how to extend EmbedIO. Feel free to use these modules in your projects.

Bearer Token Module

Provides the ability to authenticate requests via a Bearer Token. This module creates a Token endpoint (at the predefined '/token' path) and all you need to do is provide a user validation delegate which authenticates the user. The module will create a JsonWebToken which can then be used by your client application in firther requests. The module can check all incoming requests or a predefined set of paths. The standard header in use is the HTTP Authorization header.

You can easily add Bearer Token to your EmbedIO server using a Basic Authorization Server Provider or writing your own:

// Create basic authentication provider
var basicAuthProvider = new BasicAuthorizationServerProvider();
// You can set which routes to check, empty param will secure entire server
var routes = new[] { "/secure.html" };

// Create Webserver with console logger and attach Bearer Token Module
var server = WebServer.CreateWithConsole("http://localhost:9696/");
server.RegisterModule(new BearerTokenModule(basicAuthProvider, routes));

NuGet Installation:

PM> Install-Package EmbedIO.BearerToken
NuGet version

Json Server Module

Based on the JsonServer's project, with this module you are able to simply specify a JSON file as a database and use standard REST methods to create, update, retrieve and delete records from it.

// Create Webserver with console logger and attach Json's Server
var server = WebServer.CreateWithConsole("http://localhost:9696/");
server.RegisterModule(new JsonServerModule(jsonPath: Path.Combine(@"c:\web", "database.json")));

Supported methods:

  • GET collection (//yourhost/entity)
  • GET single (//yourhost/entity/1 where 1 is the ID)
  • POST (//yourhost/entity with POST body the JSON object)
  • PUT (//yourhost/entity/1 with POST body the JSON object)
  • DELETE (//yourhost/entity/1 where 1 is the ID)

LiteLib WebAPI

Similar to Json Server Module, but you can serve a sqlite file with all HTTP verbs.

// Create Webserver and attach Json's Server
var server = WebServer.Create("http://localhost:9696/");
server.RegisterModule(new LiteLibModule<TestDbContext>(new TestDbContext(), "/dbapi/"));
                    

Supported methods:

  • GET collection (//yourhost/entity)
  • GET single (//yourhost/entity/1 where 1 is the ID)
  • POST (//yourhost/entity with POST body the JSON object)
  • PUT (//yourhost/entity/1 with POST body the JSON object)
  • DELETE (//yourhost/entity/1 where 1 is the ID)

NuGet Installation:

PM> Install-Package EmbedIO.LiteLibWebApi
NuGet version

Markdown Static Module

Markdown Static Module takes a markdown file and convert it to HTML before to response. It will accept markdown/html/htm extensions (This could be a middleware later).

// Create Webserver and attach Markdown Static Module
var server = WebServer.Create("http://localhost:9696/");
server.RegisterModule(new MarkdownStaticModule(@"c:\web"));

OWIN Integration

The support to OWIN is not under development, and it may not work correctly. EmbedIO can use the OWIN platform in two different ways:

  • You can use EmbedIO modules within an OWIN Server. In other words, host your application with an OWIN server and make use of EmbedIO modules.
public class Program
{
    /// <summary>
    /// Entry point
    /// </summary>
    /// <param name="args"></param>
    private static void Main(string[] args)
    {
        var options = new StartOptions
        {
            ServerFactory = OwinServerFactory.ServerFactoryName,
            Port = 4578
        };

        using (WebApp.Start<Startup>(options))
        {
            OwinServerFactory.Log.DebugFormat("Running a http server on port {0}", options.Port);
            Console.ReadKey();
        }
    }
}

/// <summary>
/// Startup object
/// </summary>
public class Startup
{
    /// <summary>
    /// Configure the OwinApp
    /// </summary>
    /// <param name="app"></param>
    public void Configuration(IAppBuilder app)
    {
        app.UseErrorPage();
        app.UseDirectoryBrowser();
        app.UseRazor(InitRoutes);
        // Attach a EmbedIO WebAPI Controller directly to OwinApp Configuration
        app.UseWebApi(typeof (PeopleController).Assembly);
    }
    /// <summary>
    /// Initialize the Razor files
    /// </summary>
    /// <param name="table"></param>
    public static void InitRoutes(IRouteTable table)
    {
        table
            .AddFileRoute("/about/me", "Views/about.cshtml", new { Name = "EmbedIO Razor", Date = DateTime.UtcNow });
    }
}
  • You can also use OWN middleware within EmbedIO. In other words, you can do stuff like serving Razor views from EmbedIO.
public class Program
{
    /// <summary>
    /// Entry point
    /// </summary>
    /// <param name="args"></param>
    private static void Main(string[] args)
    {

        // UseOwin() function returns Owin App for configuration
        using (var webServer = WebServer
                    .Create("http://localhost:4578")
                    .WithWebApi(typeof (PeopleController).Assembly)
                    .UseOwin((owinApp) => 
                        owinApp
                        .UseDirectoryBrowser()
                        .UseRazor(InitRoutes)))
                {
                    webServer.RunAsync();
                    Console.ReadKey();
                }
    }    
    /// <summary>
    /// Initialize the Razor files
    /// </summary>
    /// <param name="table"></param>
    public static void InitRoutes(IRouteTable table)
    {
        table
            .AddFileRoute("/about/me", "Views/about.cshtml", new { Name = "EmbedIO Razor", Date = DateTime.UtcNow });
    }
}

NuGet Installation:

PM> Install-Package EmbedIO.OWIN
NuGet version