Skip to content

Commit

Permalink
Merge pull request #305 from the-hideout/http-server
Browse files Browse the repository at this point in the history
Add HTTP Server
  • Loading branch information
Razzmatazzz authored Jun 19, 2024
2 parents 80475b4 + 598c72e commit 6514957
Show file tree
Hide file tree
Showing 8 changed files with 1,314 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,12 @@ wrangler publish
```

> We don't do this often and generally use GitHub actions to do all of our deployments for us
## HTTP Server

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):

- CLOUDFLARE_TOKEN (token must have permissions to read the KVs the API uses)
- CACHE_BASIC_AUTH (used for caching)
- ENVIRONMENT (either `production` or `dev`; determines which KVs are read)
- PORT (defaults to 8088)
28 changes: 28 additions & 0 deletions http/env-binding.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// this module provides a way for the http server to access the cloudflare KVs
// using the same method as in the worker context
const accountId = '424ad63426a1ae47d559873f929eb9fc';

const productionNamespaceId = '2e6feba88a9e4097b6d2209191ed4ae5';
const devNameSpaceID = '17fd725f04984e408d4a70b37c817171';

export default function getEnv() {
return {
...process.env,
DATA_CACHE: {
getWithMetadata: async (kvName, format) => {
const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
},
});
return {
value: await response.text(),
};
},
},
}
};
46 changes: 46 additions & 0 deletions http/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import express from 'express';
import 'dotenv/config';

import worker from '../index.mjs';
import getEnv from './env-binding.mjs';

const port = process.env.PORT ?? 8788;

const convertIncomingMessageToRequest = (req) => {
var headers = new Headers();
for (var key in req.headers) {
if (req.headers[key]) headers.append(key, req.headers[key]);
}
let body = req.body;
if (typeof body === 'object') {
body = JSON.stringify(body);
}
let request = new Request(new URL(req.url, `http://127.0.0.1:${port}`).toString(), {
method: req.method,
body: req.method === 'POST' ? body : null,
headers,
})
return request
};

const app = express();
app.use(express.json({limit: '100mb'}), express.text());
app.all('*', async (req, res, next) => {
const response = await worker.fetch(convertIncomingMessageToRequest(req), getEnv(), {waitUntil: () => {}});

// Convert Response object to JSON
const responseBody = await response.text();

// Reflect headers from Response object
//Object.entries(response.headers.raw()).forEach(([key, value]) => {
response.headers.forEach((value, key) => {
res.setHeader(key, value);
});

// Send the status and JSON body
res.status(response.status).send(responseBody);
});

app.listen(port, () => {
console.log(`HTTP GraphQL server running at http://127.0.0.1:${port}`);
});
Loading

0 comments on commit 6514957

Please sign in to comment.