Skip to content

Docker/microservice #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 59 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
f35a09a
Add dist to gitignore.
ajlee12 May 20, 2020
fd2d6c7
Insert our MongoDB URI into Models. Select pSQL param and use our pSQ…
ajlee12 May 20, 2020
63a3819
Add .catch for deleteBooks fetch.
ajlee12 May 21, 2020
4b6a8f8
Add CMD3 to dependencies.
ajlee12 May 21, 2020
7ca1927
Enable .microCom(); delete unnecessary pseudo-code.
ajlee12 May 21, 2020
a3cb1f0
Enable global error handler.
ajlee12 May 21, 2020
41e39cb
Change first args in .microCom() to reflect correct microservice names.
ajlee12 May 21, 2020
c3160fe
Comment out images keys. Add line spacing between keys.
ajlee12 May 21, 2020
7e95a34
Add and try different values for "networks" key. Comment out after fa…
ajlee12 May 21, 2020
d35b743
Exclude package-lock from gitignore.
ajlee12 May 21, 2020
ab53c2f
Add global error handler in frontend server.
ajlee12 May 21, 2020
057cf7e
Add console logs for req.headers in Books server.
ajlee12 May 21, 2020
dbb7e8b
Delete duplicate require('express') statements.
ajlee12 May 21, 2020
49eaf8e
Comment out microsvc port mapping. Add comments re: ports & networks.
ajlee12 May 22, 2020
ffd58eb
Fix console logs in controllers to reflect correct service names.
ajlee12 May 22, 2020
16c91b9
Comment out http-proxy usage. Use res.redirect for routing to books s…
ajlee12 May 22, 2020
22e8831
Expand comments on top.
ajlee12 May 22, 2020
8c214c0
Fix deletebook bug: remove http-proxy, use res.redirect while setting…
ajlee12 May 22, 2020
5344e7f
Fix DELETE bug: remap ports in yaml; use res.redirect in servers.
ajlee12 May 22, 2020
7d840e7
Update eslint in devDep.
ajlee12 May 22, 2020
09a7c5e
Format/cleanup following linter.
ajlee12 May 22, 2020
1018c5e
Re-format Chaos Test logs.
ajlee12 May 22, 2020
876142a
Install and require dotenv. Use port # declared in .env in bookContro…
ajlee12 May 22, 2020
efecd86
Install dotenv in all microservices.
ajlee12 May 22, 2020
a3189e3
Add .env files to directories of microservices.
ajlee12 May 22, 2020
6c3e763
Add () to invoke dotenv.config. Routing to orders endpt now functions…
ajlee12 May 22, 2020
fd358ea
Require in dotenv and use process.env for port values.
ajlee12 May 22, 2020
a588c5c
Remove unnec. "images" and "networks" keys.
ajlee12 May 22, 2020
2191449
Expose only internal ports for microservices. Enhance/clarify the com…
ajlee12 May 22, 2020
2d89f1d
Format Chaos Flow test; remove console log for res.
ajlee12 May 22, 2020
2778206
Revert "Expose only internal ports for microservices. Enhance/clarify…
ajlee12 May 22, 2020
7e9423c
Fix typo in orders/getcustomer*info* (was 'data').
ajlee12 May 22, 2020
007f46c
Clarify comments re. ports and Nginx.
ajlee12 May 23, 2020
cc274c1
Merge pull request #25 from ajlee12/docker/microservice
Umius-Brian May 23, 2020
692b28d
Merge pull request #1 from oslabs-beta/docker/microservice
ajlee12 May 23, 2020
608472b
Remove CMD v2 and v3. Install v4. Remove axios for Orders microsvc.
ajlee12 May 25, 2020
2cc9ebe
Place db URIs in .env.
ajlee12 May 25, 2020
7ef4541
Remove body-parser and use express.json instead. Ref db URIs and port…
ajlee12 May 25, 2020
aa9dc81
Place books db URI in dotenv and ref to it with process.env in Model.
ajlee12 May 25, 2020
db8b37a
Place customers db URI in dotenv and ref to it with process.env in Mo…
ajlee12 May 25, 2020
cc33184
Add global err handler.
ajlee12 May 25, 2020
ced64cd
Add global err handler. Place orders db URI in dotenv and ref to it w…
ajlee12 May 25, 2020
ad56c3c
Use express.json. Ref db URIs and ports in .env. Remove/cleanup comme…
ajlee12 May 25, 2020
d8eab24
Require the new CMD4 instead of 3.
ajlee12 May 25, 2020
d59a6a1
Correct service names in microCom invocations.
ajlee12 May 25, 2020
0c5a981
Merge branch 'docker/microservice' of https://github.com/ajlee12/Chro…
ajlee12 May 25, 2020
ff0ffb2
Update to use CMD 3.0.0.
ajlee12 May 27, 2020
d51e02d
Add volumes for each service. Change container names to match microse…
ajlee12 May 27, 2020
10e0786
Change WORKDIR to match all other services.
ajlee12 May 27, 2020
34324be
Change microCom params to test logging of Docker stats.
ajlee12 May 27, 2020
6926188
Update to newer CMD version.
ajlee12 May 27, 2020
e0235b1
Update to newest CMD version.
ajlee12 May 27, 2020
b151d55
Update CMD to newest vers. Uninstall unnec packages.
ajlee12 May 27, 2020
b51aeb3
Add example .env in top dir.
ajlee12 May 27, 2020
a8bc0d2
Include .env in .gitignore.
ajlee12 May 27, 2020
81035ef
Update .gitignore to incl .env.
ajlee12 May 27, 2020
1fe00b2
Untrack all .env files.
ajlee12 May 27, 2020
d9c2594
Delete .DS_Store
ajlee12 May 28, 2020
1c6a722
Merge pull request #27 from ajlee12/docker/microservice
Umius-Brian May 28, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
20 changes: 20 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# In each of the microservices, create an .env file in the root directory.
# Supply port values for use by the Dockerfile.
# Make sure the values are consistent for all services.
# Supply your URI for the Chronos db.
# This is where the aggregated comm/health/container info are stored.
# Supply a unique URI for each service that's NOT your Chronos db.
# This is where the entries of books/orders/customers are stored.

# These should be the same for all services.
FRONTEND_PORT=
BOOKS_PORT=
CUSTOMERS_PORT=
ORDERS_PORT=
NGINX_PORT=
CHRONOS_PSQL=
CHRONOS_MONGO=

# This one will be unique for every service.
# Note that the Frontend service will not need this.
BOOKS_DB=
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
.eslintrc.js
package-lock.json
.env
node_modules
6 changes: 4 additions & 2 deletions books/BookController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config();
const fetch = require('node-fetch');
const BookModel = require('./BookModel');

Expand Down Expand Up @@ -52,10 +53,11 @@ BookController.deleteBook = (req, res, next) => {

// This controller gets order info from the order application
BookController.getorderinfo = (req, res, next) => {
console.log('req.headers (in bookController.js):', req.headers);
// const { body } = req;
// since it's a get request, you technically don't need
// all the headers but it's more declarative this way
fetch('http://localhost:8080/orders/getorders', {
fetch(`http://orders:${process.env.ORDERS_PORT}/orders/getorders`, {
method: 'GET',
headers: {
'Content-Type': 'Application/JSON',
Expand All @@ -69,7 +71,7 @@ BookController.getorderinfo = (req, res, next) => {
return next();
})
.catch((error) => {
console.log(`There was an error in getting customers data ${error}`);
console.log(`There was an error in getting orders data ${error}`);
});
};
module.exports = BookController;
10 changes: 5 additions & 5 deletions books/BookModel.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const mongoose = require('mongoose');
require('dotenv').config();

// pull schema from the mongoose object
const { Schema } = mongoose;

// database link
// can change but woun't advice that because the book application database lives here
const myURI = 'mongodb+srv://tim:tim@cluster0-khxef.mongodb.net/test?retryWrites=true&w=majority';
// DB link for books data.
const book_db_URI = `${process.env.BOOKS_DB}`;

const URI = process.env.MONGO_URI || myURI;
// const URI = process.env.MONGO_URI || myURI;

// connect the database, if error, log will be sent to the terminal
mongoose.connect(URI, { useNewUrlParser: true, useUnifiedTopology: true })
mongoose.connect(book_db_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected!!!********* Books Database is live!!!'))
.catch((err) => console.log('Connection Error ', err));

Expand Down
60 changes: 32 additions & 28 deletions books/BookServer.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
const cmd = require('chronos-microservice-debugger3');

cmd.propagate();
const PORT = 4545;
const express = require('express');
const path = require('path');
const cors = require('cors');

const app = express();
const bodyParser = require('body-parser');
const cmd = require('chronos-microservice-debugger4');
const controller = require('./BookController.js');
require('dotenv').config();

// we're using the chronos debugger tool here to intercept
// request and propagate our context onto said request as it travels

// app.use('/', cmd.microCom('books_microservice', 'sql', 'postgres://tsfcbdjo:l8AWzEJEyhxtR-ERoj7HNjIqBuRCqm9f@rajje.db.elephantsql.com:5432/tsfcbdjo'));
// cmd.microHealth('books_microservice', 'sql', 'postgres://tsfcbdjo:l8AWzEJEyhxtR-ERoj7HNjIqBuRCqm9f@rajje.db.elephantsql.com:5432/tsfcbdjo', 'h');

app.use('/', cmd.microCom('books_microservice', 'sql', 'postgres://kpbljbrv:Ry1hO5KPIU-jvVyGnHHne-yplDr2Yk3H@rajje.db.elephantsql.com:5432/kpbljbrv', 'yes', 'm'));
// Places a unique header on every req in order to trace the path in the req's life cycle.
cmd.propagate();

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.json());

// Invoke .microCom with the 6 params to enable logging of comm and health data to your own db.
// Params (6): microservice name, db type, db URI, want health data?, query freq, is service Dockerized?
// If running a svc in a Docker container, please give container the same name as the microservice...
// ... to ensure proper logging of container stats.
app.use('/', cmd.microCom(
'books',
// PostgreSQL
'sql',
`${process.env.CHRONOS_PSQL}`,
// MongoDB
// 'mongo',
// `${process.env.CHRONOS_MONGO}`,
'no',
'm',
'yes' // <-- Is the service Dockerized?
));

app.use(cors());
app.use('/', express.static(path.resolve(__dirname, '../frontend')));

// ********** I PROBABLY STILL NEED THIS PART FOR CHRONOS TO WORK AND DEBUG MY MICOSERVICE *************

// CHAOS FLOW - SIMPLY A TEST FOR THE EXPESS SERVER
app.use((req, res, next) => {
console.log(
`***************************************************************************************
CHAOS FLOW TEST --- METHOD:${req.method}, PATH: ${
req.url
}, BODY: ${JSON.stringify(req.body)}, ID: ${req.query.id}
CHAOS FLOW TEST --- METHOD:${req.method},
PATH: ${req.url},
BODY: ${JSON.stringify(req.body)},
ID: ${req.query.id}
***************************************************************************************`,
);
next();
Expand All @@ -59,9 +65,8 @@ app.get('/books/getordersinfo', controller.getorderinfo, (req, res) => {
res.status(200).json(res.locals.getorderinfo);
});


// This is my global error handler - isn't being used right now and it's not breaking anything so...
function errorHandler(error, req, res, next) {
// Global error handler
app.use((error, req, res, next) => {
// console.log(err.stack);
const defaultErr = {
log: 'Express error handler caught unknown middleware error',
Expand All @@ -72,9 +77,8 @@ function errorHandler(error, req, res, next) {
console.log(`Here is the error object's response: ${errorObj.log}`);

res.status(errorObj.status).json(errorObj.message);
}
});

// Open and listen to server on said port
app.listen(PORT, () => {
console.log(`Book server running on port ${PORT} ...`);
app.listen(process.env.BOOKS_PORT, () => {
console.log(`Book server running on port ${process.env.BOOKS_PORT} ...`);
});
Loading