Skip to content

Commit

Permalink
documentation updates and dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarprabhu-98 committed Sep 22, 2019
1 parent 004fbac commit 1889cc6
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Web app for tracking user's expenses, with features like resolving payments of people in a group, analysis of transactions, etc.

### Usage
Checkout USAGE.md
Checkout (USAGE.md)[blob/master/USAGE.md]

### File Structure
### Project Structure
- `server`: contains Express server code with MySQL integration for the API's
- `client`: contains Angular code to use server API's and provide client interface

Expand Down
27 changes: 20 additions & 7 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
## Using Docker

### Client
Reference: https://github.com/avatsaev/angular4-docker-example

```
docker build -t client .
docker run -d -p 8080:80 client
```


## General
0. Installation Node.js and Npm
```
$ sudo apt-get install nodejs npm
sudo apt-get install nodejs npm
```
1. Clone the repo
```
$ git clone https://github.com/CSE-Projects/crypto-watch.git
git clone https://github.com/CSE-Projects/crypto-watch.git
```
## Server
### Server
Check [README.md](blob/master/server/README.md)
### Client
## Client
1. Move to the client folder
```
$ cd client
cd client
```
2. install dependencies
```
$ npm install
npm install
```
3. Run locally
```
$ ng serve -o
ng serve -o
```
The client app can be accessed from http://localhost:4200
36 changes: 36 additions & 0 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Credits: https://github.com/avatsaev/angular4-docker-example

### STAGE 1: Build ###

# We label our stage as 'builder'
FROM node:9-alpine as builder

COPY package.json package-lock.json ./

RUN npm set progress=false && npm config set depth 0 && npm cache clean --force

## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i && mkdir /ng-app && cp -R ./node_modules ./ng-app

WORKDIR /ng-app

COPY . .

## Build the angular app in production mode and store the artifacts in dist folder
RUN $(npm bin)/ng build --prod


### STAGE 2: Setup ###

FROM nginx:1.13.3-alpine

## Copy our default nginx config
COPY nginx/default.conf /etc/nginx/conf.d/

## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*

## From 'builder' stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /ng-app/dist /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM node:10

WORKDIR /app

COPY . .

RUN npm install

EXPOSE 3000

CMD ["npm", "start"]
15 changes: 15 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### Server

Environment variables to be set

- Secret for jwt token generation
- Username and password to connect to the database

`/bin/.env` file contains default variables set for non-prod use. They are loaded using [dotenv](https://www.npmjs.com/package/dotenv) package

### Usage

Assuming mysql server is up on port 3306 and access allowed with the username and password
```
npm start
```
4 changes: 3 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var secret_file = require('./secret');
var cors = require('cors');
// helps secure app by adding various http headers
const helmet = require('helmet');
const secret = process.env.JWT_SECRET;

// router
var authRouter = require('./routes/authHandler');
Expand All @@ -32,7 +33,8 @@ app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static('../client/dist'));
// jwt middleware for checking existence od jwt token for paths other than the mentioned ones
app.use(expressJwt({secret: secret_file.secret}).unless({path: ['/api/auth', '/api/auth/login']}));
// app.use(expressJwt({secret: secret_file.secret}).unless({path: ['/api/auth', '/api/auth/login']}));
app.use(expressJwt({secret: secret}).unless({path: ['/api/auth', '/api/auth/login']}));
app.use(cors());
app.use(helmet());

Expand Down
3 changes: 3 additions & 0 deletions server/bin/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
JWT_SECRET=YOUR_SECRET
DB_USERNAME=crypto_watch
DB_PASSWORD=crypto_watch123
6 changes: 4 additions & 2 deletions server/bin/mysql_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ var mysql = require('mysql');
// connection parameters
var connection = mysql.createConnection({
host: 'localhost',
user: 'crypto_watch',
password: 'crypto_watch123'
// user: 'crypto_watch',
// password: 'crypto_watch123'
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD
});

// connect to database and create default tables if they don't exist
Expand Down
5 changes: 5 additions & 0 deletions server/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ var http = require('http');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

// load environment variables from .env file when non-prod use
if (!process.env.NODE_ENV || process.env.NODE_ENV !== 'production') {
require('dotenv').load();
}

/**
* Create HTTP server.
*/
Expand Down
5 changes: 5 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"debug": "~2.6.9",
"dotenv": "^8.1.0",
"express": "~4.16.0",
"express-jwt": "^5.3.1",
"helmet": "^3.14.0",
Expand Down
5 changes: 4 additions & 1 deletion server/routes/authHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var bcrypt = require('../bin/password');
var secret_file = require('../secret');
const jwt = require('jsonwebtoken');

const secret = process.env.JWT_SECRET;

/**
* route /auth/
* type: POST
Expand Down Expand Up @@ -93,7 +95,8 @@ router.post('/login', function(req, res) {
if (isMatch) {
// if password match
// create a jwt token as a response
var token = jwt.sign({userID: username}, secret_file.secret, {expiresIn: '24h'});
// var token = jwt.sign({userID: username}, secret_file.secret, {expiresIn: '24h'});
var token = jwt.sign({userID: username}, secret, {expiresIn: '24h'});
res.send(token)
}
else {
Expand Down
9 changes: 0 additions & 9 deletions server/routes/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions server/views/index.jade

This file was deleted.

0 comments on commit 1889cc6

Please sign in to comment.