Skip to content

Commit 58bdea4

Browse files
committed
initial commit to add http server option
1 parent 1e2b16e commit 58bdea4

8 files changed

+1327
-7
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,11 @@ wrangler publish
8686
```
8787

8888
> We don't do this often and generally use GitHub actions to do all of our deployments for us
89+
90+
## HTTP Server
91+
92+
There's also an http webserver in the /http folder. It can be run with `npm run dev` or `npm start`. To run locally, you need to set the following vars (for local testing, you can use an .env file in the /http folder):
93+
94+
- CLOUDFLARE_TOKEN (token must have permissions to read the KVs the API uses)
95+
- CACHE_BASIC_AUTH (used for caching)
96+
- ENVIRONMENT (either `production` or `dev`; determines which KVs are read)

http/env-binding.mjs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const accountId = '424ad63426a1ae47d559873f929eb9fc';
2+
3+
const productionNamespaceId = '2e6feba88a9e4097b6d2209191ed4ae5';
4+
const devNameSpaceID = '17fd725f04984e408d4a70b37c817171';
5+
6+
export default function getEnv() {
7+
return {
8+
...process.env,
9+
DATA_CACHE: {
10+
getWithMetadata: async (kvName, format) => {
11+
console.log('token', process.env.CLOUDFLARE_TOKEN);
12+
const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
13+
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
14+
const response = await fetch(url, {
15+
method: 'GET',
16+
headers: {
17+
'Content-Type': 'application/json',
18+
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
19+
},
20+
});
21+
return {
22+
value: await response.text(),
23+
};
24+
},
25+
},
26+
}
27+
};

http/index.mjs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import express from 'express';
2+
import 'dotenv/config';
3+
4+
import worker from '../index.mjs';
5+
import getEnv from './env-binding.mjs';
6+
7+
const port = process.env.PORT ?? 8787;
8+
9+
const convertIncomingMessageToRequest = (req) => {
10+
var headers = new Headers();
11+
for (var key in req.headers) {
12+
if (req.headers[key]) headers.append(key, req.headers[key]);
13+
}
14+
let body = req.body;
15+
if (typeof body === 'object') {
16+
body = JSON.stringify(body);
17+
}
18+
let request = new Request(new URL(req.url, `http://localhost:${port}`).toString(), {
19+
method: req.method,
20+
body: req.method === 'POST' ? body : null,
21+
headers,
22+
})
23+
return request
24+
};
25+
26+
const app = express();
27+
app.use(express.json({limit: '100mb'}), express.text());
28+
app.all('*', async (req, res, next) => {
29+
const response = await worker.fetch(convertIncomingMessageToRequest(req), getEnv(), {waitUntil: () => {}});
30+
31+
// Convert Response object to JSON
32+
const responseBody = await response.text();
33+
34+
// Reflect headers from Response object
35+
//Object.entries(response.headers.raw()).forEach(([key, value]) => {
36+
response.headers.forEach((value, key) => {
37+
res.setHeader(key, value);
38+
});
39+
40+
// Send the status and JSON body
41+
res.status(response.status).send(responseBody);
42+
});
43+
44+
app.listen(port, () => {
45+
console.log(`HTTP GraphQL server running at http://localhost:${port}`);
46+
});

0 commit comments

Comments
 (0)