Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlite can now be run as its own service
- Loading branch information
1 parent
78bfac8
commit 23cdb3b
Showing
5 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const cors = require('cors'); | ||
const session = require('cookie-session'); | ||
|
||
require('dotenv').config(); | ||
|
||
const app = express(); | ||
app.use(bodyParser.json()); | ||
|
||
const SqliteDataStore = require('./datastore_sqlite'); | ||
db = new SqliteDataStore(); | ||
db.initialize(process.env.DB_FILE || './db.sqlite').then(() => { | ||
// I love not having typechecking | ||
const proto = Object.getPrototypeOf(db); | ||
const props = Object.getOwnPropertyNames(proto); | ||
console.log(props); | ||
for (let key of props) { | ||
let fn = db[key]; | ||
if (key !== 'constructor' && typeof key === 'string' && typeof fn === 'function') { | ||
app.post(`/${key}`, async (req, res) => { | ||
try { | ||
let args = req.body; | ||
console.log('Processing', key, args); | ||
let ret = await db[key].apply(db, args); | ||
if (ret === undefined) { | ||
res.sendStatus(204); | ||
} | ||
else { | ||
res.send(JSON.stringify(ret)); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
res.sendStatus(500); | ||
} | ||
}); | ||
console.log('Routed method', key); | ||
} | ||
else { | ||
console.log('Cannot route for method/property', key); | ||
} | ||
} | ||
const port = process.env.DB_SERVICE_PORT || 3030; | ||
console.log(`DB Service hosted on :${port}`); | ||
app.listen(port); | ||
}).catch(console.error); | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters