Skip to content

Commit 7070fde

Browse files
committed
Initial commit
0 parents  commit 7070fde

File tree

9 files changed

+480
-0
lines changed

9 files changed

+480
-0
lines changed

.circleci/config.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: 2.1
2+
workflows:
3+
build:
4+
jobs:
5+
- build:
6+
filters:
7+
branches:
8+
only: master
9+
context: Docker Hub
10+
build_test:
11+
jobs:
12+
- build_test:
13+
filters:
14+
branches:
15+
ignore: master
16+
jobs:
17+
build:
18+
docker:
19+
- image: circleci/node:latest
20+
steps:
21+
- setup_remote_docker
22+
- checkout
23+
- run: |
24+
TAG=$CIRCLE_SHA1
25+
DOCKER_REPO=$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME
26+
docker build -t $DOCKER_REPO:$TAG .
27+
docker tag $DOCKER_REPO:$TAG $DOCKER_REPO:latest
28+
echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
29+
docker push $DOCKER_REPO:$TAG
30+
docker push $DOCKER_REPO:latest
31+
32+
build_test:
33+
docker:
34+
- image: circleci/node:latest
35+
steps:
36+
- setup_remote_docker
37+
- checkout
38+
- run: |
39+
TAG=$CIRCLE_SHA1
40+
DOCKER_REPO=$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME
41+
docker build -t $DOCKER_REPO:$TAG .

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM node:10-alpine
2+
3+
WORKDIR /app
4+
COPY package.json /app
5+
COPY yarn.lock /app
6+
7+
RUN yarn install
8+
9+
COPY src/ /app/src
10+
11+
EXPOSE 80
12+
CMD [ "node", "src" ]

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DEFAULT_GOAL := main
2+
3+
main:
4+
@docker build . -t srnd/docker-redirect-many
5+
run:
6+
@docker run srnd/docker-redirect-many

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"express": "^4.17.1"
4+
}
5+
}

src/env-all-matching.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = key => {
2+
return Object.keys(process.env)
3+
.filter(env => env.substr(0, key.length) === key)
4+
.map(env => process.env[env]);
5+
};

src/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const app = require("express")();
2+
const envAllMatching = require("./env-all-matching");
3+
4+
const redirectDomains = envAllMatching("REDIRECT_")
5+
.map(env => env.split(":"))
6+
.reduce((a, b) => {
7+
a[b[0]] = b[1];
8+
return a;
9+
}, {});
10+
11+
app.use((req, res) => {
12+
let redirectDomain = null;
13+
if (req.get("host") in redirectDomains)
14+
redirectDomain = redirectDomains[req.get("host")];
15+
else if (req.get("host").split(".").length === 2)
16+
redirectDomain = `www.${req.get("host")}`;
17+
18+
const proto = req.get("x-forwarded-proto") || req.protocol;
19+
20+
if (redirectDomain)
21+
return res.redirect(`${proto}://${redirectDomain}${req.originalUrl}`);
22+
else if (req.path === "/status") return res.send("ok");
23+
else
24+
return res
25+
.status(404)
26+
.contentType("text")
27+
.send("404 page not found");
28+
});
29+
30+
const port = parseInt(process.env.PORT) || 80;
31+
app.listen(port, () =>
32+
console.log(
33+
`Listening on port ${port}, redirections:\n\n${JSON.stringify(
34+
redirectDomains,
35+
null,
36+
2
37+
)}`
38+
)
39+
);

0 commit comments

Comments
 (0)