Created
November 29, 2011 03:34
-
-
Save csainty/1403282 to your computer and use it in GitHub Desktop.
Blog Post - Nancy + Mongo + AppHarbor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using MongoDB.Driver; | |
using Nancy; | |
using NancyMongo.Models; | |
namespace NancyMongo | |
{ | |
public class ApiModule : NancyModule | |
{ | |
private readonly MongoCollection<Message> _Messages; | |
public ApiModule(MongoCollection<Message> messages) | |
: base("/api") { | |
_Messages = messages; | |
Get["/messages"] = GetMessages; | |
Post["/messages"] = AddMessage; | |
} | |
private Response GetMessages(dynamic parameters) { | |
return Response.AsJson(_Messages.FindAll().SetLimit(100).ToArray()); | |
} | |
private Response AddMessage(dynamic parameters) { | |
if (!Request.Form.Message.HasValue) | |
return HttpStatusCode.BadRequest; | |
_Messages.Save(new Message { | |
Content = Request.Form.Message | |
}); | |
return HttpStatusCode.OK; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
using System.Linq; | |
using MongoDB.Driver; | |
using Nancy; | |
using Nancy.Conventions; | |
using NancyMongo.Models; | |
using TinyIoC; | |
namespace NancyMongo | |
{ | |
public class CustomBootstrapper : DefaultNancyBootstrapper | |
{ | |
protected override void ConfigureConventions(NancyConventions nancyConventions) { | |
nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("Scripts")); | |
} | |
protected override void ConfigureApplicationContainer(TinyIoCContainer container) { | |
var connString = ConfigurationManager.AppSettings["MONGOHQ_URL"]; | |
var databaseName = connString.Split('/').Last(); | |
var server = MongoServer.Create(connString); | |
var database = server.GetDatabase(databaseName); | |
if (!database.CollectionExists("Messages")) | |
database.CreateCollection("Messages"); | |
container.Register<MongoServer>(server); | |
container.Register<MongoDatabase>(database); | |
container.Register<MongoCollection<Message>>(database.GetCollection<Message>("Messages")); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>Nancy Mongo Demo Page</title> | |
<link type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/humanity/jquery-ui.css" rel="stylesheet" /> | |
</head> | |
<body> | |
<h1>Nancy Message Demo</h1> | |
<p> | |
A demo page for using Nancy + MongoDb + AppHarbor. See <a href="http://csainty.blogspot.com">here</a> | |
</p> | |
<h2>Messages</h2> | |
<ul data-bind="foreach: messages"> | |
<li data-bind="text: Content"></li> | |
</ul> | |
<h2>New Message</h2> | |
<p> | |
Enter your message and hit submit. The message will be saved in MongoDb on the server and redisplayed. | |
</p> | |
<p> | |
<input type="text" data-bind="value: messageText" /> | |
<button data-bind="click: sendMessage">Submit</button> | |
</p> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script> | |
<script src="/Scripts/knockout-1.3.0beta.js" type="text/javascript"></script> | |
<script type="text/javascript"> | |
var viewModel = { | |
messages: ko.observableArray([]), | |
messageText: ko.observable(''), | |
sendMessage: function () { | |
$.ajax({ | |
url: '/api/messages', | |
data: { message: this.messageText() }, | |
type: 'POST', | |
success: function (result) { | |
viewModel.messages.push({ Content: viewModel.messageText() }); | |
viewModel.messageText(''); | |
} | |
}); | |
} | |
}; | |
ko.applyBindings(viewModel); | |
$(function () { | |
$('button').button(); | |
getMessages(); | |
}); | |
function getMessages() { | |
$.ajax({ | |
url: '/api/messages', | |
dataType: 'json', | |
cache: false, | |
type: 'GET', | |
success: function (result) { | |
for (var i = 0; i < result.length; i++) { | |
viewModel.messages.push(result[i]); | |
} | |
} | |
}); | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MongoDB.Bson.Serialization.Attributes; | |
using MongoDB.Bson.Serialization.IdGenerators; | |
namespace NancyMongo.Models | |
{ | |
public class Message | |
{ | |
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))] | |
public string Id { get; set; } | |
public string Content { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Nancy; | |
namespace NancyMongo | |
{ | |
public class PageModule : NancyModule | |
{ | |
public PageModule() { | |
Get["/"] = HomePage; | |
} | |
private Response HomePage(dynamic parameters) { | |
return View["HomePage"]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment