Skip to content

Commit

Permalink
Merge pull request #20 from petercort/setup-db-connection
Browse files Browse the repository at this point in the history
updating!
  • Loading branch information
petercort authored Dec 27, 2024
2 parents 3c58b58 + e9d721a commit 027f9b8
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@ dist
argocd/secret_provider_class.yaml
argocd/pod.yaml

secrets-store

.gitignore
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog


### Version 0.1.0

#### enhancement
* PR [#20](https://github.com/petercort/FBF-Buddy/pull/20) - updating!



### Version 1.0.0

#### feature
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ COPY . .

EXPOSE 3000

CMD ["node", "src/app.js"]
CMD ["npm", "start"]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node": ">=20.x"
},
"scripts": {
"start": "node src/app.js",
"start": "NODE_ENV=production node src/app.js",
"register": "node src/commands.js",
"dev": "nodemon src/app.js"
},
Expand Down
20 changes: 13 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { Op } = require('sequelize');
const { EventsTable, UsersTable, BikesTable } = require('./dbObjects.js');
const { exec } = require('node:child_process');
const { execute } = require('./commands/utility/create_event.js');
const discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
require('dotenv').config();

client.commands = new Collection();
// load configs
let discordToken;

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

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

for (const folder of commandFolders) {
Expand All @@ -28,6 +33,7 @@ for (const folder of commandFolders) {
}
}

// manage the sequelize connection
client.once(Events.ClientReady, readyClient => {
console.log('Syncing database...');
EventsTable.sync({ alter: true });
Expand Down
19 changes: 15 additions & 4 deletions src/commands.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
const { REST, Routes } = require('discord.js');
require('dotenv').config()
const fs = require('node:fs');
const path = require('node:path');
const discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
const guildId = fs.readFileSync("/mnt/secrets-store/guildId", 'utf8');
const appId = fs.readFileSync("/mnt/secrets-store/appId", 'utf8');

require('dotenv').config()
let discordToken;
let guildId;
let appId;
if (process.env.NODE_ENV === 'production') {
discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
guildId = fs.readFileSync("/mnt/secrets-store/guildId", 'utf8');
appId = fs.readFileSync("/mnt/secrets-store/appId", 'utf8');
} else {
discordToken = process.env.discordToken;
guildId = process.env.guildId;
appId = process.env.appId;
}


const commands = [];
// Grab all the command folders from the commands directory you created earlier
Expand Down
18 changes: 12 additions & 6 deletions src/commands/utility/connect_strava.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const { EmbedBuilder } = require('discord.js');
const axios = require('axios');
const { UsersTable } = require('../../dbObjects.js'); // Assuming you have a UsersTable to store user data
const fs = require('node:fs');
const path = require('node:path');
// load strava configuration
const STRAVA_CLIENT_ID = fs.readFileSync("/mnt/secrets-store/STRAVA_CLIENT_ID", 'utf8');
const STRAVA_REDIRECT_URI = fs.readFileSync("/mnt/secrets-store/STRAVA_REDIRECT_URI", 'utf8');
require('dotenv').config();

let stravaClientId;
let stravaRedirectURI;

if (process.env.NODE_ENV === 'production') {
stravaClientId = fs.readFileSync("/mnt/secrets-store/stravaClientId", 'utf8');
stravaRedirectURI = fs.readFileSync("/mnt/secrets-store/stravaRedirectURI", 'utf8');
} else {
stravaClientId = process.env.stravaClientId;
stravaRedirectURI = process.env.stravaRedirectURI;
}

module.exports = {
data: new SlashCommandBuilder()
.setName('connect_strava')
.setDescription('Connect your Strava account to collect ride data.'),
async execute(interaction) {
const userId = interaction.user.id;
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`;
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`;

const embed = new EmbedBuilder()
.setTitle('Connect Strava')
Expand Down
56 changes: 42 additions & 14 deletions src/dbObjects.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
const { Sequelize } = require('sequelize');
const fs = require('node:fs');
const path = require('node:path');

const database = fs.readFileSync("/mnt/secrets-store/database", 'utf8')
const username = fs.readFileSync("/mnt/secrets-store/username", 'utf8')
const password = fs.readFileSync("/mnt/secrets-store/password", 'utf8')
const host = fs.readFileSync("/mnt/secrets-store/host", 'utf8')
require('dotenv').config();
let usePostgres = true;
let database;
let username;
let password;
let host;
let sequelize;
let config;

const sequelize = new Sequelize(database, username, password, {
host: host,
dialect: 'postgres',
dialectOptions: {
if (process.env.NODE_ENV === 'production') {
database = fs.readFileSync("/mnt/secrets-store/database", 'utf8')
username = fs.readFileSync("/mnt/secrets-store/username", 'utf8')
password = fs.readFileSync("/mnt/secrets-store/password", 'utf8')
host = fs.readFileSync("/mnt/secrets-store/host", 'utf8')
config = {
host: process.env.host,
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
}
sequelize = new Sequelize(database, username, password, config);
} else if (usePostgres) {
const databaseUrl = process.env.databaseUrl
const config = {
dialect: 'postgres',
ssl: {
require: true,
rejectUnauthorized: false // This is necessary for Azure
rejectUnauthorized: false,
require: true,
}
}
});

sequelize = new Sequelize(databaseUrl, config)
} else {
database = process.env.database
username = process.env.username
password = process.env.password
config = {
host: process.env.host,
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
}
sequelize = new Sequelize(database, username, password, config);
}


// Test the connection
sequelize.authenticate()
.then(() => {
Expand All @@ -27,7 +55,7 @@ sequelize.authenticate()
console.error('Unable to connect to the database:', err);
});


const EventsTable = require('./models/events.js')(sequelize, Sequelize.DataTypes);
const UsersTable = require('./models/users.js')(sequelize, Sequelize.DataTypes);
const BikesTable = require('./models/bikes.js')(sequelize, Sequelize.DataTypes);
Expand Down
2 changes: 1 addition & 1 deletion src/models/users.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('users', {
userId: {
type: DataTypes.STRING,
type: DataTypes.INTEGER,
primaryKey: true,
Unique: true,
},
Expand Down
21 changes: 15 additions & 6 deletions src/shared_library/strava_authentication.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
const axios = require('axios');
const { UsersTable } = require('../dbObjects.js');
const fs = require('node:fs');
const client_id = fs.readFileSync("/mnt/secrets-store/STRAVA_CLIENT_ID", 'utf8');
const client_secret = fs.readFileSync("/mnt/secrets-store/STRAVA_CLIENT_SECRET", 'utf8');
require('dotenv').config();

let stravaClientId;
let stravaClentSecret;

if (process.env.NODE_ENV === 'production') {
stravaClientId = fs.readFileSync("/mnt/secrets-store/stravaClientId", 'utf8');
stravaClentSecret = fs.readFileSync("/mnt/secrets-store/stravaClentSecret", 'utf8');
} else {
stravaClientId = process.env.stravaClientId;
stravaClentSecret = process.env.stravaClentSecret;
}
async function firstTimeAuth(userId, code){
try {
const response = await axios.post('https://www.strava.com/oauth/token', {
client_id: client_id,
client_secret: client_secret,
client_id: stravaClientId,
client_secret: stravaClentSecret,
code: code,
grant_type: 'authorization_code'
});
Expand All @@ -33,8 +42,8 @@ async function getStravaAuthentication(userData) {
// Token is expired, refresh it
console.log('Token is expired, refreshing...');
const refreshTokenResponse = await axios.post('https://www.strava.com/oauth/token', {
client_id: client_id,
client_secret: client_secret,
client_id: stravaClientId,
client_secret: stravaClentSecret,
grant_type: 'refresh_token',
refresh_token: userData.refreshToken,
});
Expand Down

0 comments on commit 027f9b8

Please sign in to comment.