mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-03 21:39:08 +00:00
setup router + tidy up
This commit is contained in:
@@ -10,3 +10,4 @@ export const serverURL = calculateServer();
|
|||||||
export const eventURL = serverURL + EVENT_TABLE;
|
export const eventURL = serverURL + EVENT_TABLE;
|
||||||
export const eventsURL = serverURL + EVENTS_TABLE;
|
export const eventsURL = serverURL + EVENTS_TABLE;
|
||||||
export const playbackURL = serverURL + 'playback';
|
export const playbackURL = serverURL + 'playback';
|
||||||
|
export const ontimeURL = serverURL + 'ontime';
|
||||||
|
|||||||
@@ -1,21 +1,6 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { eventsURL } from '../api/apiConstants';
|
import { eventsURL } from '../api/apiConstants';
|
||||||
|
|
||||||
export const downloadEvents = async () => {
|
|
||||||
await axios({
|
|
||||||
url: eventsURL + '/download',
|
|
||||||
method: 'GET',
|
|
||||||
responseType: 'blob', // important
|
|
||||||
}).then((response) => {
|
|
||||||
const url = window.URL.createObjectURL(new Blob([response.data]));
|
|
||||||
const link = document.createElement('a');
|
|
||||||
link.href = url;
|
|
||||||
link.setAttribute('download', 'events.json');
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const fetchAllEvents = async () => {
|
export const fetchAllEvents = async () => {
|
||||||
const res = await axios.get(eventsURL);
|
const res = await axios.get(eventsURL);
|
||||||
return res.data;
|
return res.data;
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
import { ontimeURL } from './apiConstants';
|
||||||
|
|
||||||
|
export const downloadEvents = async () => {
|
||||||
|
await axios({
|
||||||
|
url: ontimeURL + '/db',
|
||||||
|
method: 'GET',
|
||||||
|
responseType: 'blob', // important
|
||||||
|
}).then((response) => {
|
||||||
|
const url = window.URL.createObjectURL(new Blob([response.data]));
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = url;
|
||||||
|
link.setAttribute('download', 'events.json');
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
});
|
||||||
|
};
|
||||||
+7
-4
@@ -11,6 +11,7 @@ export const db = new Low(adapter);
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
|
import multer from 'multer';
|
||||||
import { dbModel } from './data/dataModel.js';
|
import { dbModel } from './data/dataModel.js';
|
||||||
|
|
||||||
// Read data from JSON file, this will set db.data content
|
// Read data from JSON file, this will set db.data content
|
||||||
@@ -30,9 +31,7 @@ export const data = db.data;
|
|||||||
// Import Routes
|
// Import Routes
|
||||||
import { router as eventsRouter } from './routes/eventsRouter.js';
|
import { router as eventsRouter } from './routes/eventsRouter.js';
|
||||||
import { router as eventRouter } from './routes/eventRouter.js';
|
import { router as eventRouter } from './routes/eventRouter.js';
|
||||||
|
import { router as ontimeRouter } from './routes/ontimeRouter.js';
|
||||||
// No settings yet
|
|
||||||
// const settingsRouter = require('./routes/settingsRouter.js');
|
|
||||||
|
|
||||||
// Setup default port
|
// Setup default port
|
||||||
const port = process.env.PORT || config.server.port;
|
const port = process.env.PORT || config.server.port;
|
||||||
@@ -50,12 +49,16 @@ app.use(cors());
|
|||||||
app.options('*', cors());
|
app.options('*', cors());
|
||||||
|
|
||||||
// Implement middleware
|
// Implement middleware
|
||||||
|
app.use(express.static('./public'));
|
||||||
|
app.use('/uploads', express.static('uploads'));
|
||||||
|
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({ extended: true }));
|
||||||
app.use(express.json());
|
app.use(express.json({ limit: '1mb' }));
|
||||||
|
|
||||||
// Implement route endpoints
|
// Implement route endpoints
|
||||||
app.use('/events', eventsRouter);
|
app.use('/events', eventsRouter);
|
||||||
app.use('/event', eventRouter);
|
app.use('/event', eventRouter);
|
||||||
|
app.use('/ontime', ontimeRouter);
|
||||||
|
|
||||||
// implement general router
|
// implement general router
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
// Create controller for GET request to '/ontime/download'
|
// Create controller for GET request to '/ontime/download'
|
||||||
|
import { db, data } from '../app.js';
|
||||||
|
function getEventTitle() {
|
||||||
|
return data.event.title;
|
||||||
|
}
|
||||||
// Returns -
|
// Returns -
|
||||||
export const eventsDownload = async (req, res) => {
|
export const dbDownload = async (req, res) => {
|
||||||
res.download('db.json', 'ontime events.json', (err) => {
|
const fileTitle = getEventTitle() || 'ontime events';
|
||||||
|
res.download('db.json', `${fileTitle}.json`, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(500).send({
|
res.status(500).send({
|
||||||
message: 'Could not download the file. ' + err,
|
message: 'Could not download the file. ' + err,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
export const router = express.Router();
|
export const router = express.Router();
|
||||||
|
|
||||||
// create route between controller and '/ontime/download' endpoint
|
import { dbDownload, dbUpload } from '../controllers/ontimeController.js';
|
||||||
router.get('/download', eventsDownload);
|
|
||||||
|
// create route between controller and '/ontime/db' endpoint
|
||||||
|
router.get('/db', dbDownload);
|
||||||
|
|||||||
Reference in New Issue
Block a user