Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 122 lines (101 sloc) 4.052 kb
e3af2b5 @nilakanta initial commit
nilakanta authored
1 coffee = require 'coffee-script'
2 express = require 'express'
3 cron = require 'cron'
09bd8f8 @sreeix Using winston and adding a print function as helper
sreeix authored
4 util = require 'util'
5 winston = require 'winston'
6
e3af2b5 @nilakanta initial commit
nilakanta authored
7 Conf = require './conf'
8 everyauth = require('./auth').create(Conf)
09bd8f8 @sreeix Using winston and adding a print function as helper
sreeix authored
9 helper = require './lib/helper'
e3af2b5 @nilakanta initial commit
nilakanta authored
10
11 packages = require './models/package'
12 RedisStore = require('connect-redis')(express)
13 app = express.createServer()
14
15 helpers =
16 toProperCase: (str) ->
17 str.replace ///\w\S*///g, (txt) ->
18 txt.charAt(0).toUpperCase() + txt.substr(1)
19 current_user: (req) ->
20 req.session.auth.github.user
21 flash: (req) ->
22 req.flash()
23
24 ensureAuthenticated = (req, res, next) ->
25 if req.loggedIn
26 next()
27 else
28 req.flash('warning', "Please login.")
29 res.redirect('back');
30
31 app.configure ->
32 app.use express.bodyParser()
33 app.use express.cookieParser()
34 app.use express.session
35 store: new RedisStore
36 maxAge: 24 * 60 * 60 * 1000
37 port: Conf.redis.port
38 host: Conf.redis.host
39 pass: Conf.redis.auth
40 secret: "eat your dog food"
41 app.use everyauth.middleware()
42 app.use app.router
43 app.set 'view engine', 'jade'
44 app.set 'views', __dirname + '/views'
45 app.use express.static __dirname + '/public'
46 app.use express.favicon('favicon.ico')
47 app.helpers helpers
48 everyauth.helpExpress(app)
49
50 app.configure 'development', () ->
51 app.use express.errorHandler(showStack: true, dumpExceptions: true)
52
53 app.configure 'production', () ->
54 app.error (err, req, res, next) ->
55 if err.error is 'not_found'
56 res.render '404', title: '', params: req.params, layout: false
57 else
58 next(err)
59
60 app.get '/', (req, res) ->
61 packages.by_category null, 10, (categories) ->
62 packages.by_rank 10, (top_ranked_packages) ->
63 res.render 'index',
64 categories: categories
65 top_ranked_packages: top_ranked_packages
66 title: 'Node.js happiness'
67
68 app.get '/packages', (req, res) ->
69 packages.find_all req.query.key, (packages_info) ->
70 res.render 'packages', key: packages_info.key, packages: packages_info.docs, title: 'All Packages'
71
72 app.get '/packages/:name', (req, res, next) ->
73 packages.find req.params.name, (err, package) ->
74 if err
75 next(err)
76 else
77 latest_tag = package["dist-tags"]?.latest ? ""
78 latest = package.versions?[latest_tag]
79 res.render 'package', package: package, title: req.params.name, latest_tag: latest_tag, latest: latest
80
81 app.get '/categories', (req, res) ->
82 packages.by_category null, 10, (categories) ->
83 res.render 'categories', categories: categories, title: 'All Categories'
84
85 app.get '/categories/:name', (req, res) ->
86 packages.by_category req.params.name, 10000, (category_info) ->
87 res.render 'category', category_info: category_info, title: "Category - #{req.params.name}"
88
89 app.get '/search', (req, res) ->
90 packages.search req.query.q, (response) ->
91 res.render 'search_result', response: response, title: "Search - #{req.query.q}"
92
93 app.get '/top_dependent_packages', (req, res) ->
94 packages.top_by_dependencies 10, (results) ->
95 res.render 'top_by_dependencies', layout: false, results: results, title: "Top packages by dependency"
96
97 app.get '/recently_added', (req, res) ->
98 packages.recently_added 10, (results) ->
99 res.render 'recently_added', layout: false, results: results, title: "Recently added packages"
100
101 app.post '/packages/:name/like', (req, res, next) ->
102 if req.session.auth
103 packages.like req.params.name, req.session.auth.github.user.login, (err, count) ->
104 res.send count: count
105 else
106 res.send "Please log in to Like", 403
107
108 port = process.env.PORT || 4000
109
110 app.listen port, () ->
5661f1a @nilakanta - using winston for logging
nilakanta authored
111 winston.info "app started at port #{port}"
e3af2b5 @nilakanta initial commit
nilakanta authored
112
85b0939 @sreeix Rolling back update watch
sreeix authored
113 do packages.watch_updates
e3af2b5 @nilakanta initial commit
nilakanta authored
114
d62b036 @nilakanta fixing npm-replication
nilakanta authored
115 if process.env.ENV_VARIABLE is 'production'
936f053 @nilakanta tweaking the cron jobs
nilakanta authored
116 new cron.CronJob '0 0 6,18 * * * *', () ->
5661f1a @nilakanta - using winston for logging
nilakanta authored
117 winston.info "Running github sync Cron now"
d62b036 @nilakanta fixing npm-replication
nilakanta authored
118 packages.import_from_github {}, helper.print("github sync")
119
936f053 @nilakanta tweaking the cron jobs
nilakanta authored
120 new cron.CronJob '0 0 5,17 * * * *', () ->
5661f1a @nilakanta - using winston for logging
nilakanta authored
121 winston.info "Running Import job Cron now"
d62b036 @nilakanta fixing npm-replication
nilakanta authored
122 packages.import_from_npm {}, helper.print( "NPM Import")
Something went wrong with that request. Please try again.