Skip to content

Commit

Permalink
Merge pull request #22 from petercort/setup-db-connection
Browse files Browse the repository at this point in the history
Setup db connection
  • Loading branch information
petercort authored Dec 27, 2024
2 parents c95cadc + 5f77029 commit b12d50a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog


### Version 1.0.1

#### bugfix
* PR [#22](https://github.com/petercort/FBF-Buddy/pull/22) - Setup db connection



### Version 0.1.0

#### enhancement
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fbf-event-buddy",
"private": true,
"version": "1.0.0",
"version": "1.0.1",
"description": "Cycling event app for Discord",
"main": "app.js",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require('dotenv').config();
let discordToken;

if (process.env.NODE_ENV === 'production'){
const discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
discordToken = fs.readFileSync("/mnt/secrets-store/discordToken", 'utf8');
} else {
discordToken = process.env.discordToken
}
Expand Down
8 changes: 4 additions & 4 deletions src/commands/utility/connect_strava.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const fs = require('node:fs');
require('dotenv').config();

let stravaClientId;
let stravaRedirectURI;
let stravaRedirectUri;

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

module.exports = {
Expand All @@ -21,7 +21,7 @@ module.exports = {
.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=${stravaClientId}&response_type=code&redirect_uri=${stravaRedirectURI}/${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
29 changes: 6 additions & 23 deletions src/dbObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@ const { Sequelize } = require('sequelize');
const fs = require('node:fs');

require('dotenv').config();
let usePostgres = true;
let database;
let username;
let password;
let host;
let sequelize;
let config;


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 databaseUrl = fs.readFileSync("/mnt/secrets-store/databaseUrl", 'utf8')
const config = {
dialect: 'postgres',
ssl: {
Expand All @@ -33,10 +16,10 @@ if (process.env.NODE_ENV === 'production') {
}
sequelize = new Sequelize(databaseUrl, config)
} else {
database = process.env.database
username = process.env.username
password = process.env.password
config = {
const database = process.env.database
const username = process.env.username
const password = process.env.password
const config = {
host: process.env.host,
dialect: 'sqlite',
logging: false,
Expand Down
10 changes: 5 additions & 5 deletions src/shared_library/strava_authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ const fs = require('node:fs');
require('dotenv').config();

let stravaClientId;
let stravaClentSecret;
let stravaClientSecret;

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

0 comments on commit b12d50a

Please sign in to comment.