Permalink
Browse files

Big refactoring

  • Loading branch information...
1 parent cdf80bd commit cabf3d33c3fe7f039f28e2d725bf478228cac8bb @alduro committed Mar 14, 2012
View
@@ -0,0 +1 @@
+web: node server.js
View
@@ -1,52 +0,0 @@
-express = require 'express'
-mongoose = require 'mongoose'
-require 'express-namespace'
-
-postsRoutes = require './routes/posts'
-admin = require './routes/admin'
-postModel = require './models/post'
-
-app = module.exports = express.createServer();
-
-Db = require('mongodb').Db
-Server = require('mongodb').Server
-server_config = new Server('localhost', 27017, {auto_reconnect: true, native_parser: true})
-db = new Db('my_database', server_config, {})
-mongoStore = require('connect-mongodb');
-
-# Mongo connection
-mongoose.connect 'mongodb://localhost/my_database'
-mongoose.connection.on "open", ->
- console.log "mongodb is connected!!"
-
-# Configuration
-app.configure () ->
- app.use express.logger format: ':method :url'
- app.use express.bodyParser()
- app.use express.methodOverride()
- app.use express.cookieParser()
- app.use express.session
- secret: '076ee61d63aa10a125ea872411e433b9'
- maxAge: new Date(Date.now() + 3600000)
- store: new mongoStore { db: db }
- app.use app.router
- app.use express.static "#{__dirname}/public"
- app.set 'views', __dirname + '/views'
- app.register '.coffee', require 'coffeekup'
- app.set 'view engine', 'coffee'
- app.set 'view options', layout: false
-
-
-app.configure 'development', () ->
- app.use express.errorHandler dumpExceptions: true, showStack: true
-
-app.configure 'production', () ->
- app.use express.errorHandler()
-
-#routes
-postsRoutes(app, postModel.Post)
-admin(app, postModel.Post, postModel.User)
-
-# Starting app
-app.listen process.env.PORT || 3000
-console.log "Express server listening on port #{app.address().port}"
@@ -0,0 +1,178 @@
+# Set all global variables
+pusher = require 'pusher'
+controller = {}
+app = {}
+db = {}
+
+# Constructor
+
+module.exports = (_app) ->
+ app = _app
+ db = app.set 'db'
+ return controller
+
+########################################################################
+# Login routes
+# render login form
+controller.login = (req, res) ->
+ User = db.main.model('User')
+ res.render 'admin/login', { user: new User(), title: 'Ingresar' }
+
+# login user
+controller.session = (req, res) ->
+ User = db.main.model('User')
+ User.findOne({ email: req.body.user.email }, (err, user) ->
+ if (user && user.authenticate(req.body.user.password))
+ req.session.user_id = user.id
+
+ if (req.body.remember_me)
+ loginToken = new LoginToken({ email: user.email })
+ loginToken.save( ->
+ res.cookie('logintoken', loginToken.cookieValue, { expires: new Date(Date.now() + 2 * 604800000), path: '/' })
+ )
+ res.redirect '/'
+ else
+ req.flash 'error', 'Login failed, try again'
+ res.redirect '/admin/login'
+ )
+
+# logout user
+controller.logout = (req, res) ->
+ LoginToken = db.main.model('LoginToken')
+ if (req.session)
+ LoginToken.remove({ email: req.currentUser.email }, -> {} )
+ res.clearCookie('logintoken')
+ req.session.destroy(-> {})
+ res.redirect '/admin/login'
+
+##########################################################################
+# Administrative Blog routes
+#
+
+##########################################################################
+# USER
+#
+
+# render user create form
+controller.newUser = (req, res) ->
+ res.render 'users/create', { user: new User() }
+
+# save new user
+controller.createUser = (req, res) ->
+ User = db.main.model('User')
+ user = new User(req.body.user)
+
+ userSaveFailed = ->
+ req.flash('error', 'Saving user failed')
+ res.render 'users/create', { user: user }
+
+ user.save( (err) ->
+ if (err)
+ userSaveFailed()
+ req.flash('info', 'User has been saved!')
+ res.redirect '/'
+ )
+
+##########################################################################
+# POSTS
+#
+
+controller.index = (req, res, next) ->
+ # expose pusher key
+ res.expose
+ app_key : req.app.set('pusher_key')
+ channel : 'blog_post'
+ events : 'post'
+ , 'PUSHER'
+
+ # render template
+ res.render 'home', { posts: db.posts.posts() }
+
+##########################################################################
+# NEW POST
+#
+
+# GET render post creation form
+controller.newPost = (req, res) ->
+ BlogPost = db.main.model('BlogPost')
+ res.render 'blogpost/create', { post: new BlogPost() }
+
+##########################################################################
+# CREATE POST
+#
+# POST save new blog post
+ controller.createPost = (req, res) ->
+ BlogPost = db.main.model('BlogPost')
+ post = new BlogPost()
+ post.title = req.body.blogpost.title
+ post.rsstext = req.body.blogpost.rsstext
+ post.preview = req.body.blogpost.preview
+ post.body = req.body.blogpost.body
+ post.created = new Date()
+ post.modified = new Date()
+ post.tags = req.body.blogpost.tags.split(',')
+
+ postCreationFailed = ->
+ req.flash('error', 'Creating post failed')
+ res.render 'blogpost/create', { post: post }
+
+ post.save (err) ->
+ if (err)
+ return postCreationFailed()
+
+ req.flash('info', 'Post created')
+ res.redirect '/'
+
+##########################################################################
+# DELETE POST
+#
+# delete blog post
+controller.delete = (req, res, next) ->
+ BlogPost = db.main.model('BlogPost')
+ BlogPost.findById(req.params.id, (err, bp) ->
+ if (!bp)
+ return next(new NotFound('Blogpost not found'))
+ else
+ bp.remove (err) ->
+ if (err)
+ return next(new Error('Blogpost failed to remove'))
+ else
+ req.flash('info', 'Post was removed')
+ res.redirect '/'
+ )
+
+# render update form
+controller.edit = (req, res, next) ->
+ BlogPost = db.main.model('BlogPost')
+ BlogPost.findById(req.params.id, (err, bp) ->
+ if (!bp)
+ return next(new NotFound('Blogpost not found'))
+ else
+ res.render 'blogpost/edit', { post: bp }
+ )
+
+# update blog post
+controller.update = (req, res, next) ->
+ BlogPost = db.main.model('BlogPost')
+ BlogPost.findById(req.params.id, (err, bp) ->
+ if (!bp)
+ return next(new NotFound('Blogpost not found'))
+ else
+ bp.title = req.body.blogpost.title
+ bp.rsstext = req.body.blogpost.rsstext
+ bp.preview = req.body.blogpost.preview
+ bp.body = req.body.blogpost.body
+ bp.tags = req.body.blogpost.tags.split(',')
+ bp.modified = new Date()
+
+ postUpdateFailed = ->
+ req.flash('error', 'Failde to update Post')
+ res.render 'blogpost/edit', { post: bp }
+
+ bp.save (err) ->
+ if (err)
+ return postUpdateFailed()
+
+ req.flash('info', 'Post updated')
+ res.redirect '/'
+ )
View
@@ -0,0 +1,49 @@
+# Load dependencies
+express = require 'express'
+stylus = require 'stylus'
+mongoose = require 'mongoose'
+nib = require 'nib'
+models = require './models'
+config = require './config'
+routes = require './routes'
+environment = require './environment'
+errors = require './errors'
+hooks = require './hooks'
+
+# util = require 'util'
+mongoStore = require 'connect-mongodb'
+helpers = require './helpers'
+
+#
+# Exports
+#
+module.exports = ->
+ # Create Server
+ app = express.createServer()
+
+ helpers(app)
+ # Load Mongoose Models
+
+ models(app)
+
+ # Load Expressjs config
+
+ config(app)
+
+ # Load Environmental Settings
+
+ environment(app)
+
+ # Load routes config
+
+ routes(app)
+
+ # Load error routes + pages
+
+ # errors(app)
+
+ # Load hooks
+
+ hooks(app)
+
+ return app
Oops, something went wrong. Retry.

0 comments on commit cabf3d3

Please sign in to comment.