Skip to content

Commit 862e219

Browse files
Merge pull request #69 from hurricanemark/Phase4-TeamRolEmiSeriousWorks
Phase4 team rol emi serious works
2 parents 82dfa09 + 4219108 commit 862e219

39 files changed

+791
-168
lines changed

.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
**/.aws
1111
**/dist
1212
**/utils.js
13-
**/index.copy.js
13+
**/index.copy.js
14+
**/config/keys.js

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ index.copy.js
99
public/tests
1010
currencyRate.js
1111
views/index-temp.ejs
12-
config/keys.js
12+
config/keys.js
13+
Dockerfile.debug

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,58 @@ To access the Local Weather app running in docker container, point your browser
195195
For the purpose of hosting this app, we select [Cyclic free-for-life tier](https://app.cyclic.sh/api/login).
196196
Cyclic deploys full stack NodeJS apps on AWS infrastructure directly from GitHub. It works by integrating with your GitHub repos. It will build and deploy your code on every merge or push to your default branch.
197197

198+
**Build log on Cylic**
199+
200+
To avoid building from the main branch on Cylic, you can specify build source as a git staging branch.
201+
202+
```c
203+
2022-11-19 08:49:39: [CYCLIC] Building...
204+
2022-11-19 08:49:39: [CYCLIC] cloning...
205+
2022-11-19 08:49:40: From https://github.com/hurricanemark/Weather_Report_NodeJS_EJS_Express_WeatherBit_API
206+
* branch 46b9aa68f54072cc8cf20f8936c79ad2bceaf306 -> FETCH_HEAD
207+
2022-11-19 08:49:40: HEAD is now at 46b9aa6 Merge pull request #62 from hurricanemark/Phase4-TeamRolEmiSeriousWorks
208+
2022-11-19 08:49:40: [CYCLIC] verifying...
209+
2022-11-19 08:49:40: [CYCLIC] using: node:v16.18.1 npm:8.11.0 runtime:nodejs16.x
210+
[CYCLIC] building from: /
211+
2022-11-19 08:49:40: [CYCLIC] installing dependencies from: package-lock.json
212+
2022-11-19 08:49:40: npm
213+
2022-11-19 08:49:40: WARN config production Use `--omit=dev` instead.
214+
2022-11-19 08:49:47: npm
215+
2022-11-19 08:49:47: WARN deprecated w3c-hr-time@1.0.2: Use your platform's native performance.now() and performance.timeOrigin.
216+
2022-11-19 08:49:48: npm
217+
2022-11-19 08:49:48: WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
218+
2022-11-19 08:49:48: npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
219+
2022-11-19 08:49:50: npm WARN
220+
2022-11-19 08:49:50: deprecated har-validator@5.1.5: this library is no longer supported
221+
2022-11-19 08:49:51: npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
222+
2022-11-19 08:49:59:
223+
added 925 packages in 18s
224+
2022-11-19 08:49:59: npm notice
225+
npm notice New major version of npm available! 8.11.0 -> 9.1.2
226+
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.1.2>
227+
npm notice Run `npm install -g npm@9.1.2` to update!
228+
npm notice
229+
2022-11-19 08:49:59: [CYCLIC] running build if defined...
230+
2022-11-19 08:49:59: [CYCLIC] pruning dev dependencies...
231+
2022-11-19 08:50:01:
232+
up to date, audited 489 packages in 2s
233+
2022-11-19 08:50:01:
234+
52 packages are looking for funding
235+
run `npm fund` for details
236+
237+
found 0 vulnerabilities
238+
2022-11-19 08:50:01: [CYCLIC] packaging 187.66 MB...
239+
2022-11-19 08:50:01: [CYCLIC] bundling from / ...
240+
2022-11-19 08:50:06: [CYCLIC] done packaging
241+
[CYCLIC] deploying...
242+
2022-11-19 08:50:16: deployed us-west-2 - 10.168s
243+
2022-11-19 08:50:16: SUCCESS
244+
245+
took 36.7 seconds
246+
api deployed at:
247+
https://cute-teal-pigeon-sari.cyclic.app
248+
```
249+
198250
Please do not abuse this live website below. Thank you!
199251
[Live demo](https://techrolemiweather.cyclic.app/about)
200252

config/keys_sample.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// add this file to .gitignore
2+
3+
const keys = {
4+
google: {
5+
GOOGLECLIENT_ID: 'xxxxxxxxxx',
6+
GOOGLECLIENT_SECRET: 'xxxxxxxxxxx'
7+
},
8+
mongodb: {
9+
MONGODB_CONSTR: 'mongodb+srv://[userid]:[password]@cluster0.brf1j.mongodb.net/oautho?authSource=admin&replicaSet=Cluster0-shard-0&readPreference=primary&ssl=true'
10+
},
11+
session: {
12+
cookieKey: 'xxxxxxxxxxxxxxxx'
13+
},
14+
development: {
15+
port: '1111',
16+
},
17+
weatherbitapi: {
18+
WEATHERBIT_URI: 'https://api.weatherbit.io/v2.0/',
19+
WEATHERBIT_APIKEY: 'xxxxxxxxxxxxxxxx'
20+
},
21+
exchangerateapi: {
22+
EXCHANGERATE_URI: 'https://v6.exchangerate-api.com/v6/',
23+
EXCHANGERATE_APIKEY: 'xxxxxxxxxxxxxxxxxxxxx',
24+
}
25+
};
26+
27+
export { keys };

config/passport-setup.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
const passport = require('passport');
2-
const GoogleStrategy = require('passport-google-oauth20');
3-
const User = require('../models/user-models');
4-
// const keys = require ('./keys');
5-
import { GoogleclientID, GoogleclientSecret } from './../index.js';
1+
2+
import passport from 'passport';
3+
import GoogleStrategy from 'passport-google-oauth20';
4+
import { User } from '../models/user-models.js';
5+
6+
import { GoogleclientID, GoogleclientSecret } from '../loadSecrets.js';
7+
8+
// const logging = console.log(GoogleclientID + ' --- ' + GoogleclientSecret);
69

710
// serialize user structure in the mongodb
8-
passport.serializeUser((user, done) => {
11+
const serialize = passport.serializeUser((user, done) => {
912
done(null, user.id);
1013
});
1114

12-
passport.deserializeUser((id, done) => {
15+
const deserialize = passport.deserializeUser((id, done) => {
1316
try {
1417
User.findById(id).then((user) => {
1518
done(null, user);
@@ -28,11 +31,15 @@ try {
2831
*
2932
* Go to https://console.cloud.google.com/ to create your own clientID and clientSecret.
3033
*/
31-
passport.use(new GoogleStrategy({
34+
const googleStrategy = passport.use(new GoogleStrategy({
3235
// options for google strategy
33-
callbackURL: '/auth/google/redirect',
36+
callbackURL: 'https://techrolemiweather.cyclic.app/auth/google/redirect',
37+
// callbackURL: '/auth/google/redirect',
38+
// clientID: keys.google.clientID,
39+
// clientSecret: keys.google.clientSecret
3440
clientID: GoogleclientID,
3541
clientSecret: GoogleclientSecret
42+
3643
}, (accessToken, refreshToken, profile, done) => {
3744
// passport callback function
3845
// console.log('accessToken: ' + accessToken);
@@ -60,4 +67,6 @@ passport.use(new GoogleStrategy({
6067
}
6168
})
6269

63-
}));
70+
}));
71+
72+
export { serialize, deserialize, googleStrategy };

index.js

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,14 @@ import * as dotenv from 'dotenv';
22
import request from 'request';
33
import express from 'express';
44
import bodyParser from 'body-parser';
5+
import cookieSession from 'cookie-session';
6+
import passport from 'passport';
7+
import mongoose from 'mongoose';
8+
import { compile } from 'ejs';
9+
import { serialize, deserialize, googleStrategy } from './config/passport-setup.js';
510
import {encryptAES, decryptAES} from './crypto.js';
611

7-
// dev env
8-
dotenv.config();
9-
10-
// application secrets
11-
// import { keys } from './config/keys.js';
12-
13-
let EXCHANGE_RATE_APIKEY;
14-
let EXCHANGE_BASE_URI='https://v6.exchangerate-api.com/v6/';
15-
let WEATHERBIT_KEY;
16-
let WEATHERBIT_URI='https://api.weatherbit.io/v2.0/';
17-
let GoogleclientID;
18-
let GoogleclientSecret;
19-
if (process.env.NODE_ENV === "production") {
20-
EXCHANGE_RATE_APIKEY = process.env.EXCHANGE_RATE_APIKEY;
21-
WEATHERBIT_KEY = process.env.WEATHERBIT_KEY;
22-
GoogleclientID = process.env.GoogleclientID;
23-
GoogleclientSecret = process.env.GoogleclientSecret;
24-
25-
} else {
26-
// dynamically importing keys.js using promise:
27-
import('./config/keys.js').then((secrets) => {
28-
EXCHANGE_RATE_APIKEY = secrets.keys.exchangerateapi.EXCHANGE_APIKEY;
29-
WEATHERBIT_KEY = secrets.keys.weatherbitapi.WEATHERBIT_APIKEY;
30-
GoogleclientID = secrets.keys.google.clientID;
31-
GoogleclientSecret = secrets.keys.google.clientSecret;
32-
});
33-
34-
}
35-
36-
12+
import { CookieKey, MongoDBConString } from './loadSecrets.js';
3713

3814
// Create network routing
3915
const app = express();
@@ -48,6 +24,8 @@ import { router as weatherbitRoutes } from './routes/weatherbit-routes.js';
4824
// routes handler for OAuth20 methods
4925
import { router as oauth20Routes } from './routes/auth-routes.js';
5026

27+
// routes handler for profile
28+
import { router as profileRoutes } from './routes/profile-routes.js';
5129

5230
// EJS is accessed by default in the views directory.
5331
app.set('view engine', 'ejs');
@@ -58,6 +36,29 @@ app.use(express.static('public'));
5836
// Parse incoming request bodies in a middleware before your handlers, available under the `req.body` property.
5937
app.use(bodyParser.urlencoded({ extended: true }));
6038

39+
40+
41+
// cookie!
42+
app.use(cookieSession({
43+
maxAge: 24 * 60 * 60 * 1000,
44+
keys: [CookieKey]
45+
}));
46+
47+
48+
// Initialize passport
49+
app.use(passport.initialize());
50+
app.use(passport.session());
51+
52+
53+
// connect to mongodb
54+
mongoose.connect(MongoDBConString, () => {
55+
console.log('Connected to MongoDB.');
56+
});
57+
58+
59+
60+
61+
6162
// Set up all routes related to currency exchange rate methods
6263
app.use('/exchange', currencyExchangeRoutes);
6364

@@ -67,14 +68,17 @@ app.use('/weatherbit', weatherbitRoutes);
6768
// Set up all routes related to oauth2 methods
6869
app.use('/auth', oauth20Routes);
6970

71+
// set up routes for user info after logged in
72+
app.use('/profile', profileRoutes);
73+
7074
// Homepage: get the locale from the client-side via the ejs form
7175
app.get('/', (req, res) => {
72-
res.render('index', {xkey: 'hey'});
76+
res.render('index', {user: req.user, xkey: 'hey'});
7377
})
7478

7579
// about page
7680
app.get('/about', function(req, res) {
77-
res.render('pages/about');
81+
res.render('pages/about', {user: req.user});
7882
});
7983

8084

@@ -86,14 +90,12 @@ app.listen(port, () => {
8690
}).on('error', (e) => {
8791
console.log('Error happened: ', e.message);
8892
// try different port
89-
try {
90-
port = process.env.PORT2;
91-
app.listen(port, () => {
92-
console.log(`TechRolEmi is listening on port ${port}`);
93-
})
94-
} catch (e) {
95-
console.log('Sorry, failed to launch.');
96-
}
93+
// try {
94+
// port = process.env.PORT2;
95+
// app.listen(port, () => {
96+
// console.log(`TechRolEmi is listening on port ${port}`);
97+
// })
98+
// } catch (e) {
99+
// console.log('Sorry, failed to launch.');
100+
// }
97101
});
98-
99-
export { EXCHANGE_RATE_APIKEY, EXCHANGE_BASE_URI, WEATHERBIT_KEY, WEATHERBIT_URI, GoogleclientID, GoogleclientSecret };

loadSecrets.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* IMPORTANT SECRETS */
2+
/*
3+
* To load environment variables, this file must be initialized
4+
* at the top of the application
5+
*
6+
* This file depends on ./config/keys.js in development mode!
7+
*/
8+
9+
import * as dotenv from 'dotenv';
10+
11+
// dev env
12+
dotenv.config();
13+
let EXCHANGE_RATE_APIKEY;
14+
let WEATHERBIT_KEY;
15+
let GoogleclientID;
16+
let GoogleclientSecret;
17+
let MongoDBConString;
18+
let WEATHERBIT_URI='https://api.weatherbit.io/v2.0/';
19+
let EXCHANGE_RATE_URI='https://v6.exchangerate-api.com/v6/';
20+
let CookieKey = 'c93da061ab7f984267e36c8431645035d611bc892c58f0e64614c68a4384a179126e7ed0b5829e460f292f72e9ef4facb68a0894cb2425ba046b82a3bae0b529';
21+
22+
if (process.env.NODE_ENV === "production") {
23+
EXCHANGE_RATE_APIKEY = process.env.EXCHANGERATE_APIKEY;
24+
WEATHERBIT_KEY = process.env.WEATHERBIT_KEY;
25+
GoogleclientID = process.env.GOOGLECLIENT_ID;
26+
GoogleclientSecret = process.env.GOOGLECLIENT_SECRET;
27+
MongoDBConString = process.env.MONGODB_URI;
28+
console.log('Loaded keys from dotenv');
29+
} else {
30+
// dynamically importing keys.js using promise:
31+
let secrets = await getSecrets();
32+
// console.log(secrets);
33+
if(secrets) {
34+
EXCHANGE_RATE_APIKEY = secrets.keys.exchangerateapi.EXCHANGERATE_APIKEY;
35+
WEATHERBIT_KEY = secrets.keys.weatherbitapi.WEATHERBIT_APIKEY;
36+
GoogleclientID = secrets.keys.google.GOOGLECLIENT_ID;
37+
GoogleclientSecret = secrets.keys.google.GOOGLECLIENT_SECRET;
38+
MongoDBConString = secrets.keys.mongodb.MONGODB_CONSTR;
39+
};
40+
}
41+
42+
/*
43+
* Async function loading keys dynamically from ./config/keys.js
44+
* IMPORTANT NOTE: ./config/keys.js is not saved in the archive!
45+
* It is meant to be used in development environment only.
46+
* For `production` build, you must change NODE_ENV accordingly.
47+
*/
48+
function getSecrets(){
49+
return new Promise(resolve => {
50+
setTimeout(() => {
51+
try {
52+
import('./config/keys.js').then((secrets) => {
53+
resolve(secrets);
54+
});
55+
} catch (err) {
56+
console.log(err);
57+
resolve(null);
58+
}
59+
})
60+
}, 200);
61+
}
62+
63+
64+
export { EXCHANGE_RATE_APIKEY, EXCHANGE_RATE_URI, WEATHERBIT_KEY, WEATHERBIT_URI, GoogleclientID, GoogleclientSecret, CookieKey, MongoDBConString };

package-lock.json

Lines changed: 9 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)