Created
June 13, 2014 07:52
-
-
Save carlos8f/b7d6f5b60306e63da635 to your computer and use it in GitHub Desktop.
simple blog with LevelDB persistence, built with Motley
This file contains hidden or 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
| 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('/'); | |
| }) | |
| }; |
This file contains hidden or 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
| id: vpaoaI75VJEnzcD3 | |
| public: "*.css" | |
| collections: "*_collection.js" | |
| controllers: "*_controller.js" | |
| views: "*.hbs" | |
| port: 3000 |
This file contains hidden or 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
| { | |
| "name": "motley-example-gist-blog", | |
| "version": "0.0.0", | |
| "description": "example using motley", | |
| "dependencies": { | |
| "marked": "~0.2.9" | |
| } | |
| } |
This file contains hidden or 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
| 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(); | |
| } | |
| }); | |
| }; |
This file contains hidden or 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
| 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); | |
| }); | |
| }) | |
| }; |
This file contains hidden or 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
| 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