Skip to content

Commit

Permalink
build: 0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
32teeth committed Sep 11, 2024
1 parent 1aef444 commit eb61393
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
PORT=3000
APP_CRT=LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURhekNDQWxPZ0F3SUJBZ0lVUk1WclJ1UGJ2T3JicjVlK2ZWdXVOOHJ3MzRjd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1JURUxNQWtHQTFVRUJoTUNRVlV4RXpBUkJnTlZCQWdNQ2xOdmJXVXRVM1JoZEdVeElUQWZCZ05WQkFvTQpHRWx1ZEdWeWJtVjBJRmRwWkdkcGRITWdVSFI1SUV4MFpEQWVGdzB5TkRBNU1URXhNVE00TlRGYUZ3MHlOVEE1Ck1URXhNVE00TlRGYU1FVXhDekFKQmdOVkJBWVRBa0ZWTVJNd0VRWURWUVFJREFwVGIyMWxMVk4wWVhSbE1TRXcKSHdZRFZRUUtEQmhKYm5SbGNtNWxkQ0JYYVdSbmFYUnpJRkIwZVNCTWRHUXdnZ0VpTUEwR0NTcUdTSWIzRFFFQgpBUVVBQTRJQkR3QXdnZ0VLQW9JQkFRQ3gva2pKQVpyUHA3RDVKanRTQWZ4QnYvT2N2ck5KdVAzS1dMdXYrcHcyCjV6cXgzYUJIRklMUk5oYVFYd1RJaXVHemp3UlhJVjVFWVF4b0ltMng5b0U3MTFaRHBhM09zTC8vMW1tUUZZelIKR2lyTjgzRFBhL0RyRG90Sy9KU1llV2NlUHdSTkp6Mi9QV2hRc29KbGN3cVpDSVE0MmJDUlllTUdPY2pvVkhCSwoyWjh1NjIzN1Z6bVg4SUdrQkY4aEg3WWsvNk1YZTh2aE9hbVRpVXBwRnBQOE1WSTBHdkRaNE1UcVoveVhIWTNLClpscCtxUjlsNUZaSkVyT1Y3dnRRV1BW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 +1,13 @@
# Changelog

### 0.1.4

What changed in this version?

* Change 1
* Change 2
* Change 3

### 0.1.3

What changed in this version?
Expand Down
92 changes: 67 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
'use strict';

if(!process.env.HEROKU) {
if (!process.env.HEROKU) {
require('dotenv').config();
}
/**
* npm packages
*/
const fs = require('fs');
const express = require('express');
const app = require('express')();
const base64 = require('base-64');
Expand All @@ -28,27 +24,27 @@ if (process.env.HTTPS) {
const server = process.env.HTTPS ? require('https').createServer(options, app) : require('http').createServer(app);
const port = process.env.PORT;
const cors = require('cors');
const ip = require("ip");

global.os = require('os');
global.ifaces = os.networkInterfaces();

Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;

ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
return;
/**
* Replace ip package with custom IP class
*/
class IP {
address() {
const os = require('os');
const networkInterfaces = os.networkInterfaces();
for (const interfaceName of Object.keys(networkInterfaces)) {
for (const iface of networkInterfaces[interfaceName]) {
// Skip over internal (i.e., 127.0.0.1) and non-IPv4 addresses
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return '127.0.0.1';
}
}

if (alias >= 1) {
console.log(ifname + ':' + alias, iface.address);
} else {
console.log(ifname, iface.address);
}
++alias;
});
});
const ip = new IP();

/**
* router
Expand Down Expand Up @@ -125,8 +121,54 @@ app.all('*', (req, res) => {
res.redirect('/404');
});

const convert = (collection) => {
if (collection instanceof Map) {
let obj = {};
for (let [k, v] of collection) {
obj[k] = convert(v);
}
return obj;
} else if (collection instanceof Set) {
let arr = [];
for (let value of collection) {
arr.push(convert(value));
}
return arr;
} else {
return collection;
}
};

const stringify = (data) => {
return JSON.stringify(data);
};

const wss = require('ws').Server;
const socket = new wss({ server: server });
socket.broadcast = (obj) => socket.clients.forEach((client) => client.json(obj));

socket.on('connection', (ws) => {
ws.json = (obj) => ws.send(JSON.stringify(obj));
ws.broadcast = socket.broadcast;
ws.id = uuid()

ws.on('message', (message) => {
message = JSON.parse(message);
const { service, data } = message;

switch (service) {
default:
log(chalk`{bgWhite.bold ${service}} {white.bold ${stringify(data)}}`);
break;
}
});
});

module.exports = server.listen(port, () => {
log(chalk`{bgBlue.bold Web:} {blue.bold ${process.env.HTTPS ? 'https' : 'http'}://localhost:${port}} {cyan.bold ${process.env.HTTPS ? 'https' : 'http'}://${ip.address()}:${port}}`)
log(chalk`{bgBlue.bold API:} {blue.bold ${process.env.HTTPS ? 'https' : 'http'}://api.localhost:${port}} {cyan.bold ${process.env.HTTPS ? 'https' : 'http'}://api.${ip.address()}:${port}}`)
log(chalk`
{bgBlue.bold Web:}\t{blue.bold ${process.env.HTTPS ? 'https' : 'http'}://localhost:${port}} {cyan.bold ${process.env.HTTPS ? 'https' : 'http'}://${ip.address()}:${port}}
{bgBlue.bold API:}\t{blue.bold ${process.env.HTTPS ? 'https' : 'http'}://api.localhost:${port}} {cyan.bold ${process.env.HTTPS ? 'https' : 'http'}://api.${ip.address()}:${port}}
{bgBlue.bold Socket:}\t{blue.bold ${process.env.HTTPS ? 'wss' : 'ws'}://localhost:${port}} {cyan.bold ${process.env.HTTPS ? 'wss' : 'ws'}://${ip.address()}:${port}}
`);
chalk.reset();
});
120 changes: 69 additions & 51 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "git+https://github.com/32teeth/generic-nodejs-express-api.git"
},
"license": "CC-BY-SA-4.0",
"version": "0.1.3",
"version": "0.1.4",
"keywords": [
"nodejs",
"express",
Expand Down Expand Up @@ -71,7 +71,6 @@
"dotenv": "^16.3.1",
"ejs": "^3.1.10",
"express": "^4.18.2",
"ip": "^2.0.1",
"ws": "^8.16.0"
},
"devDependencies": {
Expand All @@ -91,4 +90,4 @@
"directories": {
"test": "test"
}
}
}

0 comments on commit eb61393

Please sign in to comment.