Skip to content

Commit 0f4df9e

Browse files
committedAug 12, 2023
refactor: DB_PASS --> DB_PASSWORD
1 parent 4c27268 commit 0f4df9e

21 files changed

+186
-39
lines changed
 

‎.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ TLS_KEY_FILE=
2323
DB_HOST=
2424
DB_PORT=
2525
DB_USER="hanabiuser"
26-
DB_PASS="1234567890"
26+
DB_PASSWORD="1234567890"
2727
DB_NAME="hanabi"
2828

2929
# The Google Drive configuration (for automated database backups). If blank, it will skip backing up

‎database_backup.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fi
3535
# Back up the database and gzip it.
3636
mkdir -p "$BACKUPS_DIR"
3737
echo "Dumping the database..."
38-
PGPASSWORD="$DB_PASS" pg_dump --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME" > "$BACKUPS_DIR/$FILENAME"
38+
PGPASSWORD="$DB_PASSWORD" pg_dump --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME" > "$BACKUPS_DIR/$FILENAME"
3939
if [[ $? -ne 0 ]]; then
4040
exit 1
4141
fi

‎database_shell.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ if uname -a | grep MINGW64 >/dev/null 2>&1; then
2424
fi
2525

2626
# Open a database shell.
27-
PGPASSWORD="$DB_PASS" psql --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME"
27+
PGPASSWORD="$DB_PASSWORD" psql --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME"

‎docs/INSTALL.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Building the client code can be memory intensive. Make sure that your system has
8080
- `exit`
8181
- Set up environment variables (optional):
8282
- `notepad .env` <br />
83-
(the two important ones to verify are "DOMAIN" and "DB_PASS")
83+
(the two important ones to verify are "DOMAIN" and "DB_PASSWORD")
8484
- Install the database schema:
8585
- `./install/install_database_schema.sh`
8686
- Open VSCode using the cloned repository as the project folder:
@@ -142,7 +142,7 @@ Building the client code can be memory intensive. Make sure that your system has
142142
- `./install/install_development_dependencies.sh`
143143
- Set up environment variables (optional):
144144
- `open -t .env` <br />
145-
(the two important ones to verify are "DOMAIN" and "DB_PASS")
145+
(the two important ones to verify are "DOMAIN" and "DB_PASSWORD")
146146
- Install the database schema:
147147
- `./install/install_database_schema.sh`
148148
- Open VSCode using the cloned repository as the project folder:
@@ -221,7 +221,7 @@ These instructions assume you are running Ubuntu 20.04 LTS. Some adjustments may
221221
- `./install/install_development_dependencies.sh`
222222
- Set up environment variables:
223223
- `nano .env`
224-
(the two important ones to verify are "DOMAIN" and "DB_PASS")
224+
(the two important ones to verify are "DOMAIN" and "DB_PASSWORD")
225225
- Install the database schema:
226226
- `./install/install_database_schema.sh`
227227
- See [Running the Server](#running-the-server).

‎install/install_database_schema.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if [[ -z ${DB_PORT-} ]]; then
1313
DB_PORT=5432
1414
fi
1515

16-
PGPASSWORD="$DB_PASS" psql \
16+
PGPASSWORD="$DB_PASSWORD" psql \
1717
--quiet \
1818
--variable=ON_ERROR_STOP=1 \
1919
--host="$DB_HOST" \

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@types/linkifyjs": "^2.1.4",
2626
"@types/lodash": "^4.14.197",
2727
"@types/node": "^20.4.9",
28+
"@types/pg": "^8.10.2",
2829
"@types/tooltipster": "^0.0.32",
2930
"bufferutil": "^4.0.7",
3031
"canvas": "^2.11.2",

‎packages/server-old/src/models/db.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require("dotenv").config();
1010
const databaseConfig = {
1111
host: process.env.DB_HOST,
1212
user: process.env.DB_USER,
13-
password: process.env.DB_PASS,
13+
password: process.env.DB_PASSWORD,
1414
database: process.env.DB_NAME,
1515
};
1616

‎packages/server/src/db.ts

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { parseIntSafe } from "@hanabi/utils";
2+
import { Client } from "pg";
3+
import { logger } from "./logger";
4+
5+
export async function databaseInit(): Promise<void> {
6+
const client = new Client({});
7+
await client.connect();
8+
9+
const res = await client.query("SELECT $1::text as message", [
10+
"Hello world!",
11+
]);
12+
console.log(res.rows[0].message); // Hello world!
13+
await client.end();
14+
}
15+
16+
/**
17+
* Read the database configuration from environment variables. (They should already be loaded from
18+
* the ".env" file at this point.)
19+
*/
20+
function getDatabaseConfig() {
21+
let host = process.env["DB_HOST"];
22+
if (host === undefined || host === "") {
23+
host = "localhost";
24+
logger.info(`DB_HOST not specified; using a default value of: ${host}`);
25+
}
26+
27+
const portString = process.env["DB_PORT"];
28+
let port: number;
29+
if (portString === undefined || portString === "") {
30+
port = 5432; // The default port for PostgreSQL.
31+
logger.info(`DB_PORT not specified; using a default value of: ${port}`);
32+
} else {
33+
port = parseIntSafe(portString);
34+
if (Number.isNaN(port)) {
35+
throw new TypeError(
36+
`Failed to parse the "DB_PORT" environment variable: ${portString}`,
37+
);
38+
}
39+
}
40+
41+
let user = process.env["DB_USER"];
42+
if (user === undefined || user === "") {
43+
user = "hanabiuser";
44+
logger.info(`DB_USER not specified; using a default value of: ${user}`);
45+
}
46+
47+
let password = process.env["DB_PASSWORD"];
48+
if (password === undefined || password === "") {
49+
password = "1234567890";
50+
logger.info(
51+
`DB_PASSWORD not specified; using a default value of: ${password}`,
52+
);
53+
}
54+
55+
/*
56+
57+
dbName = os.Getenv("DB_NAME")
58+
if len(dbPass) == 0 {
59+
defaultName := "hanabi"
60+
logger.Info("DB_NAME not specified; using default value of \"" + defaultName + "\".")
61+
dbName = defaultName
62+
}
63+
64+
*/
65+
66+
return {
67+
host,
68+
port,
69+
};
70+
}

‎packages/server/src/main.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { PROJECT_NAME } from "@hanabi/data";
2-
import { todo } from "@hanabi/utils";
32
import dotenv from "dotenv";
43
import Fastify from "fastify";
54
import fs from "node:fs";
65
import path from "node:path";
6+
import { databaseInit } from "./db";
77
import { recordCurrentGitCommitSHA1 } from "./git";
88
import { logger, setLoggerPretty } from "./logger";
99

@@ -18,7 +18,7 @@ const VERSION_TXT_PATH = path.join(
1818
);
1919

2020
main().catch((error) => {
21-
throw new Error(`The Hanabi server encountered an error: ${error}`);
21+
throw new Error(`${PROJECT_NAME} server encountered an error: ${error}`);
2222
});
2323

2424
async function main() {
@@ -27,7 +27,7 @@ async function main() {
2727
logWelcomeMessage();
2828
recordCurrentGitCommitSHA1();
2929
validateVersionTXT();
30-
databaseInit();
30+
await databaseInit();
3131

3232
// eslint-disable-next-line new-cap
3333
const fastify = Fastify({
@@ -86,7 +86,3 @@ function validateVersionTXT() {
8686
);
8787
}
8888
}
89-
90-
function databaseInit() {
91-
todo(); // TODO
92-
}

‎packages/server/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"resolveJsonModule": true,
1717

1818
// "target" specifies the ECMAScript target version. By default, it is "ES3". This is too
19-
// conservative; the project targets browsers within a 2 year time-frame.
19+
// conservative; the project targets versions of Node.js within a 2 year time-frame.
2020
"target": "ES2018",
2121
},
2222

‎scripts/bash/dump_single_table.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if [[ -z ${DB_USER-} ]]; then
2121
echo "Error: You must specify the database username in the \".env\" file."
2222
exit 1
2323
fi
24-
if [[ -z ${DB_PASS-} ]]; then
24+
if [[ -z ${DB_PASSWORD-} ]]; then
2525
echo "Error: You must specify the database password in the \".env\" file."
2626
exit 1
2727
fi
@@ -30,4 +30,4 @@ if [[ -z ${DB_NAME-} ]]; then
3030
exit 1
3131
fi
3232

33-
PGPASSWORD="$DB_PASS" pg_dump --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME" --format plain --verbose --file "/tmp/$TABLE_NAME.sql" --table="$TABLE_NAME"
33+
PGPASSWORD="$DB_PASSWORD" pg_dump --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME" --format plain --verbose --file "/tmp/$TABLE_NAME.sql" --table="$TABLE_NAME"

‎scripts/bash/import_db.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if [[ -z ${DB_USER-} ]]; then
2323
echo "Error: You must specify the database username in the \".env\" file."
2424
exit 1
2525
fi
26-
if [[ -z ${DB_PASS-} ]]; then
26+
if [[ -z ${DB_PASSWORD-} ]]; then
2727
echo "Error: You must specify the database password in the \".env\" file."
2828
exit 1
2929
fi
@@ -33,12 +33,12 @@ if [[ -z ${DB_NAME-} ]]; then
3333
fi
3434

3535
# This assumes that "postgres" and "hanabiuser" share the same password
36-
PGPASSWORD="$DB_PASS" psql --host="$DB_HOST" --port="$DB_PORT" --username="postgres" << EOF
36+
PGPASSWORD="$DB_PASSWORD" psql --host="$DB_HOST" --port="$DB_PORT" --username="postgres" << EOF
3737
DROP DATABASE $DB_NAME;
3838
CREATE DATABASE $DB_NAME;
3939
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
4040
GRANT USAGE ON SCHEMA public TO $DB_USER;
4141
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $DB_USER;
4242
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $DB_USER;
4343
EOF
44-
PGPASSWORD="$DB_PASS" psql --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME" < "$1"
44+
PGPASSWORD="$DB_PASSWORD" psql --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME" < "$1"

‎scripts/bash/sanitize_local_db.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if [[ -z ${DB_USER-} ]]; then
1818
echo "Error: You must specify the database username in the \".env\" file."
1919
exit 1
2020
fi
21-
if [[ -z ${DB_PASS-} ]]; then
21+
if [[ -z ${DB_PASSWORD-} ]]; then
2222
echo "Error: You must specify the database password in the \".env\" file."
2323
exit 1
2424
fi
@@ -27,7 +27,7 @@ if [[ -z ${DB_NAME-} ]]; then
2727
exit 1
2828
fi
2929

30-
PGPASSWORD="$DB_PASS" psql --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME" << EOF
30+
PGPASSWORD="$DB_PASSWORD" psql --host="$DB_HOST" --port="$DB_PORT" --username="$DB_USER" --dbname="$DB_NAME" << EOF
3131
UPDATE users SET last_ip='0.0.0.0';
3232
DELETE FROM user_settings;
3333
DELETE FROM user_friends;

‎scripts/python/change_password.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Variables
1919
user = os.getenv("DB_USER")
20-
password = os.getenv("DB_PASS")
20+
password = os.getenv("DB_PASSWORD")
2121
host = os.getenv("DB_HOST")
2222
if host == "":
2323
host = "localhost"

‎scripts/python/find_users_with_highest_games.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Variables
1919
user = os.getenv("DB_USER")
20-
password = os.getenv("DB_PASS")
20+
password = os.getenv("DB_PASSWORD")
2121
host = os.getenv("DB_HOST")
2222
if host == "":
2323
host = "localhost"

‎scripts/python/prune_games_with_invalid_player_count.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Variables
1919
user = os.getenv("DB_USER")
20-
password = os.getenv("DB_PASS")
20+
password = os.getenv("DB_PASSWORD")
2121
host = os.getenv("DB_HOST")
2222
if host == "":
2323
host = "localhost"

‎scripts/python/prune_users_with_no_games_played.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Variables
1919
user = os.getenv("DB_USER")
20-
password = os.getenv("DB_PASS")
20+
password = os.getenv("DB_PASSWORD")
2121
host = os.getenv("DB_HOST")
2222
if host == "":
2323
host = "localhost"

‎scripts/python/rename_user.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Variables
1919
user = os.getenv("DB_USER")
20-
password = os.getenv("DB_PASS")
20+
password = os.getenv("DB_PASSWORD")
2121
host = os.getenv("DB_HOST")
2222
if host == "":
2323
host = "localhost"
@@ -26,6 +26,7 @@
2626
port = "5432"
2727
database = os.getenv("DB_NAME")
2828

29+
2930
# Subroutines
3031
def is_ascii(s):
3132
return all(ord(c) < 128 for c in s)

‎scripts/python/unban.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Variables
1919
user = os.getenv("DB_USER")
20-
password = os.getenv("DB_PASS")
20+
password = os.getenv("DB_PASSWORD")
2121
host = os.getenv("DB_HOST")
2222
if host == "":
2323
host = "localhost"

‎server/src/models.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ func modelsInit() (*Models, error) {
5656
logger.Info("DB_USER not specified; using default value of \"" + defaultUser + "\".")
5757
dbUser = defaultUser
5858
}
59-
dbPass := os.Getenv("DB_PASS")
60-
if len(dbPass) == 0 {
59+
dbPassword := os.Getenv("DB_PASSWORD")
60+
if len(dbPassword) == 0 {
6161
defaultPass := "1234567890"
62-
logger.Info("DB_PASS not specified; using default value of \"" + defaultPass + "\".")
63-
dbPass = defaultPass
62+
logger.Info("DB_PASSWORD not specified; using default value of \"" + defaultPass + "\".")
63+
dbPassword = defaultPass
6464
}
6565
dbName = os.Getenv("DB_NAME")
66-
if len(dbPass) == 0 {
66+
if len(dbPassword) == 0 {
6767
defaultName := "hanabi"
6868
logger.Info("DB_NAME not specified; using default value of \"" + defaultName + "\".")
6969
dbName = defaultName
@@ -76,7 +76,7 @@ func modelsInit() (*Models, error) {
7676
"host=" + dbHost,
7777
"port=" + dbPort,
7878
"user=" + dbUser,
79-
"password=" + dbPass,
79+
"password=" + dbPassword,
8080
"dbname=" + dbName,
8181
}
8282
dsn := strings.Join(dsnArray, " ")
@@ -111,9 +111,10 @@ func (*Models) Close() {
111111
//
112112
// INSERT INTO notes (thing_a, thing_b)
113113
// VALUES
114-
// ($1, $2),
115-
// ($3, $4),
116-
// ($5, $6)
114+
//
115+
// ($1, $2),
116+
// ($3, $4),
117+
// ($5, $6)
117118
//
118119
// Also see:
119120
// https://stackoverflow.com/questions/12486436/how-do-i-batch-sql-statements-with-package-database-sql

0 commit comments

Comments
 (0)
Please sign in to comment.