Skip to content

Commit

Permalink
NodeJS service - move from mysql to postgres (#65)
Browse files Browse the repository at this point in the history
* NodeJS service - move from mysql to postgres

* Update package-lock.json
  • Loading branch information
mathieu-benoit authored Nov 6, 2024
1 parent 76e55b4 commit 0497c38
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 118 deletions.
76 changes: 48 additions & 28 deletions templates/node-service/content/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const http = require('http');
const mysql = require('mysql2');
const http = require("http");
const { Client } = require("pg");

// create the connection to database
const host = process.env.DB_HOST || 'localhost';
const connection = mysql.createConnection({
const host = process.env.DB_HOST || "localhost";
const client = new Client({
host,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'secret',
database: process.env.DB_DATABASE || 'score',
port: process.env.DB_PORT || 3306
user: process.env.DB_USER || "postgres",
password: process.env.DB_PASSWORD || "secret",
database: process.env.DB_DATABASE || "score",
port: process.env.DB_PORT || 5432,
});

const requestHandler = async (request, response) => {
Expand All @@ -17,40 +16,61 @@ const requestHandler = async (request, response) => {
const message = process.env.MESSAGE || "Hello, World!";

// Run hello world query
const [rows, fields] = await connection.promise().query(
'SELECT version() as version'
const serverVersionRes = await client.query(
`SHOW server_version;`
);
const version = rows[0].version;
const serverVersion = serverVersionRes.rows[0].server_version;

const versionRes = await client.query(
`SELECT version();`
);
const version = versionRes.rows[0].version;

const html = `
<html>
<body>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity_no="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity_no="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity_no="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity_no="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<div class="container text-center mt-5 pt-5">
<h1>${message}</h1>
<p>This is an application talking to a MySQL <code>${version}</code> database on host <code>${host}</code>, deployed with Score!</p>
<h1>${message}</h1>
<p>This is an application talking to a PostgreSQL <code>${serverVersion}</code> database on host <code>${host}</code>, deployed with Score!</p>
<p><code></p>
<p>
<pre>
SELECT version();
${version}
</pre>
</p>
</div>
</body>
</html>
`
`;

response.end(html);
}
};

const server = http.createServer(requestHandler);
const App = async () => {
// create the connection to database
await client.connect();

const port = process.env.PORT || 8080;
const server = http.createServer(requestHandler);

server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err);
}
const port = process.env.PORT || 8080;

console.log(`server is listening on ${port}`);
});
server.listen(port, (err) => {
if (err) {
return console.log("something bad happened", err);
}

console.log(`server is listening on ${port}`);
});
};

App();

// Exit the process when signal is received (For docker)
process.on('SIGINT', () => {
process.exit();
["SIGINT", "SIGTERM"].forEach((signal) => {
process.on(signal, () => {
process.exit();
});
});
176 changes: 104 additions & 72 deletions templates/node-service/content/package-lock.json

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

4 changes: 2 additions & 2 deletions templates/node-service/content/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "${{ values.name }}",
"version": "1.0.0",
"description": "A hello world app",
"description": "An Hello World workload in NodeJS talking to a PostgreSQL database.",
"main": "index.js",
"license": "PRIVATE",
"private": true,
"dependencies": {
"mysql2": "^3.9.7"
"pg": "^8.13.1"
}
}
23 changes: 8 additions & 15 deletions templates/node-service/content/score.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
apiVersion: score.dev/v1b1

metadata:
name: node-workload

# Define the ports that this service exposes
service:
ports:
www:
port: 80 # The port that the service will be exposed on
targetPort: 3000 # The port that the container will be listening on

# Define the containers that make up this service
containers:
node-service:
image: . # Set by pipeline
Expand All @@ -21,16 +11,19 @@ containers:
DB_PASSWORD: ${resources.db.password}
DB_HOST: ${resources.db.host}
DB_PORT: ${resources.db.port}

# Define the resources that this service needs
resources:
dns: # We need a DNS record to point to the service
dns:
type: dns
route:
type: route
params:
host: ${resources.dns.host}
path: /
port: 80
db: # We need a database to store data
type: mysql
db:
type: postgres
service:
ports:
www:
port: 80
targetPort: 3000
3 changes: 2 additions & 1 deletion templates/node-service/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ kind: Template
metadata:
name: nodejs-service-template
title: NodeJS Service Template
description: An example template that creates a simple Hello World service
description: An example template that creates a simple NodeJS service talking to a PostgreSQL database
tags:
- score
- humanitec
- postgres
spec:
owner: user:guest
type: service
Expand Down

0 comments on commit 0497c38

Please sign in to comment.