-
Notifications
You must be signed in to change notification settings - Fork 0
/
storybookMockServer.ts
170 lines (150 loc) · 4.99 KB
/
storybookMockServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
getNowPlaying,
ChannelName,
NowPlayingResponse,
} from "./packages/streamplayer-server/lib/StreamPlayerAPI";
import {
getDockerList,
startContainer,
stopContainer,
} from "./packages/dockerlist-server/lib/DockerListAPI";
import express, { Request, Response } from "express";
const app = express();
const port = 3100;
enum CORS_MODE {
NONE,
DEBUG,
}
const corsMode: CORS_MODE =
process.argv.length > 2 && process.argv[2] === "--CORS=debug"
? CORS_MODE.DEBUG
: CORS_MODE.NONE;
const setCorsHeaders = (corsMode: CORS_MODE, res: Response): void => {
if (corsMode === CORS_MODE.DEBUG) {
console.log("CORS DEBUG MODE");
res.header("Access-Control-Allow-Origin", "http://localhost:6006"); // update to match the domain you will make the request from
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
}
};
const logRequest = (req: Request): void => {
console.log(`Request made to ${req.url}`);
};
const startServer = (corsMode: CORS_MODE): void => {
app.get("/", (req, res) => res.sendStatus(404));
app.get("/api/nowplaying/radio2", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
try {
const response = await getNowPlaying(ChannelName.RADIO2);
res.send(response);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.get("/mock/square/api/nowplaying/radio2", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
const mockResponse: NowPlayingResponse = {
artist: "Family Of The Year",
title: "Hero",
last_updated: "2020-10-31T11:57:03",
songImageUrl: "http://localhost:3100/square.jpg",
name: "Spijkers Met Koppen / Dolf Jansen, Felix Meurders",
imageUrl:
"https://radio-images.npo.nl/{format}/34e8df87-0f33-45c6-bd1a-f2528ff87626/b4c0c59c-1ade-4d9f-b2ed-a5710ca23e8e.png",
};
res.send(mockResponse);
});
app.get("/mock/landscape/api/nowplaying/radio2", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
const mockResponse: NowPlayingResponse = {
artist: "Family Of The Year",
title: "Hero",
last_updated: "2020-10-31T11:57:03",
songImageUrl: "http://localhost:3100/landscape.jpg",
name: "Spijkers Met Koppen / Dolf Jansen, Felix Meurders",
imageUrl:
"https://radio-images.npo.nl/{format}/34e8df87-0f33-45c6-bd1a-f2528ff87626/b4c0c59c-1ade-4d9f-b2ed-a5710ca23e8e.png",
};
res.send(mockResponse);
});
app.get("/api/nowplaying/radio3", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
try {
const response = await getNowPlaying(ChannelName.RADIO3);
res.send(response);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.get("/api/nowplaying/sky", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
try {
const response = await getNowPlaying(ChannelName.SKY);
res.send(response);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.get("/api/nowplaying/pinguin", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
try {
const response = await getNowPlaying(ChannelName.PINGUIN);
res.send(response);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.get("/api/dockerlist", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
try {
const response = await getDockerList();
res.send(response);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.get("/api/dockerlist/start/:id", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
try {
const response = await startContainer(req.params.id);
res.send(response);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.get("/api/dockerlist/stop/:id", async (req, res) => {
logRequest(req);
setCorsHeaders(corsMode, res);
try {
const response = await stopContainer(req.params.id);
res.send(response);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.use(express.static("mocks/public"));
app.listen(port, () =>
console.log(`Storybook Mock server listening on port ${port}!`)
);
};
startServer(corsMode);
module.exports = {
getNowPlaying,
};