Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Adds support for a Route Prefix, Improves Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
rastiqdev committed Jun 20, 2023
1 parent 1b46256 commit d613496
Show file tree
Hide file tree
Showing 16 changed files with 1,399 additions and 1,233 deletions.
18 changes: 0 additions & 18 deletions .eslintrc

This file was deleted.

15 changes: 7 additions & 8 deletions Dockerfile
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"]
224 changes: 123 additions & 101 deletions bin/web.js
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);
}
);
66 changes: 31 additions & 35 deletions lib/api.js
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,
});
},
};

Loading

0 comments on commit d613496

Please sign in to comment.