Skip to content

Commit ab646cd

Browse files
committed
Host as a Firebase function
1 parent 320573b commit ab646cd

10 files changed

+1052
-22
lines changed

.firebaserc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "mirror-http-server"
4+
}
5+
}

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
node_modules
22
package-lock.json
3+
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
firebase-debug.log*
11+
firebase-debug.*.log*
12+
13+
# Firebase cache
14+
.firebase/

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM node:lts-alpine
22
RUN apk add --no-cache yarn
33
WORKDIR /app
4-
COPY . .
4+
COPY functions .
55
RUN yarn --frozen-lockfile --loglevel=error --prod
66
EXPOSE 8080
77
CMD [ "yarn", "start" ]

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
![logo](logo.png)
22

3-
# 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/)
3+
# 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)
44

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

7+
Testing URL: <https://mirror-http-server.web.app>
8+
79
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.
810

911
## Usage

firebase.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"functions": {
3+
"source": "functions"
4+
},
5+
"hosting": {
6+
"public": "public",
7+
"ignore": [
8+
"firebase.json",
9+
"**/.*",
10+
"**/node_modules/**"
11+
],
12+
"rewrites": [
13+
{
14+
"source": "**",
15+
"function": "mirror"
16+
}
17+
]
18+
}
19+
}

functions/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
const mirror = require('./mirror'),
4+
functions = require('firebase-functions');
5+
6+
module.exports = { mirror: functions.https.onRequest(mirror.app) };

server.js functions/mirror.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use strict';
22

3-
const host = '0.0.0.0',
4-
port = 8080;
5-
63
const _ = require('lodash'),
74
bunyan = require('bunyan'),
85
bodyParser = require('body-parser'),
@@ -15,13 +12,13 @@ app.enable('trust proxy');
1512
app.use(bodyParser.json());
1613
app.use(bodyParser.urlencoded({ extended: true }));
1714

18-
// Intercepts all HTTP verb requests
15+
// Intercepts all HTTP verbs requests
1916
app.all('*', function (req, res, next) {
2017
// Returned response headers
2118
const responseHeaders = {};
2219

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

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

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

85-
app.listen(port, host, 511, () => logger.info('Listening on http://%s:%s', host, port));
82+
module.exports = {
83+
app: app,
84+
logger: logger,
85+
};

package.json functions/package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{
22
"name": "mirror-http-server",
3-
"version": "2.0.0",
43
"description": "A dummy HTTP server that responds whatever you told it to",
54
"scripts": {
65
"start": "node server.js | npm run bunyan",
76
"start:dev": "nodemon server.js | npm run bunyan",
87
"test": "echo \"No test specified yet\"",
9-
"bunyan": "$(npm bin)/bunyan"
8+
"bunyan": "$(npm bin)/bunyan",
9+
"serve": "firebase emulators:start --only functions",
10+
"shell": "firebase functions:shell",
11+
"deploy": "firebase deploy --only functions",
12+
"logs": "firebase functions:log"
1013
},
1114
"keywords": [
1215
"node",
@@ -21,11 +24,16 @@
2124
],
2225
"author": "Joris Berthelot <[email protected]>",
2326
"license": "MIT",
27+
"engines": {
28+
"node": "16"
29+
},
2430
"dependencies": {
2531
"body-parser": "^1.20.0",
2632
"bunyan": "^1.8.15",
2733
"express": "^4.18",
28-
"lodash": ">=4.17.21"
34+
"lodash": ">=4.17.21",
35+
"firebase-admin": "^10.0.2",
36+
"firebase-functions": "^3.18.0"
2937
},
3038
"devDependencies": {
3139
"nodemon": "^2.0.0"

functions/server.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const mirror = require('./mirror'),
4+
host = '0.0.0.0',
5+
port = 8080;
6+
7+
mirror.app.listen(
8+
port, host, 511,
9+
() => mirror.logger.info('Listening on http://%s:%s', host, port)
10+
);

0 commit comments

Comments
 (0)