This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Remove configuration - Add integrated mock server
- Loading branch information
Michel Zimmer
committed
Aug 31, 2022
1 parent
2cd688d
commit d7aa555
Showing
20 changed files
with
201 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
**/* | ||
!index.html | ||
!mocks/ | ||
!nginx.conf | ||
!package.json | ||
!public/ | ||
!src/ | ||
!tsconfig.json | ||
!tsconfig.node.json | ||
!vite-plugin-serve-handler/ | ||
!vite.config.ts | ||
!yarn.lock |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import fs from "fs" | ||
import http from "http"; | ||
import path from "path" | ||
import process from "process" | ||
import type { Connect, ResolvedConfig } from "vite"; | ||
|
||
import { ServeHandler } from "../vite-plugin-serve-handler"; | ||
|
||
const isApiRoute: (url: string) => boolean = | ||
(url) => url === "/api" || url.startsWith("/api?") || url.startsWith("/api/"); | ||
|
||
const bandwhichdApiServerFromEnv: (viteConfig: ResolvedConfig) => URL | null = | ||
(viteConfig) => { | ||
const bandwhichdApiServer = viteConfig.env["BANDWHICHD_API_SERVER"]; | ||
try { | ||
return new URL(bandwhichdApiServer); | ||
} catch (_) { | ||
return null; | ||
} | ||
}; | ||
|
||
const handleWithMocks = | ||
(request: Connect.IncomingMessage, response: http.ServerResponse) => { | ||
const chunks = []; | ||
request.on("data", chunk => { | ||
chunks.push(chunk); | ||
}); | ||
|
||
request.on("end", () => { | ||
if (request.method !== "GET" | ||
|| request.url !== "/api/v1/stats") { | ||
response.writeHead(404); | ||
response.end(); | ||
return; | ||
} | ||
|
||
if (chunks.length > 0) { | ||
response.writeHead(400); | ||
response.end(); | ||
return; | ||
} | ||
|
||
const format = | ||
request.headers.accept === "text/vnd.graphviz; q=1.0" | ||
? "dot" | ||
: "json"; | ||
|
||
const filePath = path.join(process.cwd(), 'mocks', `stats.${format}`); | ||
const fileStat = fs.statSync(filePath); | ||
|
||
response.writeHead(200, { | ||
"Access-Control-Allow-Origin": "*", | ||
"Content-Length": fileStat.size, | ||
}); | ||
fs.createReadStream(filePath).pipe(response); | ||
}); | ||
}; | ||
|
||
const handleWithServer = | ||
(request: Connect.IncomingMessage, response: http.ServerResponse, viteConfig: ResolvedConfig, bandwhichdApiServer: URL) => { | ||
const upstreamRequestOptions: http.RequestOptions = { | ||
protocol: bandwhichdApiServer.protocol, | ||
host: bandwhichdApiServer.hostname, | ||
port: bandwhichdApiServer.port, | ||
method: request.method, | ||
path: request.url.substring("/api".length), | ||
headers: { | ||
...request.headers, | ||
host: `${bandwhichdApiServer.host}`, | ||
}, | ||
}; | ||
|
||
request.pipe(http.request(upstreamRequestOptions, (upstreamResponse) => { | ||
upstreamResponse.pipe(response).on("error", (error) => { | ||
viteConfig.logger.error("mocks/apiHandler: Error proxying response", { | ||
error, | ||
timestamp: true | ||
}); | ||
response.end(); | ||
}); | ||
})).on("error", (error) => { | ||
viteConfig.logger.error("mocks/apiHandler: Error proxying request", { | ||
error, | ||
timestamp: true | ||
}); | ||
response.end(); | ||
}); | ||
}; | ||
|
||
export const apiHandler: ServeHandler.Handler = | ||
(request, response, viteConfig) => { | ||
if (!isApiRoute(request.url)) { | ||
return false; | ||
} | ||
|
||
const bandwhichdApiServer = bandwhichdApiServerFromEnv(viteConfig); | ||
|
||
if (bandwhichdApiServer === null) { | ||
handleWithMocks(request, response); | ||
} else { | ||
handleWithServer(request, response, viteConfig, bandwhichdApiServer); | ||
} | ||
|
||
return true; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"hosts":{"28e9f9f4-c9ec-7448-d591-f8fce34086ce":{"hostname":"spring-db1","additional_hostnames":[],"connections":{}},"d416fa7d-edef-ded2-5074-3244110a5a3d":{"hostname":"spring-mongodb","additional_hostnames":[],"connections":{}},"fda7ecc9-eb5f-002d-2d11-eb4ab88ef9e4":{"hostname":"spring-logging","additional_hostnames":[],"connections":{}},"b110d228-d4c9-667b-c4da-913425b25175":{"hostname":"ecom-nginx-services","additional_hostnames":[],"connections":{}},"3509c6bc-b535-a72a-2bc1-f5b585e03063":{"hostname":"ecom-cache2","additional_hostnames":[],"connections":{}},"dda70a41-0ee2-8e2c-dc15-1a12cd46b7e4":{"hostname":"ecom-app-live1","additional_hostnames":[],"connections":{}},"f6541cc6-8eea-8e2c-d40b-959e18660923":{"hostname":"ecom-cache","additional_hostnames":[],"connections":{}},"55faca59-16af-34b8-9f0d-882b6531e82d":{"hostname":"spring-staging","additional_hostnames":[],"connections":{}},"2b09a772-335d-d36b-1cfb-eb75f6c9bea1":{"hostname":"spring-app-live2","additional_hostnames":[],"connections":{}},"fd4760d6-baa4-3b27-3bf0-a83174eb5014":{"hostname":"spring-web-live6","additional_hostnames":[],"connections":{}},"35b2b94f-41f9-c120-8852-af0665d5b628":{"hostname":"spring-cache","additional_hostnames":[],"connections":{}},"6075cc26-5b1b-5f01-f0d8-2a78e139a0d2":{"hostname":"spring-cache2","additional_hostnames":[],"connections":{}},"1c9684d1-cb83-501e-63da-9077e9868a98":{"hostname":"spring-services1","additional_hostnames":[],"connections":{}},"2000044d-80c2-fb2f-80a7-30345cea90ff":{"hostname":"ecom-services1","additional_hostnames":[],"connections":{}},"eebbdbb0-1fdc-7802-8b35-6a5f84699eb5":{"hostname":"ecom-services2","additional_hostnames":[],"connections":{}},"045c74d2-e4f6-6c5d-76fa-a55a3b23be6e":{"hostname":"ecom-logging","additional_hostnames":[],"connections":{}},"0ead0515-5d04-9c5a-9a8d-9724d34882ee":{"hostname":"ecom-app-live2","additional_hostnames":[],"connections":{}},"78334682-0244-2adb-6cf2-c243717a3f58":{"hostname":"ecom-web-live3","additional_hostnames":[],"connections":{}},"71d66f34-2bc1-7853-bc9d-0ee87a963264":{"hostname":"ecom-web-live6","additional_hostnames":[],"connections":{}},"a5a0f816-8cb2-a634-9dc5-6d28dbbef6e3":{"hostname":"spring-staging2","additional_hostnames":[],"connections":{}},"40b13e86-8d85-816d-bbe4-09d223eaf94c":{"hostname":"spring-nginx-services","additional_hostnames":[],"connections":{}},"0add42fd-d09b-13ad-62d0-9d7edf87e7b1":{"hostname":"spring-app-live1","additional_hostnames":[],"connections":{}},"98bc86ae-6dd9-05a0-4119-851e82b84dc2":{"hostname":"spring-services2","additional_hostnames":[],"connections":{}},"2c22b189-ed70-9ad1-b758-54bd5b1aef4b":{"hostname":"spring-web-live3","additional_hostnames":[],"connections":{}}},"unmonitoredHosts":{}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
server { | ||
listen 8080; | ||
listen [::]:8080; | ||
listen 3000; | ||
listen [::]:3000; | ||
|
||
location ~ ^/api(/.*)?$ { | ||
proxy_pass ${BANDWHICHD_API_SERVER}$1; | ||
proxy_set_header Host $host; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Real-IP $remote_addr; | ||
} | ||
|
||
location / { | ||
root /usr/share/nginx/html; | ||
index index.html; | ||
sub_filter 'PLACEHOLDER_API_SERVER' '${BANDWHICHD_API_SERVER}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import React from "react"; | ||
import { configuration } from "./Configuration"; | ||
|
||
import styles from "./Header.module.css" | ||
|
||
export const Header: React.FC = | ||
() => <header className={styles.header}>Connected to <code>{configuration.apiServer}</code></header>; | ||
() => <header className={styles.header}>Timeframe: 2 hours</header>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.