Skip to content

Commit

Permalink
That was pretty easy
Browse files Browse the repository at this point in the history
  • Loading branch information
ftab committed Aug 1, 2024
1 parent 956014e commit 4cb1d79
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
- name: Deploy with rsync
run: rsync -rav --delete . musicbox@${{ secrets.MUSICBOX_SERVER_IP }}:/home/musicbox/api/production/
run: rsync -rav --delete --filter 'protect /config.js' --filter 'protect /node_modules/' . musicbox@${{ secrets.MUSICBOX_SERVER_IP }}:/home/musicbox/api/production/
- name: Install dependencies
run: |
ssh musicbox@${{ secrets.MUSICBOX_SERVER_IP }} <<'ENDSSH'
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/

# next.js build output
.next

config.js
12 changes: 12 additions & 0 deletions config.js.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const config = {
db: {
host: "127.0.0.1",
user: "username",
password: "hunter2",
database: "ircbot",
connectTimeout: 60000
},
listPerPage: 100,
};

module.exports = config;
15 changes: 15 additions & 0 deletions helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function getOffset(currentPage = 1, listPerPage) {
return (currentPage - 1) * [listPerPage];
}

function emptyOrRows(rows) {
if (!rows) {
return [];
}
return rows;
}

module.exports = {
getOffset,
emptyOrRows
}
107 changes: 106 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"supertest": "^6.3.4"
},
"dependencies": {
"express": "^4.19.2"
"express": "^4.19.2",
"mysql2": "^3.11.0"
}
}
15 changes: 15 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require('express');
const router = express.Router();
const users = require('../services/users');

/* GET users */
router.get('/', async function(req, res, next) {
try {
res.json(await users.getMultiple(req.query.page));
} catch (err) {
console.error(`Error while getting users `, err.message);
next(err);
}
});

module.exports = router;
13 changes: 12 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ const app = express();

const port = process.env.PORT || 5000;

const usersRouter = require("./routes/users");

app.get("/", (req, res) => {
return res.status(200).send({
message: "Hello World!",
});
});

app.use("/users", usersRouter);

app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
console.error(err.message, err.stack);
res.status(statusCode).json({ message: err.message });
return;
});

app.listen(port, () => {
console.log("Listening on " + port);
});

module.exports = app;
module.exports = app;
13 changes: 13 additions & 0 deletions services/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mysql = require('mysql2/promise');
const config = require('../config');

async function query(sql, params) {
const connection = await mysql.createConnection(config.db);
const [results, ] = await connection.execute(sql, params);

return results;
}

module.exports = {
query
}
22 changes: 22 additions & 0 deletions services/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const db = require('./db');
const helper = require('../helper');
const config = require('../config');

async function getMultiple(page = 1){
const offset = helper.getOffset(page, config.listPerPage);
const rows = await db.query(
`SELECT userId, nickname
FROM user LIMIT ${offset},${config.listPerPage}`
);
const data = helper.emptyOrRows(rows);
const meta = {page};

return {
data,
meta
}
}

module.exports = {
getMultiple
}

0 comments on commit 4cb1d79

Please sign in to comment.