Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add code formatting #13

Merged
merged 6 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
tab_width = 2
charset = utf-8

# Matches multiple files with brace expansion notation
# Set default charset
#[*.{js,ts}]

# Matches the exact files either package.json or .travis.yml
# [{package.json,.travis.yml}]

15 changes: 14 additions & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@ on:

name: Main
jobs:
Format:
name: 'Format'
runs-on: 'ubuntu-latest'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'npm'
- run: npm install
- run: npm run format:check

Test:
name: 'Test'
runs-on: 'ubuntu-latest'
needs: [Format]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'npm'
- run: npm install
- run: npm test
- run: npm test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ node_modules
.clever.json
coverage
paastis.yml
.history
.vscode
18 changes: 18 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.DS_Store
node_modules
/build
.svelte-kit
/package
.env
.env.*
!.env.example
.vercel
.output
.log
.idea
.clever.json
build
coverage
.github
.history
.vscode
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
2 changes: 1 addition & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Cf. https://dev.to/rushankhan1/build-a-cli-with-node-js-4jbi

console.log("🍻 Hello Paastis!");
console.log('🍻 Hello Paastis!');

import config from '../src/config.js';
import Server from '../src/Server.js';
Expand Down
24 changes: 23 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
},
"main": "src/index.js",
"scripts": {
"format:check": "prettier --check './**/*.{js,ts}'",
"format:write": "prettier --write './**/*.{js,ts}'",
"start": "node src/index.js",
"test": "NODE_OPTIONS=--experimental-vm-modules ./node_modules/.bin/jest"
},
Expand All @@ -31,6 +33,7 @@
"npm": "8"
},
"devDependencies": {
"jest": "^29.0.3"
"jest": "^29.0.3",
"prettier": "2.7.1"
}
}
23 changes: 15 additions & 8 deletions src/Scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import provider from './provider/index.js';
import { factory, registry } from './registry/index.js';

export default class Scheduler {

constructor(config) {
this._config = config;

this.stopIdleApps = this._stopIdleApps.bind(this);

this._task = cron.schedule(this._config.startAndStop.checkingIntervalCron, this.stopIdleApps, {
scheduled: false
});
this._task = cron.schedule(
this._config.startAndStop.checkingIntervalCron,
this.stopIdleApps,
{
scheduled: false,
}
);
}

async start() {
Expand Down Expand Up @@ -43,11 +46,13 @@ export default class Scheduler {
if (runningApp) {
// already managed
const diffMs = Math.abs(now - runningApp.lastAccessedAt);
const diffMins = Math.floor(((diffMs % 86400000) % 3600000) / 60000);
const diffMins = Math.floor(
((diffMs % 86400000) % 3600000) / 60000
);

if (diffMins > runningApp.maxIdleTime - 1) {
// ☠️ app should be stopped
await provider.stopApp(app.key, app.region)
await provider.stopApp(app.key, app.region);
await registry.removeApp(app.key);
}
} else {
Expand All @@ -59,11 +64,13 @@ export default class Scheduler {
}
const runningApps = await registry.listApps();
if (runningApps) {
console.log('Active apps: ', runningApps.map(app => app.name));
console.log(
'Active apps: ',
runningApps.map((app) => app.name)
);
}
} catch (err) {
console.error(err);
}
}

}
32 changes: 17 additions & 15 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ import http from 'http';
import { system, upstream } from './router/index.js';

export default class Server {

constructor(config) {
this._config = config;
this._host = this._config.server.host;
this._port = this._config.server.port;
this._systemApiEnabled = this._config.routing.systemApiEnabled;
this._server = http.createServer({
insecureHTTPParser: true
}, async (req, res) => {
try {
let proxyTarget = req.headers['PaastisProxyTarget'.toLowerCase()] || 'upstream';
if (this._systemApiEnabled && proxyTarget === 'system') {
return system(req, res);
this._server = http.createServer(
{
insecureHTTPParser: true,
},
async (req, res) => {
try {
let proxyTarget =
req.headers['PaastisProxyTarget'.toLowerCase()] || 'upstream';
if (this._systemApiEnabled && proxyTarget === 'system') {
return system(req, res);
}
return upstream(req, res);
} catch (err) {
console.error(err);
res.statusCode = 502;
res.end(err.toString());
}
return upstream(req, res);
} catch (err) {
console.error(err);
res.statusCode = 502;
res.end(err.toString());
}
});
);
}

async start() {
Expand All @@ -44,5 +47,4 @@ export default class Server {
process.exit(1);
}
}

}
4 changes: 2 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Command } from 'commander';

const program = new Command();

program
.option('-c, --config <file>');
program.option('-c, --config <file>');

program.parse();

Expand Down
22 changes: 14 additions & 8 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const config = {
port: parseInt(process.env.PORT, 10) || 3000,
},
routing: {
systemApiEnabled: parseBoolean(process.env.ROUTING_SYSTEM_API_ENABLED) || false,
systemApiEnabled:
parseBoolean(process.env.ROUTING_SYSTEM_API_ENABLED) || false,
systemApiToken: process.env.ROUTING_SYSTEM_API_TOKEN,
},
registry: {
Expand All @@ -32,32 +33,37 @@ const config = {
name: process.env.PROVIDER_NAME || 'scalingo', // ['clever-cloud', 'scalingo']
region: process.env.PROVIDER_REGION || 'to_be_defined',
clever: {
apiHost: process.env.PROVIDER_CLEVER_API_HOST || 'https://api.clever-cloud.com',
apiHost:
process.env.PROVIDER_CLEVER_API_HOST || 'https://api.clever-cloud.com',
oauthConsumerKey: process.env.PROVIDER_CLEVER_OAUTH_CONSUMER_KEY,
oauthConsumerSecret: process.env.PROVIDER_CLEVER_OAUTH_CONSUMER_SECRET,
apiOauthToken: process.env.PROVIDER_CLEVER_TOKEN,
apiOauthTokenSecret: process.env.PROVIDER_CLEVER_SECRET,
operationTimeout: parseInt(process.env.PROVIDER_CLEVER_OPERATION_TIMEOUT, 10) || 60,
operationTimeout:
parseInt(process.env.PROVIDER_CLEVER_OPERATION_TIMEOUT, 10) || 60,
},
heroku: {
apiToken: process.env.PROVIDER_HEROKU_API_TOKEN || 'tk-us-xxx',
operationTimeout: parseInt(process.env.PROVIDER_HEROKU_OPERATION_TIMEOUT, 10) || 30,
operationTimeout:
parseInt(process.env.PROVIDER_HEROKU_OPERATION_TIMEOUT, 10) || 30,
},
scalingo: {
apiToken: process.env.PROVIDER_SCALINGO_API_TOKEN || 'tk-us-xxx',
operationTimeout: parseInt(process.env.PROVIDER_SCALINGO_OPERATION_TIMEOUT, 10) || 30,
operationTimeout:
parseInt(process.env.PROVIDER_SCALINGO_OPERATION_TIMEOUT, 10) || 30,
},
},
startAndStop: {
checkingIntervalCron: process.env.START_AND_STOP_CHECKING_INTERVAL_CRON || '* * * * *',
checkingIntervalCron:
process.env.START_AND_STOP_CHECKING_INTERVAL_CRON || '* * * * *',
maxIdleTime: parseInt(process.env.START_AND_STOP_MAX_IDLE_TIME, 10) || 15,
},
hooks: {
beforeAppStart: process.env.HOOKS_BEFORE_START,
afterAppStart: process.env.HOOKS_AFTER_START,
beforeAppStop: process.env.HOOKS_BEFORE_STOP,
afterAppStop: process.env.HOOKS_AFTER_STOP,
}
}
},
};

export default config;
5 changes: 1 addition & 4 deletions src/provider/PaasApp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export default class PaasApp {

_app;

constructor(app) {
Expand All @@ -11,7 +10,7 @@ export default class PaasApp {
}

get isRunning() {
return this.status === 'running'
return this.status === 'running';
}

get key() {
Expand Down Expand Up @@ -69,6 +68,4 @@ export default class PaasApp {
get framework() {
throw new Error('Not yet implemented');
}

}