Skip to content

Commit

Permalink
0.5.0 api for simple tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
jolav committed Aug 23, 2024
1 parent beb2800 commit e5426d4
Show file tree
Hide file tree
Showing 10 changed files with 178,570 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
![Active YES](https://img.shields.io/badge/Active-YES-green.svg)
![Version](https://img.shields.io/badge/version-0.4.4-orange.svg)
![Version](https://img.shields.io/badge/version-0.5.0-orange.svg)

# [BETAZONE](https://jolav.github.io/betazone) ![logo](https://github.com/jolav/betazone/blob/master/www/assets/idea64.png?raw=true)

Expand Down
41 changes: 41 additions & 0 deletions server-Node/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* */

import { _config } from "./_private.js";
import os from "os";

const config = {
"version": "0.5.0",
"mode": "dev",
"port": 3000,
};

checkMode();

function checkMode() {
const serverName = os.hostname().toLowerCase();
if (!_config.devHosts.includes(serverName)) {
config.mode = _config.mode;
config.port = _config.port;
}
}

export { config };

/* FAKE _private.js
const _config = {
"devHosts": [
"host1",
"host2",
"host3",
"host4",
],
"mode": "production",
"port": 3003,
};
export {
_config
};
*/
9 changes: 9 additions & 0 deletions server-Node/aux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* */

function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

export {
randomInt
};
50 changes: 50 additions & 0 deletions server-Node/betazone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* */

import express from "express";
import helmet from 'helmet';

import { config } from "./_config.js";
import { logger } from "./middlewares.js";
import { random } from "./random/random.js";

const app = express();

app.use(helmet());
app.disable('x-powered-by');

app.use(logger);

// routes
app.use('/random', random);

app.get("/version", function (req, res) {
res
.status(200)
.json({ version: config.version });
});

app.get("/ping", function (req, res) {
res
.status(200)
.json({ ping: "ok" });
});

// custom 404
app.use(function (req, res) {
console.error('Unavailable Endpoint', req.path);
res.
status(404)
.json({ status: "Unavailable Endpoint " + req.path });
});

// custom error
app.use(function (err, req, res, next) {
console.error(err.stack);
res.
status(500).
json({ status: "Internal Server Error" });
});

app.listen(config.port || 3000, function () {
console.log('Server running on port ', config.port);
});
23 changes: 23 additions & 0 deletions server-Node/middlewares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* */

function logger(req, res, next) {
console.log(
new Date().toUTCString(),
getIP(req),
req.method,
req.originalUrl
);
next();
}

function getIP(req) {
return (
req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress).split(',')[0];
}

export {
logger
};
Loading

0 comments on commit e5426d4

Please sign in to comment.