Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

Develop server. #329

Merged
merged 9 commits into from
Oct 23, 2019
Merged
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
136 changes: 136 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/unmock-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@types/http-proxy": "^1.17.0",
"concurrently": "^5.0.0",
"express": "^4.17.1",
"helmet": "^3.21.1",
"http-proxy": "^1.18.0",
"unmock-core": "file:../unmock-core",
"unmock-node": "file:../unmock-node"
Expand Down
29 changes: 29 additions & 0 deletions packages/unmock-server/scripts/prepare-cert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
domains=$1

if [ -z ${domains} ];
then
echo "Add domain names like this: bash prepare-cert.sh domain.io,domain.io2"
exit 1
fi

# Split names
arr=(${domains//,/ })

# Build SAN of the form "DNS:domain1,DNS:domain2"
SAN=""

first=true

for i in "${arr[@]}"
do
if [ ${first} = false ];
then
SAN+=,
fi
first=false
SAN+=DNS:$i
done

openssl req -x509 -newkey rsa:4096 -subj "/C=FI/ST=HE/O=Meeshkan/CN=*" -reqexts SAN -extensions SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=${SAN}")) -nodes -keyout key.pem -out cert.pem

echo $(openssl x509 -in cert.pem -text -noout)
45 changes: 30 additions & 15 deletions packages/unmock-server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import debug from "debug";
import express = require("express");
import * as fs from "fs";
// @ts-ignore
import helmet = require("helmet");
import * as http from "http";
import * as https from "https";
import * as os from "os";
import { ISerializedRequest, ISerializedResponse } from "unmock-core";
// import * as os from "os";

import {
ISerializedRequest,
ISerializedResponse,
transform,
} from "unmock-core";
import { createUnmockAlgo } from "./unmock";

const httpPort = 8000;
Expand Down Expand Up @@ -59,39 +66,47 @@ export const requestResponseHandler = (opts: IServerOptions) => {

export const buildApp = (opts?: IServerOptions) => {
const app = express();
app.use(helmet());

const domain = process.env.UNMOCK_SERVER_DOMAIN || "localhost";

app.get(
"/unmock-api",
const { unmock, handler } = requestResponseHandler(opts || {});

app.post(
"/api",
(
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
if (req.hostname === domain) {
res.send(
`unmock-api: host ${req.headers.host}, url: ${
req.url
}, own hostname: ${os.hostname()}, req.hostname: ${req.hostname}`,
if (req.hostname !== domain) {
return next();
}
if (req.query && req.query.code) {
debugLog(`Setting response code to ${req.query.code}`);
const asNumber = parseInt(req.query.code, 10);
Object.values(unmock.services).forEach(service =>
service.state(
// @ts-ignore
transform.withCodes(asNumber),
),
);
} else {
next();
res.json({ code: req.query.code });
return;
}
return res.sendStatus(400);
},
);

const { unmock, handler } = requestResponseHandler(opts || {});

app.all("*", handler);

return { unmock, app };
};

export const startServer = (app: express.Express) => {
const options = {
key: fs.readFileSync("key.pem"),
cert: fs.readFileSync("cert.pem"),
key: fs.readFileSync(`${process.cwd()}/key.pem`),
cert: fs.readFileSync(`${process.cwd()}/cert.pem`),
};
const httpServer = http.createServer(app);
const httpsServer = https.createServer(options, app);
Expand Down