Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created October 25, 2012 02:17
Show Gist options
  • Save prabirshrestha/3950085 to your computer and use it in GitHub Desktop.
Save prabirshrestha/3950085 to your computer and use it in GitHub Desktop.
SimpleBlog
namespace SimpleBlog
{
using System;
using System.Collections.Generic;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
public class SimpleBlog
{
public static AppFunc App(ISimpleBlogService service)
{
if (service == null)
throw new ArgumentNullException("service");
var app = new List<Func<AppFunc, AppFunc>>();
var router = new RegexRouter(app);
var renderers = new Renderers(service);
router.Get(@"^\/()$", renderers.Index);
router.Get(@"^\/()feed.(?<type>xml|json|js)$", renderers.Feed);
router.Get(@"^\/()robots.txt$", renderers.Robots);
router.Get(@"^\/([a-f0-9]{40})\/([a-z0-9_-]+)$", renderers.Article);
router.Get(@"^\/([a-f0-9]{40})\/(.+\.[a-z]{2,4})$", renderers.StaticFile);
router.Get(@"^\/()([a-z0-9_-]+)$", renderers.Article);
router.Get(@"^\/()(.+\.[a-z]{2,4})$", renderers.StaticFile);
router.Get(@"^\/()category\/([\%\.a-z0-9_-]+)$", renderers.Category);
router.All(@"*", renderers.NotFound);
return app.ToOwinApp();
}
}
}
namespace SimpleBlog
{
using System.Dynamic;
using Extensions;
using RazorEngine;
using RazorEngine.Configuration;
using RazorEngine.Templating;
using Stream;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
class Renderers
{
private readonly ISimpleBlogService service;
private readonly ITemplateService template;
public Renderers(ISimpleBlogService service)
{
this.service = service;
var templateConfig = new TemplateServiceConfiguration
{
Resolver = new DelegateTemplateResolver(service.GetTemplate),
BaseTemplateType = typeof(TemplateBaseExtensions<>),
};
this.template = new TemplateService(templateConfig);
}
public AppFunc Index(AppFunc next)
{
const string templateName = "index.cshtml";
var template = this.template.Resolve(templateName, typeof(DynamicObject));
//this.template.Compile(service.GetIndexTemplate(), typeof(DynamicObject), templateName);
return
async env =>
{
env.GetRequestHeaders()
.SetHeader("content-type", "text/html");
var blog = this.service.GetBlog();
int totalPosts;
var posts = this.service.GetPosts(1, blog.ArticlesCountPerPage, out totalPosts);
var model = new
{
Blog = blog,
Url = new
{
RequestPathBase = env.GetRequestPathBase(),
RequestPath = env.GetRequestPath()
}.ToDynamicObject(),
Posts = posts
};
string html = this.template.Run("index.cshtml", model.ToDynamicObject());
await env.GetResponseBody()
.WriteStringAsync(html);
};
}
public AppFunc Feed(AppFunc next)
{
return
async env =>
{
// http://rssjs.org/
var routeParameters = env.GetSimpleOwinRouteParameters();
var type = routeParameters["type"];
await env.GetResponseBody()
.WriteStringAsync("feed " + type);
};
}
public AppFunc Robots(AppFunc arg)
{
return
async env =>
{
await env.GetResponseBody()
.WriteStringAsync("robots.txt");
};
}
public AppFunc Article(AppFunc next)
{
return
async env =>
{
await env.GetResponseBody()
.WriteStringAsync("article");
};
}
public AppFunc StaticFile(AppFunc next)
{
return
async env =>
{
await env.GetResponseBody()
.WriteStringAsync("static file");
};
}
public AppFunc Category(AppFunc next)
{
return
async env =>
{
await env.GetResponseBody()
.WriteStringAsync("category");
};
}
public AppFunc NotFound(AppFunc next)
{
return
async env =>
{
env.SetResponseStatusCode(404);
await env.GetResponseBody()
.WriteStringAsync("not found");
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment