Migrate to CommonJS
This commit is contained in:
parent
3c1f0e4c48
commit
ec605b49fc
6 changed files with 82 additions and 60 deletions
|
@ -1,8 +1,8 @@
|
|||
import express from 'express';
|
||||
import basicAuth from 'express-basic-auth';
|
||||
import { randomId } from './util.js';
|
||||
import { basicAuthConfig } from './config.js'
|
||||
import pool from './db.js';
|
||||
const express = require('express');
|
||||
const basicAuth = require('express-basic-auth');
|
||||
const randomId = require('./util.js').randomId;
|
||||
const basicAuthConfig = require('./config.js').basicAuthConfig;
|
||||
const pool = require('./db.js');
|
||||
|
||||
pool.query(`CREATE TABLE IF NOT EXISTS apps (
|
||||
id VARCHAR(32) PRIMARY KEY,
|
||||
|
@ -11,7 +11,7 @@ pool.query(`CREATE TABLE IF NOT EXISTS apps (
|
|||
if (err) console.error(err);
|
||||
});
|
||||
|
||||
export default class App {
|
||||
class App {
|
||||
id = randomId(32);
|
||||
name;
|
||||
|
||||
|
@ -20,7 +20,7 @@ export default class App {
|
|||
}
|
||||
}
|
||||
|
||||
export class AppRepository {
|
||||
class AppRepository {
|
||||
static getApps() {
|
||||
return new Promise((resolve, reject) => {
|
||||
pool.query('SELECT * FROM apps', (err, res) => {
|
||||
|
@ -87,10 +87,10 @@ export class AppRepository {
|
|||
}
|
||||
}
|
||||
|
||||
export const appRouter = express.Router();
|
||||
appRouter.use(basicAuth(basicAuthConfig));
|
||||
const router = express.Router();
|
||||
router.use(basicAuth(basicAuthConfig));
|
||||
|
||||
appRouter.get('/', (req, res) => {
|
||||
router.get('/', (req, res) => {
|
||||
AppRepository.getApps()
|
||||
.then((apps) => {
|
||||
res.json(apps);
|
||||
|
@ -100,7 +100,7 @@ appRouter.get('/', (req, res) => {
|
|||
})
|
||||
});
|
||||
|
||||
appRouter.post('/', basicAuth(basicAuthConfig), (req, res) => {
|
||||
router.post('/', basicAuth(basicAuthConfig), (req, res) => {
|
||||
const name = req.body.name;
|
||||
if (!name) {
|
||||
res.status(400).send({ message: 'Invalid app name' });
|
||||
|
@ -116,7 +116,7 @@ appRouter.post('/', basicAuth(basicAuthConfig), (req, res) => {
|
|||
})
|
||||
});
|
||||
|
||||
appRouter.get('/:appId', basicAuth(basicAuthConfig), (req, res) => {
|
||||
router.get('/:appId', basicAuth(basicAuthConfig), (req, res) => {
|
||||
AppRepository.getApp(req.params.appId)
|
||||
.then((app) => {
|
||||
if (!app) {
|
||||
|
@ -130,7 +130,7 @@ appRouter.get('/:appId', basicAuth(basicAuthConfig), (req, res) => {
|
|||
})
|
||||
})
|
||||
|
||||
appRouter.patch('/:appId', (req, res) => {
|
||||
router.patch('/:appId', (req, res) => {
|
||||
AppRepository.updateApp(req.params.appId, req.body.name)
|
||||
.then((app) => {
|
||||
if (!app) {
|
||||
|
@ -144,7 +144,7 @@ appRouter.patch('/:appId', (req, res) => {
|
|||
})
|
||||
})
|
||||
|
||||
appRouter.delete('/:appId', (req, res) => {
|
||||
router.delete('/:appId', (req, res) => {
|
||||
AppRepository.deleteApp(req.params.appId)
|
||||
.then((app) => {
|
||||
res.send(204);
|
||||
|
@ -152,3 +152,9 @@ appRouter.delete('/:appId', (req, res) => {
|
|||
res.status(500).send();
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = {
|
||||
App,
|
||||
AppRepository,
|
||||
router
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
const users = {};
|
||||
users[process.env.ADMIN_USER || 'admin'] = process.env.ADMIN_USER || 'flayre'
|
||||
export const basicAuthConfig = {
|
||||
const basicAuthConfig = {
|
||||
users: users
|
||||
};
|
||||
|
||||
export const dbConfig = {
|
||||
const dbConfig = {
|
||||
connectionLimit: process.env.DB_POOL_SIZE || 10,
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: process.env.DB_PORT || 3306,
|
||||
|
@ -13,4 +13,10 @@ export const dbConfig = {
|
|||
database: process.env.DB_NAME || 'flayre'
|
||||
}
|
||||
|
||||
export const port = process.env.PORT || 3000;
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
module.exports = {
|
||||
basicAuthConfig,
|
||||
dbConfig,
|
||||
port,
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
import mysql from 'mysql';
|
||||
import { dbConfig } from './config.js';
|
||||
const mysql = require('mysql');
|
||||
const dbConfig = require('./config.js').dbConfig;
|
||||
|
||||
const pool = mysql.createPool(dbConfig);
|
||||
|
||||
export default pool
|
||||
module.exports = pool
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
import { randomId, firstOfMonth, lastOfMonth } from './util.js';
|
||||
import pool from './db.js';
|
||||
import express from 'express';
|
||||
import basicAuth from 'express-basic-auth';
|
||||
import { basicAuthConfig } from './config.js'
|
||||
const utils = require('./util.js');
|
||||
const randomId = utils.randomId;
|
||||
const firstOfMonth = utils.firstOfMonth;
|
||||
const lastOfMonth = utils.lastOfMonth;
|
||||
const pool = require('./db.js');
|
||||
const express = require('express');
|
||||
const basicAuth = require('express-basic-auth');
|
||||
const basicAuthConfig = require('./config.js')
|
||||
|
||||
export default class Event {
|
||||
class Event {
|
||||
static types = [
|
||||
'view',
|
||||
'click',
|
||||
|
@ -88,7 +91,7 @@ pool.query(`CREATE TABLE IF NOT EXISTS events (
|
|||
ON DELETE CASCADE
|
||||
)`);
|
||||
|
||||
export class EventRepository {
|
||||
class EventRepository {
|
||||
static getEvents(
|
||||
appId,
|
||||
from,
|
||||
|
@ -142,8 +145,8 @@ export class EventRepository {
|
|||
}
|
||||
}
|
||||
|
||||
export const eventRouter = express.Router()
|
||||
eventRouter.get('/', basicAuth(basicAuthConfig), (req, res) => {
|
||||
const router = express.Router()
|
||||
router.get('/', basicAuth(basicAuthConfig), (req, res) => {
|
||||
const appId = req.query.appId;
|
||||
if (!appId) {
|
||||
res.status(400).send({ message: 'Invalid appId' });
|
||||
|
@ -164,7 +167,7 @@ eventRouter.get('/', basicAuth(basicAuthConfig), (req, res) => {
|
|||
// events will be coming from all over the place, I don't think it makes
|
||||
// sense to try to put auth in front of this. Even some kind of client
|
||||
// "secret" would be trivial to deduce by examining the requests.
|
||||
eventRouter.post('/', (req, res) => {
|
||||
router.post('/', (req, res) => {
|
||||
if (typeof req.body.appId === "undefined") {
|
||||
res.status(400).json({ message: 'Invalid appId' });
|
||||
return;
|
||||
|
@ -206,3 +209,9 @@ eventRouter.post('/', (req, res) => {
|
|||
res.sendStatus(500);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
Event,
|
||||
EventRepository,
|
||||
router
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import express from 'express';
|
||||
import { eventRouter } from './event.js';
|
||||
import { port } from './config.js';
|
||||
import { randomId } from './util.js';
|
||||
import { appRouter } from './app.js';
|
||||
const express = require('express');
|
||||
const eventRouter = require('./event.js').router;
|
||||
const port = require('./config.js').port;
|
||||
const randomId = require('./util.js').randomId;
|
||||
const appRouter = require('./app.js').router;
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
@ -16,8 +16,8 @@ app.get('/id', (req, res) => {
|
|||
res.send(randomId(length));
|
||||
});
|
||||
|
||||
app.use('/apps', appRouter)
|
||||
app.use('/events', eventRouter)
|
||||
app.use('/api/apps', appRouter)
|
||||
app.use('/api/events', eventRouter)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Started Flayre server on port ${port}`);
|
||||
|
|
|
@ -1,23 +1,25 @@
|
|||
export function randomId(length) {
|
||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
let id = ""
|
||||
while (id.length < length) {
|
||||
id += characters[Math.floor(Math.random() * characters.length)]
|
||||
module.exports = {
|
||||
randomId: function (length) {
|
||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
let id = ""
|
||||
while (id.length < length) {
|
||||
id += characters[Math.floor(Math.random() * characters.length)]
|
||||
}
|
||||
return id
|
||||
},
|
||||
|
||||
firstOfMonth: function () {
|
||||
const d = new Date();
|
||||
d.setUTCDate(1);
|
||||
d.setUTCHours(0, 0, 0, 0);
|
||||
return d;
|
||||
},
|
||||
|
||||
lastOfMonth: function () {
|
||||
const d = new Date();
|
||||
d.setUTCMonth(d.getUTCMonth() + 1)
|
||||
d.setUTCDate(0);
|
||||
d.setUTCHours(23, 59, 59, 999);
|
||||
return d;
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
export function firstOfMonth() {
|
||||
const d = new Date();
|
||||
d.setUTCDate(1);
|
||||
d.setUTCHours(0, 0, 0, 0);
|
||||
return d;
|
||||
}
|
||||
|
||||
export function lastOfMonth() {
|
||||
const d = new Date();
|
||||
d.setUTCMonth(d.getUTCMonth() + 1)
|
||||
d.setUTCDate(0);
|
||||
d.setUTCHours(23, 59, 59, 999);
|
||||
return d;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue