Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5488d7a8d7 | |||
| 3bb0b2aa29 | |||
| c30cf80fc6 | |||
| 62afef402f | |||
| db61357a0f | |||
| fd1ba06b45 | |||
| 1b9e35539e | |||
| 19a4051159 | |||
| 6fee9090e7 | |||
| 5c9ee58f41 | |||
| f56f1e859b | |||
| 8504017cf7 | |||
| 5d61cf3cdc | |||
| 5d721ddea6 | |||
| 733927966a | |||
| c38f700e93 | |||
| f59fc2e8e0 | |||
| 70712b9a87 | |||
| 4ff507b0fa | |||
| 7ebced9304 | |||
| 028bcc99dd | |||
| 74869993b0 | |||
| bac87e9c33 | |||
| afa35939fa | |||
| 1c32e42b47 | |||
| c72c933c0b | |||
| 3c7add4807 |
@@ -0,0 +1,10 @@
|
|||||||
|
.git*
|
||||||
|
|
||||||
|
tools*
|
||||||
|
mysql*
|
||||||
|
letsencrypt*
|
||||||
|
test*
|
||||||
|
|
||||||
|
.env*
|
||||||
|
.github*
|
||||||
|
LICENSE*
|
||||||
@@ -3,9 +3,16 @@ WEB_PORT=3100
|
|||||||
DB_HOSTNAME=database
|
DB_HOSTNAME=database
|
||||||
DB_DATABASE=news
|
DB_DATABASE=news
|
||||||
DB_USERNAME=news
|
DB_USERNAME=news
|
||||||
DB_PASSWORD=charizard
|
DB_PASSWORD=venusaur
|
||||||
|
|
||||||
|
# Select a "TZ database name" that suits your needs: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
||||||
DB_TIMEZONE=Australia/Sydney
|
DB_TIMEZONE=Australia/Sydney
|
||||||
|
|
||||||
QUERY_LIMIT=10
|
# Give this any value to enable database logging (such as "true")
|
||||||
|
DB_LOGGING=
|
||||||
|
|
||||||
SECRET_ACCESS=access
|
# Make sure this value matches the system that you connect to
|
||||||
|
SECRET_ACCESS=access
|
||||||
|
|
||||||
|
# Select the default number of articles returned by a GET request
|
||||||
|
QUERY_LIMIT=10
|
||||||
@@ -26,7 +26,7 @@ jobs:
|
|||||||
docker_image: krgamestudios/news-server
|
docker_image: krgamestudios/news-server
|
||||||
|
|
||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v1
|
uses: docker/login-action@v1
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
FROM node:15
|
FROM node:16
|
||||||
WORKDIR "/app"
|
WORKDIR "/app"
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install --production
|
RUN npm install --production
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ An API centric news server. Uses Sequelize and mariaDB by default.
|
|||||||
|
|
||||||
# Setup
|
# 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 10 minutes):
|
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.eggtrainer.com/auth/login HTTP/1.1
|
POST https://dev-auth.krgamestudios.com/auth/login HTTP/1.1
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -25,13 +25,21 @@ Content-Type: application/json
|
|||||||
//NOTE: you can add a "limit" query parameter to change the default limit
|
//NOTE: you can add a "limit" query parameter to change the default limit
|
||||||
GET /news?limit=10
|
GET /news?limit=10
|
||||||
|
|
||||||
//get latest news, up to a default limit, or specify the index "id"
|
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
//DOCS: get latest news, up to a default limit, or specify the index "id"
|
||||||
GET /news/:id
|
GET /news/:id
|
||||||
|
|
||||||
//get the news starting from the beginning, up to a default limit, or specify the index "id"
|
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
//DOCS: get the news starting from the beginning, up to a default limit, or specify the index "id"
|
||||||
GET /news/archive/:id
|
GET /news/archive/:id
|
||||||
|
|
||||||
//result (if only a single article is specified, returns just that article rather than an array):
|
//DOCS: result (if only a single article is specified, returns just that article rather than an array):
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"index": index, //absolute index of the result
|
"index": index, //absolute index of the result
|
||||||
@@ -45,13 +53,21 @@ GET /news/archive/:id
|
|||||||
...
|
...
|
||||||
]
|
]
|
||||||
|
|
||||||
//get the latest metadata, up to a default limit, or specify the index "id"
|
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
//DOCS: get the latest metadata, up to a default limit, or specify the index "id"
|
||||||
GET /news/metadata/:id
|
GET /news/metadata/:id
|
||||||
|
|
||||||
//get the metadata starting from the beginning, up to a default limit, or specify the index "id"
|
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
//DOCS: get the metadata starting from the beginning, up to a default limit, or specify the index "id"
|
||||||
GET /news/archive/metadata/:id
|
GET /news/archive/metadata/:id
|
||||||
|
|
||||||
//result (if only a single article is specified, returns just that article rather than an array):
|
//DOCS: result (if only a single article is specified, returns just that article rather than an array):
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"index": index, //absolute index of the result
|
"index": index, //absolute index of the result
|
||||||
@@ -64,38 +80,51 @@ GET /news/archive/metadata/:id
|
|||||||
...
|
...
|
||||||
]
|
]
|
||||||
|
|
||||||
//send a formatted JSON object, returns new index on success, or error on failure
|
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
//DOCS: send a formatted JSON object, returns new index on success, or error on failure
|
||||||
POST /news
|
POST /news
|
||||||
Authorization: Bearer XXX
|
Authorization: Bearer XXX
|
||||||
|
|
||||||
//arguments:
|
|
||||||
{
|
{
|
||||||
"title": title //title of the article
|
"title": title //title of the article
|
||||||
"author": author //author of the article
|
"author": author //author of the article
|
||||||
"body": body //body of the article
|
"body": body //body of the article
|
||||||
}
|
}
|
||||||
|
|
||||||
//result (status 200 on success, otherwise an error status):
|
//DOCS: result (status 200 on success, otherwise an error status):
|
||||||
{
|
{
|
||||||
"index": index //new index of the article
|
"index": index //new index of the article
|
||||||
}
|
}
|
||||||
|
|
||||||
//similar to `POST /news`, but allows overwriting an existing article
|
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
//DOCS: similar to `POST /news`, but allows overwriting an existing article
|
||||||
PATCH /news/:id
|
PATCH /news/:id
|
||||||
Authorization: Bearer XXX
|
Authorization: Bearer XXX
|
||||||
|
|
||||||
//arguments:
|
|
||||||
{
|
{
|
||||||
"title": title //title of the article, optional
|
"title": title //title of the article, optional
|
||||||
"author": author //author of the article, optional
|
"author": author //author of the article, optional
|
||||||
"body": body //body of the article, optional
|
"body": body //body of the article, optional
|
||||||
}
|
}
|
||||||
|
|
||||||
result: status 200 on success, otherwise an error status
|
//DOCS: result: status 200 on success, otherwise an error status
|
||||||
|
|
||||||
//remove an article from the news feed
|
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
//DOCS: remove an article from the news feed
|
||||||
DELETE /news/:id
|
DELETE /news/:id
|
||||||
Authorization: Bearer XXX
|
Authorization: Bearer XXX
|
||||||
|
|
||||||
//result: status 200 on success, otherwise an error status
|
//DOCS: result: status 200 on success, otherwise an error status
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
```
|
```
|
||||||
|
|||||||
+1
-1
@@ -109,7 +109,7 @@ networks:
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const dockerfile = `
|
const dockerfile = `
|
||||||
FROM node:15
|
FROM node:16
|
||||||
WORKDIR "/app"
|
WORKDIR "/app"
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install --production
|
RUN npm install --production
|
||||||
|
|||||||
Generated
+231
-358
File diff suppressed because it is too large
Load Diff
+5
-6
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "news-server",
|
"name": "news-server",
|
||||||
"version": "1.0.0",
|
"version": "1.3.1",
|
||||||
"description": "An API centric news server. Uses Sequelize and mariaDB by default.",
|
"description": "An API centric news server. Uses Sequelize and mariaDB by default.",
|
||||||
"main": "server/server.js",
|
"main": "server/server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -19,15 +19,14 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/krgamestudios/news-server#readme",
|
"homepage": "https://github.com/krgamestudios/news-server#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.19.0",
|
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.6.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"mariadb": "^2.5.2",
|
"mariadb": "^2.5.4",
|
||||||
"sequelize": "^6.5.0"
|
"sequelize": "^6.6.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^2.0.7"
|
"nodemon": "^2.0.12"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
const Sequelize = require('sequelize');
|
const Sequelize = require('sequelize');
|
||||||
const sequelize = require('..');
|
const sequelize = require('..');
|
||||||
|
|
||||||
const articles = require('./articles');
|
|
||||||
|
|
||||||
const revisions = sequelize.define('revisions', {
|
const revisions = sequelize.define('revisions', {
|
||||||
title: {
|
title: {
|
||||||
type: Sequelize.TEXT,
|
type: Sequelize.TEXT,
|
||||||
@@ -17,12 +15,15 @@ const revisions = sequelize.define('revisions', {
|
|||||||
body: {
|
body: {
|
||||||
type: Sequelize.TEXT,
|
type: Sequelize.TEXT,
|
||||||
defaultValue: ''
|
defaultValue: ''
|
||||||
|
},
|
||||||
|
|
||||||
|
originalIndex: {
|
||||||
|
type: Sequelize.INTEGER(11),
|
||||||
|
default: null
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//relationships
|
//relationships
|
||||||
articles.hasOne(revisions, { as: 'original' });
|
|
||||||
|
|
||||||
sequelize.sync();
|
sequelize.sync();
|
||||||
|
|
||||||
module.exports = revisions;
|
module.exports = revisions;
|
||||||
+3
-3
@@ -25,9 +25,9 @@ const route = async (req, res) => {
|
|||||||
|
|
||||||
//update the data
|
//update the data
|
||||||
await articles.update({
|
await articles.update({
|
||||||
title: req.body.title,
|
title: req.body.title || record.title,
|
||||||
author: req.body.author,
|
author: req.body.author || record.author,
|
||||||
body: req.body.body,
|
body: req.body.body || record.body,
|
||||||
edits: record.edits + 1
|
edits: record.edits + 1
|
||||||
}, {
|
}, {
|
||||||
where: {
|
where: {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ router.get('/archive/metadata/:id(\\d+)', query(true, true));
|
|||||||
//use middleware to authenticate the rest of the routes
|
//use middleware to authenticate the rest of the routes
|
||||||
router.use(authToken);
|
router.use(authToken);
|
||||||
router.use((req, res, next) => {
|
router.use((req, res, next) => {
|
||||||
if (req.user.privilege == 'administrator') {
|
if (req.user.admin) {
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
res.status(403).end();
|
res.status(403).end();
|
||||||
|
|||||||
@@ -1,6 +1,19 @@
|
|||||||
const { articles } = require('../database/models');
|
const { articles } = require('../database/models');
|
||||||
|
|
||||||
const route = async (req, res) => {
|
const route = async (req, res) => {
|
||||||
|
//check for missing data
|
||||||
|
if (!req.body.title) {
|
||||||
|
return res.status(401).end("Missing title");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!req.body.author) {
|
||||||
|
return res.status(401).end("Missing author");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!req.body.body) {
|
||||||
|
return res.status(401).end("Missing body");
|
||||||
|
}
|
||||||
|
|
||||||
//upsert the data
|
//upsert the data
|
||||||
const [instance, created] = await articles.upsert({
|
const [instance, created] = await articles.upsert({
|
||||||
title: req.body.title,
|
title: req.body.title,
|
||||||
|
|||||||
+1
-2
@@ -5,11 +5,10 @@ require('dotenv').config();
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = require('http').Server(app);
|
const server = require('http').Server(app);
|
||||||
const bodyParser = require('body-parser');
|
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
|
|
||||||
//config
|
//config
|
||||||
app.use(bodyParser.json());
|
app.use(express.json());
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
//database connection
|
//database connection
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
use news;
|
||||||
|
ALTER TABLE revisions CHANGE COLUMN id `index` INTEGER(11) UNIQUE NOT NULL AUTO_INCREMENT;
|
||||||
|
|
||||||
|
ALTER TABLE revisions DROP FOREIGN KEY revisions_ibfk_1;
|
||||||
|
|
||||||
|
ALTER TABLE revisions CHANGE COLUMN originalIndex originalIndex INTEGER(11);
|
||||||
Reference in New Issue
Block a user