Compare commits

..

60 Commits

Author SHA1 Message Date
Ratstail91 2e8c3f98ca Fully tested the remote database
Updated docker base image
2024-05-03 09:27:29 +10:00
Ratstail91 582c0b453c UNTESTED: Updated all dependencies 2024-05-03 07:08:09 +10:00
Ratstail91 9db6dbf63b UNTESTED: Added database port as a configurable option
Also updated license field in package.json
2024-04-15 21:03:23 +10:00
Ratstail91 3700372e92 Updated dependencies 2024-04-15 17:12:04 +10:00
Ratstail91 416ab2f3f9 Updated libraries, docker engine version, docker distro version 2023-12-23 23:55:26 +11:00
Kayne Ruse 1b5cbaea17 Docker behaviour changed, fixed 2023-06-26 23:20:34 +10:00
Kayne Ruse 0f2b8d3f52 Updated dependencies, bumped patch version 2023-06-26 23:06:15 +10:00
Kayne Ruse 88c2239fdb Updated dependencies 2023-05-29 07:56:13 +10:00
Kayne Ruse b5f9d0a7fc Updated dependencies 2023-05-03 21:32:31 +10:00
Kayne Ruse 1ec29e4519 Updated depencencies, bumped version 2023-03-25 01:50:32 +11:00
Kayne Ruse a15c43b3d0 Updated dependencies 2023-03-19 02:53:17 +11:00
Kayne Ruse 9bc96bdb5f Updated dependencies 2023-02-21 09:30:15 +11:00
Kayne Ruse 4bdcee11ea Updated dependencies, License 2023-01-12 08:08:34 +11:00
Kayne Ruse e1cd1ec001 Bumped version number 2023-01-04 12:57:13 +00:00
Kayne Ruse e8a9a79687 Switched to a slim docker distro 2023-01-04 23:51:49 +11:00
Kayne Ruse 7d628be826 Updated libraries 2022-12-31 19:03:50 +00:00
Kayne Ruse 7a42ab3108 Updated dependencies 2022-11-29 05:06:21 +00:00
Kayne Ruse ec573e1074 Updated dependencies 2022-11-13 02:17:33 +00:00
Kayne Ruse 9c5033daea Updated dependencies 2022-08-01 10:40:43 +01:00
Kayne Ruse fb4d857224 Tweaked cors handing 2022-07-26 13:40:18 +01:00
Kayne Ruse e0d6260d1a Updated dependencies 2022-07-23 11:49:18 +01:00
Kayne Ruse 53ea726c89 Bumped patch version 2022-06-10 17:26:27 +01:00
Kayne Ruse c44ed79e6d Updated dependencies 2022-06-10 17:09:31 +01:00
Kayne Ruse 518f5dbee7 Updated dependencies 2022-05-30 06:57:11 +01:00
Kayne Ruse be57dbe51c Added FUNDING.yml 2022-02-13 07:48:48 +11:00
Kayne Ruse 8309c3b832 Updated libraries 2022-01-20 13:49:05 +11:00
Kayne Ruse 95d229c561 Bumped version number 2022-01-03 08:31:56 +00:00
Kayne Ruse 0b1456ebde Merge branch 'timestamps' 2022-01-03 08:30:23 +00:00
Kayne Ruse bd5b6e8233 Added async keyword 2022-01-03 08:30:03 +00:00
Keith Campbell 05eecf8bdd removed everything but lines 50-60 2022-01-02 18:35:50 -05:00
Keith Campbell 2aedb6e938 ready for PR/review 2022-01-02 18:00:16 -05:00
Keith Campbell ab73d05471 updated original code, using createdAt from log 2022-01-01 21:45:09 -05:00
Keith Campbell f72b0e5522 added timestamp to messages and logs, need to test 2022-01-01 17:11:11 -05:00
Kayne Ruse 900312752b Cleaning up tools 2021-12-23 14:00:47 +00:00
Kayne Ruse 1b2868d68f Updated README.md 2021-12-21 19:38:08 +00:00
Kayne Ruse 33157d48d3 Re-established a database relationship 2021-12-12 16:53:33 +00:00
Kayne Ruse e0b3193607 Preparing for Egg Trainer merge 2021-11-18 15:00:56 +00:00
Kayne Ruse 655c81174e Updated package-lock.json 2021-11-17 06:14:31 +00:00
Kayne Ruse 8c053e75aa Bumped node to version 16 LTS 2021-11-17 04:43:11 +00:00
Kayne Ruse 69aff6ec32 Updated package-lock.json 2021-11-15 22:40:16 +00:00
Kayne Ruse c42d84864e Updated documentation 2021-08-21 22:45:27 +01:00
Kayne Ruse 6b01bfaad0 Update package.json 2021-07-23 20:42:48 +10:00
Kayne Ruse 5ae0c1c47c Updated packages 2021-07-23 19:59:36 +10:00
Kayne Ruse 812766d96a Merged private changes 2021-07-23 19:58:08 +10:00
Kayne Ruse 31d19df4a5 Updated .dockerignore 2021-07-23 19:12:53 +10:00
Kayne Ruse 53776438a9 Updated packages 2021-07-15 09:25:14 +10:00
Kayne Ruse 488b932282 Updated packages 2021-04-28 21:29:08 +10:00
Kayne Ruse 370d7905eb Renaming database primary key 2021-04-23 21:20:22 +10:00
Kayne Ruse 0e88c9e64c Updated modules 2021-04-20 18:50:39 +10:00
Kayne Ruse 254e5f2d18 Updated packages 2021-04-19 06:05:27 +10:00
Kayne Ruse e077d4b2d5 Updated .dockerignore 2021-04-16 16:25:24 +10:00
Kayne Ruse 14c4f52a93 Added .dockerignore 2021-04-16 13:24:38 +10:00
Kayne Ruse 8556465796 Tweaked package.json 2021-04-10 04:59:24 +10:00
Kayne Ruse 37e6c35b9c Ripped out unneeded stuff 2021-04-07 02:00:33 +10:00
Kayne Ruse b503df3399 Funky config stuff gives me a headache 2021-04-03 03:26:59 +11:00
Kayne Ruse 928c76fc84 Tweaked Dockerfile 2021-04-03 02:35:00 +11:00
Kayne Ruse ee5394f895 Tweaked Dockerfile 2021-04-03 02:11:21 +11:00
Kayne Ruse 7b85bb1aeb Formatting errors 2021-04-03 01:59:30 +11:00
Kayne Ruse 028766c82b Working on multiplatform things 2021-04-03 01:42:37 +11:00
Kayne Ruse 266cf4070f Added docker publishing 2021-04-03 01:08:10 +11:00
19 changed files with 817 additions and 3162 deletions
+10
View File
@@ -0,0 +1,10 @@
.git*
tools*
mysql*
letsencrypt*
test*
.env*
.github*
LICENSE*
+5 -1
View File
@@ -1,6 +1,10 @@
WEB_PORT=3300
DB_HOSTNAME=database
WEB_ORIGIN=http://localhost:3001
DB_HOSTNAME=localhost
DB_PORTNAME=3306
DB_DATABASE=chat
DB_USERNAME=chat
DB_PASSWORD=blastoise
+5
View File
@@ -0,0 +1,5 @@
# These are supported funding model platforms
patreon: krgamestudios
ko_fi: krgamestudios
custom: ["https://www.paypal.com/donate/?hosted_button_id=73Q82T2ZHV8AA"]
+1 -1
View File
@@ -26,7 +26,7 @@ jobs:
docker_image: krgamestudios/chat-server
- name: Login to DockerHub
uses: docker/login-action@v1
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
+2 -2
View File
@@ -1,7 +1,7 @@
FROM node:15
FROM node:22-bookworm-slim
WORKDIR "/app"
COPY package*.json ./
COPY package*.json /app
RUN npm install --production
COPY . /app
EXPOSE 3300
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright (c) 2021 Kayne Ruse, KR Game Studios
Copyright (c) 2021-2023 Kayne Ruse, KR Game Studios
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
+23 -2
View File
@@ -2,13 +2,34 @@
An API centric chat server. Uses Sequelize and mariaDB by default.
This server is available via docker hub at krgamestudios/chat-server.
# Setup
There are multiple ways to run this app - it can run on it's own via `npm start` (for production) or `npm run dev` (for development). it can also run inside docker using `docker-compose up --build` - run `node configure-script.js` to generate docker-compose.yml.
There are multiple ways to run this app - it can run on it's own via `npm start` (for production) or `npm run dev` (for development). it can also run inside docker using `docker compose up --build` - run `node configure-script.js` to generate docker-compose.yml and startup.sql.
To generate an authorization token, use [auth-server](https://github.com/krgamestudios/auth-server). A public-facing development auth-server is available here (tokens are valid for 10 minutes):
```
POST https://dev-auth.krgamestudios.com/auth/login HTTP/1.1
Content-Type: application/json
{
"email": "example@example.com",
"password": "helloworld"
}
```
# API
This server uses socket.io for communication. Be aware that every chat message requires a valid JWT. See the [auth-server](https://github.com/krgamestudios/auth-server) for details.
This server uses socket.io for communication. Be aware that every 'open chat', 'message' and 'report' signal requires a valid JWT, as part of the message:
```js
socket.emit('message', {
accessToken,
text: inputRef.current.value
});
```
The event types are as follows:
+59 -28
View File
@@ -30,10 +30,30 @@ const question = (prompt, def = null) => {
//project configuration
const appName = await question('App Name', 'chat');
const appWebAddress = await question('Web Addr', `${appName}.example.com`);
const appWebOrigin = await question('Web Origin', `https://example.com`); //TODO: clean these up properly
const appPort = await question('App Port', '3300');
//configure the database address
let dbLocation = '';
while (typeof dbLocation != 'string' || /^[le]/i.test(dbLocation[0]) == false) {
dbLocation = await question('[l]ocal or [e]xternal database?');
}
let appDBHost = '';
let appDBPort = '';
if (/^[l]/i.test(dbLocation[0])) {
appDBHost = 'database';
appDBPort = '3306';
}
else {
appDBHost = await question('DB Host');
appDBPort = await question('DB Port', '3306');
}
//configure the database account
const appDBUser = await question('DB User', appName);
const appDBPass = await question('DB Pass', uuid());
const appDBPass = await question('DB Pass', 'blastoise');
const dbRootPass = await question('DB Root Pass');
const appSecretAccess = await question('Access Token Secret', uuid(32));
@@ -42,73 +62,84 @@ const question = (prompt, def = null) => {
//generate the files
const ymlfile = `
version: '3'
services:
${appName}:
build:
context: .
ports:
- "${appPort}"
- ${appPort}
labels:
- "traefik.enable=true"
- "traefik.http.routers.${appName}router.rule=Host(\`${appWebAddress}\`)"
- "traefik.http.routers.${appName}router.entrypoints=websecure"
- "traefik.http.routers.${appName}router.tls.certresolver=myresolver"
- "traefik.http.routers.${appName}router.service=${appName}service@docker"
- "traefik.http.services.${appName}service.loadbalancer.server.port=${appPort}"
- traefik.enable=true
- traefik.http.routers.${appName}router.rule=Host(\`${appWebAddress}\`)
- traefik.http.routers.${appName}router.entrypoints=websecure
- traefik.http.routers.${appName}router.tls.certresolver=myresolver
- traefik.http.routers.${appName}router.service=${appName}service@docker
- traefik.http.services.${appName}service.loadbalancer.server.port=${appPort}
environment:
- WEB_PORT=${appPort}
- DB_HOSTNAME=database
- WEB_ORIGIN=${appWebOrigin}
- DB_HOSTNAME=${appDBHost}
- DB_PORTNAME=${appDBPort}
- DB_DATABASE=${appName}
- DB_USERNAME=${appDBUser}
- DB_PASSWORD=${appDBPass}
- DB_TIMEZONE=Australia/Sydney
- SECRET_ACCESS=${appSecretAccess}
volumes:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
networks:
- app-network
- app-network${ appDBHost != 'database' ? '' : `
depends_on:
- database
database:
image: mariadb:latest
environment:
MYSQL_DATABASE: ${appName}
MYSQL_TCP_PORT: ${appDBPort}
MYSQL_USER: ${appDBUser}
MYSQL_PASSWORD: ${appDBPass}
MYSQL_ROOT_PASSWORD: ${dbRootPass}
networks:
networks:
- app-network
volumes:
- ./mysql:/var/lib/mysql
- ./startup.sql:/docker-entrypoint-initdb.d/startup.sql:ro
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro`}
traefik_${appName}:
container_name: ${appName}_traefik
image: "traefik:v2.4"
container_name: "traefik"
image: traefik:v2.10
container_name: traefik
command:
- "--log.level=ERROR"
- "--api.insecure=false"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.myresolver.acme.tlschallenge=true"
- "--certificatesresolvers.myresolver.acme.email=${supportEmail}"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
- --log.level=ERROR
- --api.insecure=false
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --entrypoints.websecure.address=:443
- --certificatesresolvers.myresolver.acme.tlschallenge=true
- --certificatesresolvers.myresolver.acme.email=${supportEmail}
- --certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json
ports:
- "80:80"
- "443:443"
- 80:80
- 443:443
volumes:
- "./letsencrypt:/letsencrypt"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- ./letsencrypt:/letsencrypt
- /var/run/docker.sock:/var/run/docker.sock:ro
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
networks:
- app-network
networks:
app-network:
driver: bridge
`;
const dockerfile = `
FROM node:15
FROM node:22-bookworm-slim
WORKDIR "/app"
COPY package*.json ./
RUN npm install --production
+662 -3085
View File
File diff suppressed because it is too large Load Diff
+9 -10
View File
@@ -1,6 +1,6 @@
{
"name": "chat-server",
"version": "1.0.0",
"version": "1.5.1",
"description": "An API centric chat server. Uses Sequelize and mariaDB by default.",
"main": "server/server.js",
"scripts": {
@@ -13,22 +13,21 @@
"url": "git+https://github.com/krgamestudios/chat-server.git"
},
"author": "Kayne Ruse",
"license": "ISC",
"license": "Zlib",
"bugs": {
"url": "https://github.com/krgamestudios/chat-server/issues"
},
"homepage": "https://github.com/krgamestudios/chat-server#readme",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mariadb": "^2.5.2",
"sequelize": "^6.5.0",
"socket.io": "^4.0.0"
"dotenv": "^16.4.5",
"express": "^4.19.2",
"jsonwebtoken": "^9.0.2",
"mariadb": "^3.3.0",
"sequelize": "^6.37.3",
"socket.io": "^4.7.5"
},
"devDependencies": {
"nodemon": "^2.0.7"
"nodemon": "^3.1.0"
}
}
+1 -1
View File
@@ -4,7 +4,7 @@ const { chatlog, reports } = require('../database/models');
const route = async (req, res) => {
const reps = await reports.destroy({
where: {
chatlogId: req.body.chatlogId
chatlogIndex: req.body.chatlogIndex
}
});
+2 -2
View File
@@ -7,14 +7,14 @@ const route = async (req, res) => {
model: chatlog,
required: true
}],
order: ['chatlogId']
order: ['chatlogIndex']
});
//collate
const response = [];
for(let i = 0; i < reps.length; i++) {
//new chatlog
if (response.length == 0 || response[response.length - 1].chatlogId != reps[i].chatlogId) {
if (response.length == 0 || response[response.length - 1].chatlogIndex != reps[i].chatlogIndex) {
response.push(reps[i]);
response[response.length - 1].reporter = [response[response.length - 1].reporter]; //reporters in an array
continue;
+9 -9
View File
@@ -35,7 +35,7 @@ const chat = io => {
});
//from here, handles all normal messages
socket.on('open chat', message => {
socket.on('open chat', async message => {
//handle rooms - only in a room if you've opened chat
const newlyOpened = !socket.user.room;
socket.user.room = socket.user.room || 'general'; //default to general
@@ -46,11 +46,8 @@ const chat = io => {
socket.join(socket.user.room);
//broadcast to this room
socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} entered chat` });
//log
chatlog.create({
const log = await chatlog.create({
notification: true,
username: socket.user.username,
text: `${socket.user.username} entered chat`,
@@ -58,6 +55,9 @@ const chat = io => {
emphasis: true
});
//broadcast to this room
socket.broadcast.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} entered chat` });
//send backlog to the user
chatlog.findAll({
where: {
@@ -72,7 +72,7 @@ const chat = io => {
}
},
order: [
['id', 'DESC']
['index', 'DESC']
],
limit: 50
})
@@ -120,7 +120,7 @@ const chat = io => {
socket.broadcast.to(socket.user.room).emit('message', log);
});
socket.on('disconnect', reason => {
socket.on('disconnect', async reason => {
//broadcast to this room
if (!socket.user) {
return;
@@ -140,14 +140,14 @@ const chat = io => {
socket.on('report', info => {
//handle reports of malicious content
if (!info.id) {
if (!info.index) {
return;
}
//report
reports.create({
reporter: socket.user.username,
chatlogId: info.id
chatlogIndex: info.index
});
});
});
+1 -2
View File
@@ -2,11 +2,10 @@ const Sequelize = require('sequelize');
const sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
host: process.env.DB_HOSTNAME,
port: process.env.DB_PORTNAME,
dialect: 'mariadb',
timezone: process.env.DB_TIMEZONE,
logging: process.env.DB_LOGGING ? console.log : false
});
sequelize.sync();
module.exports = sequelize;
+1 -1
View File
@@ -2,7 +2,7 @@ const Sequelize = require('sequelize');
const sequelize = require('..');
module.exports = sequelize.define('chatlog', {
id: {
index: {
type: Sequelize.INTEGER(11),
allowNull: false,
autoIncrement: true,
+11 -4
View File
@@ -1,5 +1,12 @@
const chatlog = require('./chatlog');
const mute = require('./mute');
const reports = require('./reports');
//relationships
reports.belongsTo(chatlog);
module.exports = {
chatlog: require('./chatlog'),
mute: require('./mute'),
reports: require('./reports')
};
chatlog,
mute,
reports,
};
+2 -9
View File
@@ -1,10 +1,8 @@
const Sequelize = require('sequelize');
const sequelize = require('..');
const chatlog = require('./chatlog');
const reports = sequelize.define('reports', {
id: {
module.exports = sequelize.define('reports', {
index: {
type: Sequelize.INTEGER(11),
allowNull: false,
autoIncrement: true,
@@ -17,8 +15,3 @@ const reports = sequelize.define('reports', {
allowNull: false
},
});
chatlog.hasMany(reports, { foreignKey: 'chatlogId', foreignKeyConstraint: true });
reports.belongsTo(chatlog, { foreignKey: 'chatlogId' });
module.exports = reports;
+9 -4
View File
@@ -7,15 +7,19 @@ const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server, {
cors: {
origin: '*'
origin: process.env.WEB_ORIGIN
}
});
const bodyParser = require('body-parser');
const cors = require('cors');
//config
app.use(bodyParser.json());
app.use(cors());
app.use(express.json());
app.use(cors({
credentials: true,
origin: [`${process.env.WEB_ORIGIN}`], //because auth-server
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'Set-Cookie'],
exposedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'Set-Cookie'],
}));
//database connection
const database = require('./database');
@@ -35,4 +39,5 @@ app.get('*', (req, res) => {
server.listen(process.env.WEB_PORT || 3300, async (err) => {
await database.sync();
console.log(`listening to localhost:${process.env.WEB_PORT || 3300}`);
console.log(`database located at ${process.env.DB_HOSTNAME || '<default>'}:${process.env.DB_PORTNAME || '<default>'}`);
});
+4
View File
@@ -0,0 +1,4 @@
#use this while debugging
CREATE DATABASE chat;
CREATE USER 'chat'@'%' IDENTIFIED BY 'blastoise';
GRANT ALL PRIVILEGES ON chat.* TO 'chat'@'%';