|
1 |
| -import programs from './data/programs.json'; |
| 1 | +import {Db} from './db.js'; |
| 2 | +import {Program} from '@code-differently/types'; |
2 | 3 | import cors from 'cors';
|
3 |
| -import {randomUUID} from 'crypto'; |
4 | 4 | import express, {Express, Request, Response} from 'express';
|
5 |
| -import fs from 'fs'; |
6 |
| -import path from 'path'; |
7 | 5 |
|
8 |
| -import {Program} from '../../types'; |
9 |
| - |
10 |
| -const PROGRAMS_FILE = path.resolve(__dirname, './data/programs.json'); |
11 | 6 | const UUID_PATTERN =
|
12 | 7 | /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
13 | 8 |
|
14 |
| -const app: Express = express(); |
15 |
| - |
16 |
| -app.use(express.static('public')); |
17 |
| -app.use(express.json()); |
18 |
| -app.use(express.urlencoded({extended: true})); |
19 |
| -app.use(cors()); |
| 9 | +export const createServer = (db: Db): Express => { |
| 10 | + const app: Express = express(); |
20 | 11 |
|
21 |
| -app.get('/programs/:id', async (req: Request, res: Response<Program>) => { |
22 |
| - if (!isUuidValid(req.params.id)) { |
23 |
| - res.status(400).send(); |
24 |
| - return; |
25 |
| - } |
26 |
| - const program = programs.find(p => p.id === req.params.id); |
| 12 | + app.use(express.static('public')); |
| 13 | + app.use(express.json()); |
| 14 | + app.use(express.urlencoded({extended: true})); |
| 15 | + app.use(cors()); |
27 | 16 |
|
28 |
| - if (!program) { |
29 |
| - res.status(404).send(); |
30 |
| - return; |
31 |
| - } |
| 17 | + app.get('/programs/:id', async (req: Request, res: Response<Program>) => { |
| 18 | + if (!isUuidValid(req.params.id)) { |
| 19 | + res.status(400).send(); |
| 20 | + return; |
| 21 | + } |
| 22 | + const program = await db.getProgram(req.params.id); |
32 | 23 |
|
33 |
| - res.status(200).send(program); |
34 |
| -}); |
| 24 | + if (!program) { |
| 25 | + res.status(404).send(); |
| 26 | + return; |
| 27 | + } |
35 | 28 |
|
36 |
| -function isUuidValid(uuid: string): boolean { |
37 |
| - return !!uuid && !!uuid.match(UUID_PATTERN); |
38 |
| -} |
| 29 | + res.status(200).send(program); |
| 30 | + }); |
39 | 31 |
|
40 |
| -app.get('/programs', async (req: Request, res: Response<Program[]>) => { |
41 |
| - // Send the raw data back to the client as JSON. |
42 |
| - res.status(200).send(programs); |
43 |
| -}); |
| 32 | + function isUuidValid(uuid: string): boolean { |
| 33 | + return !!uuid && !!uuid.match(UUID_PATTERN); |
| 34 | + } |
44 | 35 |
|
45 |
| -app.post('/programs', async (req: Request<Partial<Program>>, res: Response) => { |
46 |
| - const newProgram = req.body; |
47 |
| - programs.push({id: randomUUID(), ...newProgram}); |
48 |
| - fs.writeFile( |
49 |
| - PROGRAMS_FILE, |
50 |
| - JSON.stringify(programs, null, 2), |
51 |
| - (err: unknown) => { |
52 |
| - if (err) { |
53 |
| - res.status(500).send({error: 'Failed to write to file.'}); |
| 36 | + app.get('/programs', async (req: Request, res: Response<Program[]>) => { |
| 37 | + // Send the raw data back to the client as JSON. |
| 38 | + const programs = await db.getPrograms(); |
| 39 | + res.status(200).send(programs); |
| 40 | + }); |
| 41 | + |
| 42 | + app.post( |
| 43 | + '/programs', |
| 44 | + async (req: Request<Partial<Program>>, res: Response) => { |
| 45 | + const newProgram = req.body; |
| 46 | + try { |
| 47 | + db.addProgram(newProgram as Program); |
| 48 | + } catch (error: unknown) { |
| 49 | + res.status(500).send({error: 'Failed to add program.'}); |
54 | 50 | return;
|
55 | 51 | }
|
56 |
| - console.log(`Updated ${PROGRAMS_FILE}`); |
| 52 | + console.log('Added new program'); |
57 | 53 | res.status(201).send();
|
58 | 54 | }
|
59 | 55 | );
|
60 |
| -}); |
61 | 56 |
|
62 |
| -const port = 4000; |
63 |
| -app.listen(port, () => { |
64 |
| - console.log(`Server is running on http://localhost:${port}`); |
65 |
| -}); |
| 57 | + const port = process.env.port || 4000; |
| 58 | + app.listen(port, () => { |
| 59 | + console.log(`Server is running on http://localhost:${port}`); |
| 60 | + }); |
| 61 | + |
| 62 | + return app; |
| 63 | +}; |
0 commit comments