This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
64 lines (54 loc) · 1.55 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// requires
var cluster = require('cluster')
var express = require("express")
var stylus = require("stylus")
var knox = require('knox')
var mongoose = require('mongoose')
var config = require('./config/config.js')
var app = express.createServer()
config.mongo.db = process.env.MONGOHQ_URL || config.mongo.db
config.s3.key = process.env.S3_KEY || config.s3.key
config.s3.secret = process.env.S3_SECRET || config.s3.secret
var numCPUs = require('os').cpus().length
// express
app.configure(function(){
app.use(stylus.middleware({ src: __dirname + '/public' }))
app.use(express.methodOverride())
app.use(express.bodyParser())
app.use('/', express.static(__dirname + '/public'))
app.set("views", __dirname + '/views')
app.set("view options", { layout: false })
app.use(app.router)
})
// knox
var s3Client = knox.createClient({
key: config.s3.key,
secret: config.s3.secret,
bucket: config.s3.bucket
})
// mongoose
mongoose.connect(config.mongo.db)
var Schema = mongoose.Schema
var ObjectId = Schema.ObjectId
var FileEntry = new Schema({
url: { type: String },
shortUrl: { type: String },
name: { type: String },
secret: { type: String },
date: { type: Date, default: Date.now }
})
var fileModel = mongoose.model('FileEntry', FileEntry)
// routes
var routes = require('./lib/routes')(app, fileModel, s3Client)
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork()
console.log('Starting worker...')
}
cluster.on('death', function(worker) {
cluster.fork()
})
}
else {
app.listen(process.env.PORT || 3000)
}