Skip to content

Mostly fixing tests and deployment documentation with a hint of containerization #31

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '2.1'
orbs:
node: circleci/[email protected]
jobs:
run-tests:
docker:
- image: 'cimg/base:stable'
- image: 'mongo:5.0'
steps:
- checkout
- run:
name: Validating the port is open for MongoDB
command: dockerize -wait tcp://localhost:27017 -timeout 1m
- node/install
- node/install-packages
- run:
command: node server.js
background: true
- run:
command: npm test
workflows:
test_json_proxy:
jobs:
- run-tests
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
.DS_Store
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.DS_Store
npm-debug.log
.DS_Store
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM docker.io/library/node:14

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Expose the port defined in server.js
EXPOSE 4500

# Start things up
CMD [ "node", "server.js" ]
140 changes: 136 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This is a very simple proxy server to get around CORS issues when an API does not provide JSONP.

## How to use.
This is live at proxy.hackeryou.com.
This is live at proxy.hackeryou.com.

The set up is very simple, when you make a request with `$.ajax` you might right it like this.

Expand Down Expand Up @@ -44,7 +44,7 @@ However because of CORS you might not be able to access the API this way. If the
}).then(function(res) {
/* ... */
});

### Using the Fetch API

You can use the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for making requests too
Expand Down Expand Up @@ -74,14 +74,146 @@ fetch(url)

You pass your information via the `data` object, in there there are a bunch of options you need to pass.

param | type | description
param | type | description
----- | ------ | -----------
reqUrl | `string` | The URL for your endpoint.
params | `object` / `params[key]` | The options that you would normally pass to the data object
proxyHeaders | `object` / `proxyHeaders[header]` | Headers to pass to the API
xmlToJSON | `boolean` | Defaults to `false`, change to true if API returns XML
useCache | `boolean` | Defaults to `false`, change to store your response from an API for 1 hour.
useCache | `boolean` | Defaults to `false`, change to store your response from an API for 1 hour.

## How to deploy

### System prerequisites

To deploy on a single Linux host use the following directions. These are best all run as root except launching the actual app unless its as a container.

These are specific to Rocky Linux 8, but should work on CentOS 8 and RHEL 8 as well.

First up let's install the required packages from the Rocky repository.

```sh
sudo dnf module enable -y nodejs:14
sudo dnf module install -y nodejs
sudo dnf module install -y python36
sudo dnf install -y git make gcc gcc-c++ checkpolicy
```

### The MongoDB setup

This could be done on a separate host but then then server.js needs to be updated with the location. Ideally you could do this an an environment variable.

First step to get MongoDB is to point to its repository (as root)

```sh
cat > /etc/yum.repos.d/mongodb-org-5.0.repo <<EOF
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
EOF
```

Next prep the system for MongoDB (as root)
The semodule commands take a minute.

```sh
ulimit -n 65536
cat > mongodb_cgroup_memory.te <<EOF
module mongodb_cgroup_memory 1.0;
require {
type cgroup_t;
type mongod_t;
class dir search;
class file { getattr open read };
}
#============= mongod_t ==============
allow mongod_t cgroup_t:dir search;
allow mongod_t cgroup_t:file { getattr open read };
EOF
cat > mongodb_proc_net.te <<EOF
module mongodb_proc_net 1.0;
require {
type proc_net_t;
type mongod_t;
class file { open read };
}
#============= mongod_t ==============
allow mongod_t proc_net_t:file { open read };
EOF
checkmodule -M -m -o mongodb_cgroup_memory.mod mongodb_cgroup_memory.te
checkmodule -M -m -o mongodb_proc_net.mod mongodb_proc_net.te
semodule_package -o mongodb_cgroup_memory.pp -m mongodb_cgroup_memory.mod
semodule_package -o mongodb_proc_net.pp -m mongodb_proc_net.mod
semodule -i mongodb_cgroup_memory.pp
semodule -i mongodb_proc_net.pp
```

And install and start up MongoDB
```sh
sudo dnf install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl daemon-reload
sudo systemctl start mongod
```

Is it running? If the output looks like this we are good.

```sh
[user@proxy ~]$ ps auxww | grep mongod
mongod 17707 1.4 5.2 1591976 97824 ? Sl 02:57 0:00 /usr/bin/mongod -f /etc/mongod.conf
root 17852 0.0 0.0 221928 1152 pts/0 R+ 02:57 0:00 grep --color=auto mongod
```

### Get the code

Next let's get the json-proxy code, and enter the directory

```sh
git clone https://github.com/HackerYou/json-proxy
cd json-proxy
```

### Run it as is

Just install the dependancies, and start it up.

```sh
npm install
node server.js
```

### Run it in a container

Note: podman is used in this example. If you are on a system with the docker cli instead then just replace `podman` with `docker` as they should be completely interchange. (Podman was designed to use Docker's command line syntax.)

If you want to run it as a container then there are an additional couple steps. First we get the tools and build the image. The container-tools install takes a couple minutes and so will the first build.

```sh
dnf module install -y container-tools
podman build . -t hackeryou/json-proxy
```

Next we make sure the image built
```sh
[root@proxy json-proxy]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/hackeryou/json-proxy latest fd57faac8df7 19 seconds ago 1.01 GB
docker.io/library/node 14 db40ea2d00ea 2 weeks ago 973 MB
```

Now we run it, replace "80" with any port you want to expose it on
```sh
podman run -p 80:4500 -d hackeryou/json-proxy
```

And is it running?
```sh
[root@proxy json-proxy]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a904f9d1e7e6 localhost/hackeryou/json-proxy:latest node server.js 8 seconds ago Up 7 seconds ago 0.0.0.0:80->4500/tcp cranky_zhukovsky
```

And now its available for use based on the usage section up top.
7 changes: 0 additions & 7 deletions circle.yml

This file was deleted.

2 changes: 1 addition & 1 deletion models/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ const CacheSchema = new mongoose.Schema({
date: String
});

module.exports = mongoose.model('Cache', CacheSchema);
module.exports = mongoose.model('Cache', CacheSchema);
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"ignore": ["test"]
}
}
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const DB_PATH = 'mongodb://localhost/cache';
const Cache = require('./models/Cache.js');

app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Content-length, Accept, Cache-Control');
res.setHeader('Cache-Control','no-cache, no-store, must-revalidate');
res.setHeader('Pragma','no-cache');
res.setHeader('Expires','0');
res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
next();
});
});

app.use(express.json())

Expand Down
1 change: 0 additions & 1 deletion test/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@ describe('Get requests', () => {
});
});
});

11 changes: 7 additions & 4 deletions test/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ describe('Post requests', () => {
request
.post('http://localhost:4500')
.send({
reqUrl: 'http://api.hackeryou.com/v1/key',
reqUrl: 'https://reqbin.com/echo/post/json',
params: {
email: '[email protected]'
"Id": 12345,
"Customer": "Jane Smith",
"Quantity": 1,
"Price": 99.99
}
})
.end((err,res) => {
expect(err).to.be(null);
done();
done();
})
});
});
});