Skip to content

Instantly share code, notes, and snippets.

@carlos8f
Created June 13, 2014 07:52
Show Gist options
  • Select an option

  • Save carlos8f/b7d6f5b60306e63da635 to your computer and use it in GitHub Desktop.

Select an option

Save carlos8f/b7d6f5b60306e63da635 to your computer and use it in GitHub Desktop.
simple blog with LevelDB persistence, built with Motley
module.exports = function (app) {
return app.controller()
// just upsert a user and log them in
.post('/login', function (req, res, next) {
function logIn (err, user) {
if (err) return next(err);
if (user) {
req.login(user);
res.redirect('/');
}
else app.users.create({id: req.body.id}, logIn);
}
app.users.load(req.body.id, logIn);
})
// logout and redirect
.get('/logout', function (req, res, next) {
req.logout();
res.redirect('/');
})
};
<h1>ERR!</h1>
<p><strong>{{message}}</strong></p>
<form action="/posts" method="post">
<p><label>title<br><input class="input" type="text" name="title" size="30"></label></p>
<p><label>body (markdown)<br><textarea class="input" name="body" cols="50" rows="10"></textarea></label></p>
<p><input class="submit" type="submit" value="Post"></p>
</form>
<h1>motley example: a simple blog</h1>
{{#if user}}
<ul class="account">
<li>logged in as {{user.id}} <a href="/logout">[log out]</a></li>
</ul>
{{> form}}
{{else}}
{{> login}}
{{/if}}
{{#if posts}}<h2>posts</h2>{{/if}}
<ul class="posts">
{{#each posts}}{{> item-post}}{{/each}}
</ul>
<li><a href="/posts/{{id}}">{{title}}</a> <small>by {{author_id}}</small></li>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{title}}</title>
<link rel="stylesheet" href="/style.css">
<link rel="icon" type="image/png" href="/motley.png">
</head>
<body>
{{> messages}}
{{{content}}}
</body>
</html>
<form action="/login" method="post">
<input type="text" name="id" placeholder="username">
<input class="submit" type="submit" value="create user or log in">
</form>
{{#if messages}}
{{#each messages}}
<div class="message {{type}}">{{message}}</div>
{{/each}}
{{/if}}
id: vpaoaI75VJEnzcD3
public: "*.css"
collections: "*_collection.js"
controllers: "*_controller.js"
views: "*.hbs"
port: 3000
{
"name": "motley-example-gist-blog",
"version": "0.0.0",
"description": "example using motley",
"dependencies": {
"marked": "~0.2.9"
}
}
<p><a href="/">&laquo; back</a></p>
<h2>{{post.title}}</h2>
<p><small><em>by {{post.author_id}}</em></small></p>
<div class="body">
{{{post.body_safe}}}
</div>
module.exports = function (app) {
return app.collection({
name: 'posts',
save: function (post, cb) {
if (!post.title) return cb(new Error('title is required!'));
if (!post.body) return cb(new Error('body is required!'));
if (!post.author_id) return cb(new Error('author_id is required!'));
cb();
}
});
};
var marked = require('marked');
function requireAuth (req, res, next) {
if (req.user) next();
else res.renderStatus(403);
}
module.exports = function (app) {
app.require('posts');
return app.controller()
.get('/', function (req, res, next) {
var posts = [];
app.posts.tail({load: true}, function (err, chunk, next) {
if (err) return next(err);
posts = posts.concat(chunk);
if (chunk.length && next) next();
else res.render('index', {
title: 'motley example',
user: req.user,
posts: posts
});
});
})
.get('/posts/:id', function (req, res, next) {
app.posts.load(req.params.id, function (err, post) {
if (err) return next(err);
if (post) {
res.vars.post = post;
res.vars.title = post.title;
res.render('post');
}
else res.renderStatus(404);
});
})
.post('/posts', requireAuth, function (req, res, next) {
req.body.body_safe = marked(req.body.body);
req.body.author_id = req.user.id;
app.posts.create(req.body, function (err, post) {
if (err) return next(err);
res.flash('post successful');
res.redirect('/posts/' + post.id);
});
})
};
<h1>FRAUD!</h1>
<h1>NOT GOOD!</h1>
body {
font-family: Helvetica, sans-serif;
padding: 50px;
font-size: 1.5em;
}
input, textarea {
font-family: Helvetica, sans-serif;
padding: 10px;
font-size: 1em;
}
.submit {
font-size: 25px;
line-height: 25px;
border: 0;
margin: 0;
padding: 10px 20px;
background-color: white;
border: 2px solid grey;
cursor: pointer;
}
.submit:hover {
color: #e00;
}
a {
color: #e00;
text-decoration: none;
}
.success {
color: green;
}
.error {
color: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment