Skip to content

Commit

Permalink
Merge pull request #20 from eexit/fb
Browse files Browse the repository at this point in the history
Host as a Firebase function
  • Loading branch information
eexit authored May 20, 2022
2 parents 320573b + ab646cd commit 27be8eb
Show file tree
Hide file tree
Showing 10 changed files with 1,052 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "mirror-http-server"
}
}
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
node_modules
package-lock.json

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*
firebase-debug.*.log*

# Firebase cache
.firebase/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM node:lts-alpine
RUN apk add --no-cache yarn
WORKDIR /app
COPY . .
COPY functions .
RUN yarn --frozen-lockfile --loglevel=error --prod
EXPOSE 8080
CMD [ "yarn", "start" ]
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
![logo](logo.png)

# Mirror HTTP Server [![Build Status](https://travis-ci.org/eexit/mirror-http-server.svg)](https://travis-ci.org/eexit/mirror-http-server) [![DockerHub](https://img.shields.io/badge/docker-hub-brightgreen.svg?style=flat)](https://hub.docker.com/r/eexit/mirror-http-server/)
# Mirror HTTP Server [![DockerHub](https://img.shields.io/docker/image-size/eexit/mirror-http-server?color=brightgreen)](https://hub.docker.com/r/eexit/mirror-http-server/) [![Firebase function](https://img.shields.io/badge/firebase-function-brightgreen)](https://mirror-http-server.web.app)

*A dummy HTTP server that responds whatever you told it to.*

Testing URL: <https://mirror-http-server.web.app>

Built to play with HTTP or test your API. Make a HTTP call to the dummy server with the specified headers you want the server responds with.

## Usage
Expand Down
19 changes: 19 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"functions": {
"source": "functions"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "mirror"
}
]
}
}
6 changes: 6 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

const mirror = require('./mirror'),
functions = require('firebase-functions');

module.exports = { mirror: functions.https.onRequest(mirror.app) };
12 changes: 6 additions & 6 deletions server.js → functions/mirror.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

const host = '0.0.0.0',
port = 8080;

const _ = require('lodash'),
bunyan = require('bunyan'),
bodyParser = require('body-parser'),
Expand All @@ -15,13 +12,13 @@ app.enable('trust proxy');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Intercepts all HTTP verb requests
// Intercepts all HTTP verbs requests
app.all('*', function (req, res, next) {
// Returned response headers
const responseHeaders = {};

// Parses the wanted response code
const mirrorCode = req.get('X-Mirror-Code') || 200;
const mirrorCode = parseInt(req.get('X-Mirror-Code')) || 200;

const delay = req.get('X-Mirror-Delay') || 0;

Expand Down Expand Up @@ -82,4 +79,7 @@ app.use(function (err, req, res, next) {
res.status(500).json(err);
});

app.listen(port, host, 511, () => logger.info('Listening on http://%s:%s', host, port));
module.exports = {
app: app,
logger: logger,
};
14 changes: 11 additions & 3 deletions package.json → functions/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "mirror-http-server",
"version": "2.0.0",
"description": "A dummy HTTP server that responds whatever you told it to",
"scripts": {
"start": "node server.js | npm run bunyan",
"start:dev": "nodemon server.js | npm run bunyan",
"test": "echo \"No test specified yet\"",
"bunyan": "$(npm bin)/bunyan"
"bunyan": "$(npm bin)/bunyan",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"keywords": [
"node",
Expand All @@ -21,11 +24,16 @@
],
"author": "Joris Berthelot <[email protected]>",
"license": "MIT",
"engines": {
"node": "16"
},
"dependencies": {
"body-parser": "^1.20.0",
"bunyan": "^1.8.15",
"express": "^4.18",
"lodash": ">=4.17.21"
"lodash": ">=4.17.21",
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.18.0"
},
"devDependencies": {
"nodemon": "^2.0.0"
Expand Down
10 changes: 10 additions & 0 deletions functions/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const mirror = require('./mirror'),
host = '0.0.0.0',
port = 8080;

mirror.app.listen(
port, host, 511,
() => mirror.logger.info('Listening on http://%s:%s', host, port)
);
Loading

0 comments on commit 27be8eb

Please sign in to comment.