Skip to content

Commit 027f9b8

Browse files
authored
Merge pull request #20 from petercort/setup-db-connection
updating!
2 parents 3c58b58 + e9d721a commit 027f9b8

File tree

10 files changed

+109
-40
lines changed

10 files changed

+109
-40
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,6 @@ dist
139139
argocd/secret_provider_class.yaml
140140
argocd/pod.yaml
141141

142+
secrets-store
143+
142144
.gitignore

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Changelog
22

33

4+
### Version 0.1.0
5+
6+
#### enhancement
7+
* PR [#20](https://github.com/petercort/FBF-Buddy/pull/20) - updating!
8+
9+
10+
411
### Version 1.0.0
512

613
#### feature

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ COPY . .
1414

1515
EXPOSE 3000
1616

17-
CMD ["node", "src/app.js"]
17+
CMD ["npm", "start"]

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"node": ">=20.x"
99
},
1010
"scripts": {
11-
"start": "node src/app.js",
11+
"start": "NODE_ENV=production node src/app.js",
1212
"register": "node src/commands.js",
1313
"dev": "nodemon src/app.js"
1414
},

src/app.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
const fs = require('node:fs');
22
const path = require('node:path');
33
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
4-
const { Op } = require('sequelize');
54
const { EventsTable, UsersTable, BikesTable } = require('./dbObjects.js');
6-
const { exec } = require('node:child_process');
7-
const { execute } = require('./commands/utility/create_event.js');
8-
const discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
9-
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
5+
require('dotenv').config();
106

11-
client.commands = new Collection();
7+
// load configs
8+
let discordToken;
129

13-
const foldersPath = path.join(__dirname, 'commands');
10+
if (process.env.NODE_ENV === 'production'){
11+
const discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
12+
} else {
13+
discordToken = process.env.discordToken
14+
}
1415

16+
// Create the discord client and instantiate the commands collection
17+
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
18+
client.commands = new Collection();
19+
const foldersPath = path.join(__dirname, 'commands');
1520
const commandFolders = fs.readdirSync(foldersPath);
1621

1722
for (const folder of commandFolders) {
@@ -28,6 +33,7 @@ for (const folder of commandFolders) {
2833
}
2934
}
3035

36+
// manage the sequelize connection
3137
client.once(Events.ClientReady, readyClient => {
3238
console.log('Syncing database...');
3339
EventsTable.sync({ alter: true });

src/commands.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
const { REST, Routes } = require('discord.js');
2-
require('dotenv').config()
32
const fs = require('node:fs');
43
const path = require('node:path');
5-
const discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
6-
const guildId = fs.readFileSync("/mnt/secrets-store/guildId", 'utf8');
7-
const appId = fs.readFileSync("/mnt/secrets-store/appId", 'utf8');
4+
5+
require('dotenv').config()
6+
let discordToken;
7+
let guildId;
8+
let appId;
9+
if (process.env.NODE_ENV === 'production') {
10+
discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
11+
guildId = fs.readFileSync("/mnt/secrets-store/guildId", 'utf8');
12+
appId = fs.readFileSync("/mnt/secrets-store/appId", 'utf8');
13+
} else {
14+
discordToken = process.env.discordToken;
15+
guildId = process.env.guildId;
16+
appId = process.env.appId;
17+
}
18+
819

920
const commands = [];
1021
// Grab all the command folders from the commands directory you created earlier

src/commands/utility/connect_strava.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
const { SlashCommandBuilder } = require('@discordjs/builders');
22
const { EmbedBuilder } = require('discord.js');
3-
const axios = require('axios');
43
const { UsersTable } = require('../../dbObjects.js'); // Assuming you have a UsersTable to store user data
54
const fs = require('node:fs');
6-
const path = require('node:path');
7-
// load strava configuration
8-
const STRAVA_CLIENT_ID = fs.readFileSync("/mnt/secrets-store/STRAVA_CLIENT_ID", 'utf8');
9-
const STRAVA_REDIRECT_URI = fs.readFileSync("/mnt/secrets-store/STRAVA_REDIRECT_URI", 'utf8');
5+
require('dotenv').config();
106

7+
let stravaClientId;
8+
let stravaRedirectURI;
9+
10+
if (process.env.NODE_ENV === 'production') {
11+
stravaClientId = fs.readFileSync("/mnt/secrets-store/stravaClientId", 'utf8');
12+
stravaRedirectURI = fs.readFileSync("/mnt/secrets-store/stravaRedirectURI", 'utf8');
13+
} else {
14+
stravaClientId = process.env.stravaClientId;
15+
stravaRedirectURI = process.env.stravaRedirectURI;
16+
}
1117

1218
module.exports = {
1319
data: new SlashCommandBuilder()
1420
.setName('connect_strava')
1521
.setDescription('Connect your Strava account to collect ride data.'),
1622
async execute(interaction) {
1723
const userId = interaction.user.id;
18-
const stravaAuthUrl = `https://www.strava.com/oauth/authorize?client_id=${STRAVA_CLIENT_ID}&response_type=code&redirect_uri=${STRAVA_REDIRECT_URI}/${userId}&scope=read,activity:read_all,profile:read_all`;
24+
const stravaAuthUrl = `https://www.strava.com/oauth/authorize?client_id=${stravaClientId}&response_type=code&redirect_uri=${stravaRedirectURI}/${userId}&scope=read,activity:read_all,profile:read_all`;
1925

2026
const embed = new EmbedBuilder()
2127
.setTitle('Connect Strava')

src/dbObjects.js

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
const { Sequelize } = require('sequelize');
22
const fs = require('node:fs');
3-
const path = require('node:path');
43

5-
const database = fs.readFileSync("/mnt/secrets-store/database", 'utf8')
6-
const username = fs.readFileSync("/mnt/secrets-store/username", 'utf8')
7-
const password = fs.readFileSync("/mnt/secrets-store/password", 'utf8')
8-
const host = fs.readFileSync("/mnt/secrets-store/host", 'utf8')
4+
require('dotenv').config();
5+
let usePostgres = true;
6+
let database;
7+
let username;
8+
let password;
9+
let host;
10+
let sequelize;
11+
let config;
912

10-
const sequelize = new Sequelize(database, username, password, {
11-
host: host,
12-
dialect: 'postgres',
13-
dialectOptions: {
13+
if (process.env.NODE_ENV === 'production') {
14+
database = fs.readFileSync("/mnt/secrets-store/database", 'utf8')
15+
username = fs.readFileSync("/mnt/secrets-store/username", 'utf8')
16+
password = fs.readFileSync("/mnt/secrets-store/password", 'utf8')
17+
host = fs.readFileSync("/mnt/secrets-store/host", 'utf8')
18+
config = {
19+
host: process.env.host,
20+
dialect: 'sqlite',
21+
logging: false,
22+
storage: 'database.sqlite',
23+
}
24+
sequelize = new Sequelize(database, username, password, config);
25+
} else if (usePostgres) {
26+
const databaseUrl = process.env.databaseUrl
27+
const config = {
28+
dialect: 'postgres',
1429
ssl: {
15-
require: true,
16-
rejectUnauthorized: false // This is necessary for Azure
30+
rejectUnauthorized: false,
31+
require: true,
1732
}
1833
}
19-
});
20-
34+
sequelize = new Sequelize(databaseUrl, config)
35+
} else {
36+
database = process.env.database
37+
username = process.env.username
38+
password = process.env.password
39+
config = {
40+
host: process.env.host,
41+
dialect: 'sqlite',
42+
logging: false,
43+
storage: 'database.sqlite',
44+
}
45+
sequelize = new Sequelize(database, username, password, config);
46+
}
47+
48+
2149
// Test the connection
2250
sequelize.authenticate()
2351
.then(() => {
@@ -27,7 +55,7 @@ sequelize.authenticate()
2755
console.error('Unable to connect to the database:', err);
2856
});
2957

30-
58+
3159
const EventsTable = require('./models/events.js')(sequelize, Sequelize.DataTypes);
3260
const UsersTable = require('./models/users.js')(sequelize, Sequelize.DataTypes);
3361
const BikesTable = require('./models/bikes.js')(sequelize, Sequelize.DataTypes);

src/models/users.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = (sequelize, DataTypes) => {
22
return sequelize.define('users', {
33
userId: {
4-
type: DataTypes.STRING,
4+
type: DataTypes.INTEGER,
55
primaryKey: true,
66
Unique: true,
77
},

src/shared_library/strava_authentication.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
const axios = require('axios');
22
const { UsersTable } = require('../dbObjects.js');
33
const fs = require('node:fs');
4-
const client_id = fs.readFileSync("/mnt/secrets-store/STRAVA_CLIENT_ID", 'utf8');
5-
const client_secret = fs.readFileSync("/mnt/secrets-store/STRAVA_CLIENT_SECRET", 'utf8');
4+
require('dotenv').config();
65

6+
let stravaClientId;
7+
let stravaClentSecret;
8+
9+
if (process.env.NODE_ENV === 'production') {
10+
stravaClientId = fs.readFileSync("/mnt/secrets-store/stravaClientId", 'utf8');
11+
stravaClentSecret = fs.readFileSync("/mnt/secrets-store/stravaClentSecret", 'utf8');
12+
} else {
13+
stravaClientId = process.env.stravaClientId;
14+
stravaClentSecret = process.env.stravaClentSecret;
15+
}
716
async function firstTimeAuth(userId, code){
817
try {
918
const response = await axios.post('https://www.strava.com/oauth/token', {
10-
client_id: client_id,
11-
client_secret: client_secret,
19+
client_id: stravaClientId,
20+
client_secret: stravaClentSecret,
1221
code: code,
1322
grant_type: 'authorization_code'
1423
});
@@ -33,8 +42,8 @@ async function getStravaAuthentication(userData) {
3342
// Token is expired, refresh it
3443
console.log('Token is expired, refreshing...');
3544
const refreshTokenResponse = await axios.post('https://www.strava.com/oauth/token', {
36-
client_id: client_id,
37-
client_secret: client_secret,
45+
client_id: stravaClientId,
46+
client_secret: stravaClentSecret,
3847
grant_type: 'refresh_token',
3948
refresh_token: userData.refreshToken,
4049
});

0 commit comments

Comments
 (0)