Last active
May 21, 2016 16:56
-
-
Save sailsinaction/9a7b0dd73df58e0f75462ebe98f0aeba to your computer and use it in GitHub Desktop.
Chapter 11 - Gists
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
.d8888b. 888 888 d888 d888 .d8888b. d8b 888 | |
d88P Y88b 888 888 d8888 d8888 d88P Y88b Y8P 888 | |
888 888 888 888 888 888 888 888 888 | |
888 88888b. 8888b. 88888b. 888888 .d88b. 888d888 888 888 888 888 .d8888b 888888 .d8888b | |
888 888 "88b "88b 888 "88b 888 d8P Y8b 888P" 888 888 888 88888 888 88K 888 88K | |
888 888 888 888 .d888888 888 888 888 88888888 888 888 888 888888 888 888 888 "Y8888b. 888 "Y8888b. | |
Y88b d88P 888 888 888 888 888 d88P Y88b. Y8b. 888 888 888 Y88b d88P 888 X88 Y88b. X88 | |
"Y8888P" 888 888 "Y888888 88888P" "Y888 "Y8888 888 8888888 8888888 "Y8888P88 888 88888P' "Y888 88888P' | |
888 | |
888 | |
888 |
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
module.exports.blueprints = { | |
shortcuts: true, | |
prefix: '/bp', | |
}; | |
module.exports.connections = { | |
myPostgresqlServer: { | |
adapter: 'sails-postgresql', | |
host: 'localhost', | |
database: 'brushfire' | |
} | |
}; |
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
/** | |
* Brushfire explicit routes | |
* | |
*/ | |
module.exports.routes = { | |
/************************************************************* | |
* JSON API ENDPOINTS * | |
*************************************************************/ | |
'PUT /login': 'UserController.login', | |
'POST /logout': 'UserController.logout', | |
'GET /logout': 'PageController.logout', | |
'POST /user/signup': 'UserController.signup', | |
'PUT /user/remove-profile': 'UserController.removeProfile', | |
'PUT /user/restore-profile': 'UserController.restoreProfile', | |
'PUT /user/restore-gravatar-URL': 'UserController.restoreGravatarURL', | |
'PUT /user/update-profile': 'UserController.updateProfile', | |
'PUT /user/change-password': 'UserController.changePassword', | |
'GET /user/admin-users': 'UserController.adminUsers', | |
'PUT /user/update-admin/:id': 'UserController.updateAdmin', | |
'PUT /user/update-banned/:id': 'UserController.updateBanned', | |
'PUT /user/update-deleted/:id': 'UserController.updateDeleted', | |
'PUT /user/generate-recovery-email': 'UserController.generateRecoveryEmail', | |
'PUT /user/reset-password': 'UserController.resetPassword', | |
'PUT /user/follow': 'UserController.follow', | |
'PUT /user/unfollow': 'UserController.unFollow', | |
'GET /tutorials': 'TutorialController.browseTutorials', | |
'POST /tutorials': 'TutorialController.createTutorial', | |
'POST /tutorials/:tutorialId/videos': 'TutorialController.addVideo', | |
'PUT /tutorials/:id': 'TutorialController.updateTutorial', | |
'PUT /tutorials/:id/rate': 'TutorialController.rateTutorial', | |
'GET /videos/:id/join': 'VideoController.joinChat', | |
'POST /videos/:id/chat': 'VideoController.chat', | |
'DELETE /tutorials/:id': 'TutorialController.deleteTutorial', | |
'DELETE /videos/:id': 'TutorialController.removeVideo', | |
'POST /videos/:id/up': 'VideoController.reorderVideoUp', | |
'POST /videos/:id/down': 'VideoController.reorderVideoDown', | |
'PUT /videos/:id': 'TutorialController.updateVideo', | |
/************************************************************* | |
* Server Rendered HTML Page Endpoints * | |
*************************************************************/ | |
'GET /profile/followers': 'PageController.profileFollower', | |
'GET /': 'PageController.home', | |
'GET /profile/edit': 'PageController.editProfile', | |
'GET /profile/restore': 'PageController.restoreProfile', | |
'GET /signin': 'PageController.signin', | |
'GET /signup': 'PageController.signup', | |
'GET /administration': 'PageController.administration', | |
'GET /password-recovery-email': 'PageController.passwordRecoveryEmail', | |
'GET /password-recovery-email-sent': 'PageController.passwordRecoveryEmailSent', | |
'GET /password-reset-form/:passwordRecoveryToken': 'PageController.passwordReset', | |
'GET /tutorials/search': 'TutorialController.searchTutorials', | |
'GET /tutorials/browse': 'PageController.showBrowsePage', | |
'GET /tutorials/new': 'PageController.newTutorial', | |
'GET /tutorials/:id': 'PageController.tutorialDetail', | |
'GET /tutorials/:id/edit': 'PageController.editTutorial', | |
'GET /tutorials/:id/videos/new': 'PageController.newVideo', | |
'GET /tutorials/:tutorialId/videos/:id/edit': 'PageController.editVideo', | |
'GET /tutorials/:tutorialId/videos/:id/show': 'PageController.showVideo', | |
'GET /:username/followers': 'PageController.profileFollower', | |
'GET /:username/following': 'PageController.profileFollowing', | |
'GET /:username': { | |
controller: 'PageController', | |
action: 'profile', | |
skipAssets: true | |
} | |
// 'GET /:username': 'PageController.profile', | |
}; |
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
/** | |
* 404 (Not Found) Handler | |
* | |
* Usage: | |
* return res.notFound(); | |
* return res.notFound(err); | |
* return res.notFound(err, 'some/specific/notfound/view'); | |
* | |
* e.g.: | |
* ``` | |
* return res.notFound(); | |
* ``` | |
* | |
* NOTE: | |
* If a request doesn't match any explicit routes (i.e. `config/routes.js`) | |
* or route blueprints (i.e. "shadow routes", Sails will call `res.notFound()` | |
* automatically. | |
*/ | |
module.exports = function notFound (data, options) { | |
// Get access to `req`, `res`, & `sails` | |
var req = this.req; | |
var res = this.res; | |
var sails = req._sails; | |
// Set status code | |
res.status(404); | |
// Log error to console | |
if (data !== undefined) { | |
sails.log.verbose('Sending 404 ("Not Found") response: \n',data); | |
} | |
else sails.log.verbose('Sending 404 ("Not Found") response'); | |
// Only include errors in response if application environment | |
// is not set to 'production'. In production, we shouldn't | |
// send back any identifying information about errors. | |
if (sails.config.environment === 'production') { | |
data = undefined; | |
} | |
// If the user-agent wants JSON, always respond with JSON | |
if (req.wantsJSON) { | |
return res.jsonx(data); | |
} | |
// If second argument is a string, we take that to mean it refers to a view. | |
// If it was omitted, use an empty object (`{}`) | |
options = (typeof options === 'string') ? { view: options } : options || {}; | |
function afterwards(err,loggedInUser){ | |
if (err) { return res.serverError(err); } | |
var me; | |
if (!loggedInUser) { | |
me = null; | |
} | |
else { | |
me = { | |
email: loggedInUser.email, | |
gravatarURL: loggedInUser.gravatarURL, | |
username: loggedInUser.username, | |
admin: loggedInUser.admin | |
}; | |
} | |
var locals = { | |
data: data, | |
me: me | |
}; | |
if (options.view) { | |
return res.view(options.view, locals); | |
} | |
else return res.view('404', locals, function (err, html) { | |
if (err) { | |
if (err.code === 'E_VIEW_FAILED') { | |
sails.log.verbose('res.notFound() :: Could not locate view for error page (sending JSON instead). Details: ',err); | |
} | |
else { | |
sails.log.warn('res.notFound() :: When attempting to render error page view, an error occured (sending JSON instead). Details: ', err); | |
} | |
return res.jsonx(data); | |
} | |
return res.send(html); | |
}); | |
} | |
if (!req.session.userId) { | |
return afterwards(); | |
} | |
User.findOne({ id: req.session.userId }).exec(function(err,user){ | |
if (err) return afterwards(err); | |
return afterwards(null, user); | |
}); | |
}; |
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
/** | |
* 404 (Not Found) Handler | |
* | |
* Usage: | |
* return res.notFound(); | |
* return res.notFound(err); | |
* return res.notFound(err, 'some/specific/notfound/view'); | |
* | |
* e.g.: | |
* ``` | |
* return res.notFound(); | |
* ``` | |
* | |
* NOTE: | |
* If a request doesn't match any explicit routes (i.e. `config/routes.js`) | |
* or route blueprints (i.e. "shadow routes", Sails will call `res.notFound()` | |
* automatically. | |
*/ | |
module.exports = function notFound (data, options) { | |
// Get access to `req`, `res`, & `sails` | |
var req = this.req; | |
var res = this.res; | |
var sails = req._sails; | |
// Set status code | |
res.status(404); | |
// Log error to console | |
if (data !== undefined) { | |
sails.log.verbose('Sending 404 ("Not Found") response: \n',data); | |
} | |
else sails.log.verbose('Sending 404 ("Not Found") response'); | |
// Only include errors in response if application environment | |
// is not set to 'production'. In production, we shouldn't | |
// send back any identifying information about errors. | |
if (sails.config.environment === 'production') { | |
data = undefined; | |
} | |
// If the user-agent wants JSON, always respond with JSON | |
if (req.wantsJSON) { | |
return res.jsonx(data); | |
} | |
// If second argument is a string, we take that to mean it refers to a view. | |
// If it was omitted, use an empty object (`{}`) | |
options = (typeof options === 'string') ? { view: options } : options || {}; | |
(function ifThenFinally (cb){ | |
if (!req.session.userId) { | |
return cb(); | |
} | |
User.findOne({ id: req.session.userId }).exec(function(err,user){ | |
if (err) return cb(err); | |
return cb(null, user); | |
}); | |
})(function afterwards(err,loggedInUser){ | |
if (err) { return res.serverError(err); } | |
var me; | |
if (!loggedInUser) { | |
me = null; | |
} | |
else { | |
me = { | |
email: loggedInUser.email, | |
gravatarURL: loggedInUser.gravatarURL, | |
username: loggedInUser.username, | |
admin: loggedInUser.admin | |
}; | |
} | |
var locals = { | |
data: data, | |
me: me | |
}; | |
if (options.view) { | |
return res.view(options.view, locals); | |
} | |
else return res.view('404', locals, function (err, html) { | |
if (err) { | |
if (err.code === 'E_VIEW_FAILED') { | |
sails.log.verbose('res.notFound() :: Could not locate view for error page (sending JSON instead). Details: ',err); | |
} | |
else { | |
sails.log.warn('res.notFound() :: When attempting to render error page view, an error occured (sending JSON instead). Details: ', err); | |
} | |
return res.jsonx(data); | |
} | |
return res.send(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
module.exports.blueprints = { | |
shortcuts: true, | |
prefix: '/bp', | |
}; | |
module.exports.connections = { | |
myPostgresqlServer: { | |
adapter: 'sails-postgresql', | |
host: 'localhost', | |
database: 'brushfire' | |
} | |
}; | |
module.exports.mailgun = { | |
apiKey: 'YOUR API KEY', | |
domain: 'YOUR DOMAIN', | |
baseUrl: 'http://localhost:1337' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment