WIP: Implement backend in express

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2021-02-07 15:18:04 -07:00
parent 03bfd1bed3
commit 621782a3a8
13 changed files with 218 additions and 0 deletions

View file

@ -0,0 +1,29 @@
import express from 'express';
const router = express.Router()
router.get('/', (req, res) => {
});
router.post('/', (req, res) => {
});
router.get('/:id', (req, res) => {
});
router.get('/:id/balance', (req, res) => {
});
router.put('/:id', (req, res) => {
});
router.delete('/:id', (req, res) => {
});
export { router };

View file

@ -0,0 +1,8 @@
import { randomId } from '../utils';
export class Budget {
id: string = randomId();
name: string;
description: string;
currencyCode: string;
}

View file

@ -0,0 +1,4 @@
export class BudgetRepository {
}

View file

@ -0,0 +1,25 @@
import express from 'express';
const router = express.Router()
router.get('/', (req, res) => {
});
router.post('/', (req, res) => {
});
router.get('/:id', (req, res) => {
});
router.put('/:id', (req, res) => {
});
router.delete('/:id', (req, res) => {
});
export { router };

16
src/server/db/index.ts Normal file
View file

@ -0,0 +1,16 @@
import path from 'path';
import sqlite3 from 'sqlite3';
import * as migrations from './migrations';
export function db(dataDir: string): sqlite3.Database {
const db = new sqlite3.Database(path.join(dataDir, "twigs.db"), (err) => {
if (err != null) {
console.error("Failed to open db");
console.error(err);
throw 'Failed to open db';
}
});
return db
}

View file

34
src/server/index.ts Normal file
View file

@ -0,0 +1,34 @@
import express from 'express';
import { router as budgetRouter } from './budget/controller'
import { router as categoryRouter } from './categories/controller'
import { router as permissionsRouter } from './permissions/controller'
import { router as transactionRouter } from './transactions/controller'
import { router as userRouter } from './users/controller'
const port = process.env.PORT || 3000;
const app = express();
const dataDir = process.env.TWIGS_DATA || __dirname;
app.use(express.static(__dirname + '/public'));
// app.get('/', (req, res) => {
// console.log('hit: /');
// res.send('test');
// })
app.use('/api/budgets', budgetRouter);
app.use('/api/categories', categoryRouter);
app.use('/api/permissions', permissionsRouter);
app.use('/api/transactions', transactionRouter);
app.use('/api/users', userRouter);
app.get('/*', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.listen(port, () => {
console.log(`Twigs server listening at http://localhost:${port}`)
console.log(`Serving static content from ${__dirname}/public`)
});

View file

@ -0,0 +1,25 @@
import express from 'express';
const router = express.Router()
router.get('/', (req, res) => {
});
router.post('/', (req, res) => {
});
router.get('/:id', (req, res) => {
});
router.put('/:id', (req, res) => {
});
router.delete('/:id', (req, res) => {
});
export { router };

View file

@ -0,0 +1,25 @@
import express from 'express';
const router = express.Router()
router.get('/', (req, res) => {
});
router.post('/', (req, res) => {
});
router.get('/:id', (req, res) => {
});
router.put('/:id', (req, res) => {
});
router.delete('/:id', (req, res) => {
});
export { router };

20
src/server/tsconfig.json Normal file
View file

@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "../../dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*"
]
}
},
"include": [
"**/*"
]
}

0
src/server/tslint.json Normal file
View file

View file

@ -0,0 +1,25 @@
import express from 'express';
const router = express.Router()
router.get('/', (req, res) => {
});
router.post('/', (req, res) => {
});
router.get('/:id', (req, res) => {
});
router.put('/:id', (req, res) => {
});
router.delete('/:id', (req, res) => {
});
export { router };

7
src/server/utils.ts Normal file
View file

@ -0,0 +1,7 @@
import { randomInt } from 'crypto';
const CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
export function randomId(length = 32): string {
return Array.from(new Array(length), () => { CHARACTERS[randomInt(CHARACTERS.length)] }).join('');
}