This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
forked from GitbookIO/nuts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for a Route Prefix, Improves Dockerfile
From GitbookIO#160
- Loading branch information
Showing
16 changed files
with
1,399 additions
and
1,233 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
FROM mhart/alpine-node:5.8.0 | ||
FROM mhart/alpine-node:14.6.0 | ||
|
||
# Dumb-init, proper signal handling, and zombie reaping | ||
ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.1/dumb-init_1.2.1_amd64 /usr/local/bin/dumb-init | ||
RUN chmod +x /usr/local/bin/dumb-init | ||
|
||
# Switch to /app | ||
WORKDIR /app | ||
# Install deps | ||
COPY package.json ./ | ||
RUN npm install --production | ||
# Copy source | ||
COPY . ./ | ||
|
||
# Ports | ||
ENV PORT 80 | ||
EXPOSE 80 | ||
|
||
ENTRYPOINT ["npm", "start"] | ||
ENTRYPOINT ["/usr/local/bin/dumb-init", "--"] | ||
CMD ["npm", "start"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,134 +1,156 @@ | ||
var express = require('express'); | ||
var uuid = require('uuid'); | ||
var basicAuth = require('basic-auth'); | ||
var Analytics = require('analytics-node'); | ||
var nuts = require('../'); | ||
require('dotenv').config() | ||
var express = require("express"); | ||
var uuid = require("uuid"); | ||
var basicAuth = require("basic-auth"); | ||
var Analytics = require("analytics-node"); | ||
var nuts = require("../"); | ||
require("dotenv").config(); | ||
|
||
var app = express(); | ||
|
||
var apiAuth = { | ||
username: process.env.API_USERNAME, | ||
password: process.env.API_PASSWORD | ||
var apiAuth = { | ||
username: process.env.API_USERNAME, | ||
password: process.env.API_PASSWORD, | ||
}; | ||
|
||
var analytics = undefined; | ||
var downloadEvent = process.env.ANALYTICS_EVENT_DOWNLOAD || 'download'; | ||
var downloadEvent = process.env.ANALYTICS_EVENT_DOWNLOAD || "download"; | ||
if (process.env.ANALYTICS_TOKEN) { | ||
analytics = new Analytics(process.env.ANALYTICS_TOKEN); | ||
analytics = new Analytics(process.env.ANALYTICS_TOKEN); | ||
} | ||
|
||
var myNuts = nuts.Nuts({ | ||
repository: process.env.GITHUB_REPO, | ||
token: process.env.GITHUB_TOKEN, | ||
endpoint: process.env.GITHUB_ENDPOINT, | ||
username: process.env.GITHUB_USERNAME, | ||
password: process.env.GITHUB_PASSWORD, | ||
timeout: process.env.VERSIONS_TIMEOUT, | ||
cache: process.env.VERSIONS_CACHE, | ||
refreshSecret: process.env.GITHUB_SECRET, | ||
proxyAssets: !Boolean(process.env.DONT_PROXY_ASSETS) | ||
routePrefix: process.env.ROUTE_PREFIX, | ||
repository: process.env.GITHUB_REPO, | ||
token: process.env.GITHUB_TOKEN, | ||
endpoint: process.env.GITHUB_ENDPOINT, | ||
username: process.env.GITHUB_USERNAME, | ||
password: process.env.GITHUB_PASSWORD, | ||
timeout: process.env.VERSIONS_TIMEOUT, | ||
cache: process.env.VERSIONS_CACHE, | ||
refreshSecret: process.env.GITHUB_SECRET, | ||
proxyAssets: !Boolean(process.env.DONT_PROXY_ASSETS), | ||
}); | ||
|
||
// Control access to API | ||
myNuts.before('api', function(access, next) { | ||
if (!apiAuth.username) return next(); | ||
|
||
function unauthorized() { | ||
next(new Error('Invalid username/password for API')); | ||
}; | ||
|
||
var user = basicAuth(access.req); | ||
if (!user || !user.name || !user.pass) { | ||
return unauthorized(); | ||
}; | ||
|
||
if (user.name === apiAuth.username && user.pass === apiAuth.password) { | ||
return next(); | ||
} else { | ||
return unauthorized(); | ||
}; | ||
myNuts.before("api", function (access, next) { | ||
if (!apiAuth.username) return next(); | ||
|
||
function unauthorized() { | ||
next(new Error("Invalid username/password for API")); | ||
} | ||
|
||
var user = basicAuth(access.req); | ||
if (!user || !user.name || !user.pass) { | ||
return unauthorized(); | ||
} | ||
|
||
if (user.name === apiAuth.username && user.pass === apiAuth.password) { | ||
return next(); | ||
} else { | ||
return unauthorized(); | ||
} | ||
}); | ||
|
||
// Log download | ||
myNuts.before('download', function(download, next) { | ||
console.log('download', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type); | ||
|
||
next(); | ||
myNuts.before("download", function (download, next) { | ||
console.log( | ||
"download", | ||
download.platform.filename, | ||
"for version", | ||
download.version.tag, | ||
"on channel", | ||
download.version.channel, | ||
"for", | ||
download.platform.type | ||
); | ||
|
||
next(); | ||
}); | ||
myNuts.after('download', function(download, next) { | ||
console.log('downloaded', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type); | ||
|
||
// Track on segment if enabled | ||
if (analytics) { | ||
var userId = download.req.query.user; | ||
|
||
analytics.track({ | ||
event: downloadEvent, | ||
anonymousId: userId? null : uuid.v4(), | ||
userId: userId, | ||
properties: { | ||
version: download.version.tag, | ||
channel: download.version.channel, | ||
platform: download.platform.type, | ||
os: nuts.platforms.toType(download.platform.type) | ||
} | ||
}); | ||
} | ||
myNuts.after("download", function (download, next) { | ||
console.log( | ||
"downloaded", | ||
download.platform.filename, | ||
"for version", | ||
download.version.tag, | ||
"on channel", | ||
download.version.channel, | ||
"for", | ||
download.platform.type | ||
); | ||
|
||
// Track on segment if enabled | ||
if (analytics) { | ||
var userId = download.req.query.user; | ||
|
||
analytics.track({ | ||
event: downloadEvent, | ||
anonymousId: userId ? null : uuid.v4(), | ||
userId: userId, | ||
properties: { | ||
version: download.version.tag, | ||
channel: download.version.channel, | ||
platform: download.platform.type, | ||
os: nuts.platforms.toType(download.platform.type), | ||
}, | ||
}); | ||
} | ||
|
||
next(); | ||
next(); | ||
}); | ||
|
||
if (process.env.TRUST_PROXY) { | ||
try { | ||
var trustProxyObject = JSON.parse(process.env.TRUST_PROXY); | ||
app.set('trust proxy', trustProxyObject); | ||
} | ||
catch (e) { | ||
app.set('trust proxy', process.env.TRUST_PROXY); | ||
} | ||
try { | ||
var trustProxyObject = JSON.parse(process.env.TRUST_PROXY); | ||
app.set("trust proxy", trustProxyObject); | ||
} catch (e) { | ||
app.set("trust proxy", process.env.TRUST_PROXY); | ||
} | ||
} | ||
|
||
app.use(myNuts.router); | ||
|
||
// Error handling | ||
app.use(function(req, res, next) { | ||
res.status(404).send("Page not found"); | ||
app.use(function (req, res, next) { | ||
res.status(404).send("Page not found"); | ||
}); | ||
app.use(function(err, req, res, next) { | ||
var msg = err.message || err; | ||
var code = 500; | ||
|
||
console.error(err.stack || err); | ||
|
||
// Return error | ||
res.format({ | ||
'text/plain': function(){ | ||
res.status(code).send(msg); | ||
}, | ||
'text/html': function () { | ||
res.status(code).send(msg); | ||
}, | ||
'application/json': function (){ | ||
res.status(code).send({ | ||
'error': msg, | ||
'code': code | ||
}); | ||
} | ||
}); | ||
app.use(function (err, req, res, next) { | ||
var msg = err.message || err; | ||
var code = 500; | ||
|
||
console.error(err.stack || err); | ||
|
||
// Return error | ||
res.format({ | ||
"text/plain": function () { | ||
res.status(code).send(msg); | ||
}, | ||
"text/html": function () { | ||
res.status(code).send(msg); | ||
}, | ||
"application/json": function () { | ||
res.status(code).send({ | ||
error: msg, | ||
code: code, | ||
}); | ||
}, | ||
}); | ||
}); | ||
|
||
myNuts.init() | ||
myNuts | ||
.init() | ||
|
||
// Start the HTTP server | ||
.then(function() { | ||
var server = app.listen(process.env.PORT || 5000, function () { | ||
// Start the HTTP server | ||
.then( | ||
function () { | ||
var server = app.listen(process.env.PORT || 5000, function () { | ||
var host = server.address().address; | ||
var port = server.address().port; | ||
|
||
console.log('Listening at http://%s:%s', host, port); | ||
}); | ||
}, function(err) { | ||
console.log(err.stack || err); | ||
process.exit(1); | ||
}); | ||
console.log("Listening at http://%s:%s", host, port); | ||
}); | ||
}, | ||
function (err) { | ||
console.log(err.stack || err); | ||
process.exit(1); | ||
} | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,40 @@ | ||
var startTime = Date.now(); | ||
var Q = require('q'); | ||
var Q = require("q"); | ||
|
||
module.exports = { | ||
'status': function () { | ||
return { | ||
uptime: (Date.now() - startTime)/1000 | ||
}; | ||
}, | ||
status: function () { | ||
return { | ||
uptime: (Date.now() - startTime) / 1000, | ||
}; | ||
}, | ||
|
||
'versions': function (req) { | ||
return this.versions.filter({ | ||
platform: req.query.platform, | ||
channel: req.query.channel || '*' | ||
}); | ||
}, | ||
versions: function (req) { | ||
return this.versions.filter({ | ||
platform: req.query.platform, | ||
channel: req.query.channel || "*", | ||
}); | ||
}, | ||
|
||
'channels': function () { | ||
return this.versions.channels(); | ||
}, | ||
channels: function () { | ||
return this.versions.channels(); | ||
}, | ||
|
||
'refresh': function () { | ||
return Q() | ||
.then(this.backend.onRelease) | ||
.thenResolve({done: true} | ||
); | ||
}, | ||
refresh: function () { | ||
return Q().then(this.backend.onRelease).thenResolve({ done: true }); | ||
}, | ||
|
||
'version/:tag': function (req) { | ||
return this.versions.resolve({ | ||
tag: req.params.tag, | ||
channel: '*' | ||
}); | ||
}, | ||
"version/:tag": function (req) { | ||
return this.versions.resolve({ | ||
tag: req.params.tag, | ||
channel: "*", | ||
}); | ||
}, | ||
|
||
'resolve': function(req) { | ||
return this.versions.resolve({ | ||
channel: req.query.channel, | ||
platform: req.query.platform, | ||
tag: req.query.tag | ||
}); | ||
} | ||
resolve: function (req) { | ||
return this.versions.resolve({ | ||
channel: req.query.channel, | ||
platform: req.query.platform, | ||
tag: req.query.tag, | ||
}); | ||
}, | ||
}; | ||
|
Oops, something went wrong.