diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..e28e2e3 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,24 @@ +version: '2.1' +orbs: + node: circleci/node@4.7.0 +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 diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b0e3907 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +node_modules +npm-debug.log +.DS_Store diff --git a/.gitignore b/.gitignore index 28f1ba7..b0e3907 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -.DS_Store \ No newline at end of file +npm-debug.log +.DS_Store diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..209df8f --- /dev/null +++ b/Dockerfile @@ -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" ] diff --git a/README.md b/README.md index f5d59a6..b1116f3 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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 < mongodb_cgroup_memory.te < mongodb_proc_net.te <4500/tcp cranky_zhukovsky +``` +And now its available for use based on the usage section up top. diff --git a/circle.yml b/circle.yml deleted file mode 100644 index 579a7d6..0000000 --- a/circle.yml +++ /dev/null @@ -1,7 +0,0 @@ -machine: - node: - version: 8.0.0 -test: - pre: - - node server.js: - background: true diff --git a/models/Cache.js b/models/Cache.js index 7f55b6b..74091ab 100644 --- a/models/Cache.js +++ b/models/Cache.js @@ -8,4 +8,4 @@ const CacheSchema = new mongoose.Schema({ date: String }); -module.exports = mongoose.model('Cache', CacheSchema); \ No newline at end of file +module.exports = mongoose.model('Cache', CacheSchema); diff --git a/nodemon.json b/nodemon.json index dd25324..7f3131e 100644 --- a/nodemon.json +++ b/nodemon.json @@ -1,3 +1,3 @@ { "ignore": ["test"] -} \ No newline at end of file +} diff --git a/server.js b/server.js index 5691925..287204a 100644 --- a/server.js +++ b/server.js @@ -9,7 +9,7 @@ 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'); @@ -17,7 +17,7 @@ app.use(function(req, res, next) { res.setHeader('Expires','0'); res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS'); next(); -}); +}); app.use(express.json()) diff --git a/test/get.js b/test/get.js index 93e77fc..8b18343 100644 --- a/test/get.js +++ b/test/get.js @@ -56,4 +56,3 @@ describe('Get requests', () => { }); }); }); - diff --git a/test/post.js b/test/post.js index 413a9af..c7091fc 100644 --- a/test/post.js +++ b/test/post.js @@ -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: 'ryan@hackeryou.com' + "Id": 12345, + "Customer": "Jane Smith", + "Quantity": 1, + "Price": 99.99 } }) .end((err,res) => { expect(err).to.be(null); - done(); + done(); }) }); -}); \ No newline at end of file +});