This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
120 lines (120 loc) · 4.36 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var restify = require("restify");
var GitHubApi = require("github");
var request = require('request');
var Registry = require('npm-registry');
var mongo = require('mongodb');
var server = restify.createServer();
var npm = new Registry();
var mongoServer = mongo.MongoClient;
var NUM_PACKAGES_TO_SHOW = 10;
var port = process.env.PORT;
var npmTopPackagesUrl = "https://raw.githubusercontent.com/nexdrew/all-stars/master/packages.json";
var numProcessed = 0;
var mongoUser = process.env.DECODED_MONGO_USER;
var mongoPassword = process.env.DECODED_MONGO_PASSWORD;
var mongoServerUri = process.env.DECODED_MONGO_SERVER;
var mongoUri = "mongodb://" + mongoUser + ":" + mongoPassword + "@" + mongoServerUri;
server.use(restify.bodyParser());
server.get("/contributors/:pkg", function (req, res, next) {
var github = new GitHubApi({
version: "3.0.0"
});
npm.packages.get(req.params.pkg, function (err, packageDetails) {
var gitHubInfo = packageDetails[0].github;
github.repos.getContributors({
user: gitHubInfo.user,
repo: gitHubInfo.repo,
per_page: 10
}, function (err, response) {
res.send(response);
});
});
next();
});
server.get("/packages", function (req, res, next) {
request(npmTopPackagesUrl, function (error, response, body) {
numProcessed = 0;
var packages = JSON.parse(body);
var packageNames = Object.keys(packages).slice(0, NUM_PACKAGES_TO_SHOW);
var payload = [];
var num_processed = 0;
mongoServer.connect(mongoUri, function (err, db) {
var favorites = db.collection('favorites');
packageNames.forEach(function (name) {
var objToFind = {
repo: name,
userid: null,
tenantid: null
};
var userid = req.header("userid");
var tenantid = req.header("tenantid");
if (userid != null && tenantid != null) {
objToFind.userid = userid;
objToFind.tenantid = tenantid;
}
favorites.findOne(objToFind, function (err, doc) {
var item = { name: name, rank: packages[name].rank, favorite: false };
if (doc !== null) {
item.favorite = true;
}
// Repo marked, push it to the payload
payload.push(item);
numProcessed++;
// If we have enough to show, terminate connection to Mongo, sort the results, and send back to the client
if (numProcessed === NUM_PACKAGES_TO_SHOW) {
db.close();
payload.sort(function (a, b) {
if (a.rank < b.rank) {
return -1;
}
if (a.rank > b.rank) {
return 1;
}
return 0;
});
res.send(payload);
next();
}
});
});
});
});
});
server.post("/favorite/:repo", function (req, res, next) {
mongoServer.connect(mongoUri, function (err, db) {
var favorites = db.collection('favorites');
var userid = req.header("userid");
var tenantid = req.header("tenantid");
var favDoc = {
repo: req.params.repo,
userid: userid,
tenantid: tenantid
};
var isFavorite = req.params.isFavorite;
if (isFavorite) {
favorites.insertOne(favDoc, function (err, result) {
if (err != null) {
console.log(err);
}
db.close();
res.send("OK");
});
}
else {
favorites.findOneAndDelete(favDoc, null, function (err, result) {
if (err != null) {
console.log(err);
}
db.close();
res.send("OK");
});
}
});
});
server.get(/.*/, restify.serveStatic({
directory: __dirname,
default: 'decoded.html'
}));
server.listen(port, function () {
console.log("Listening for requests");
});