-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from the-hideout/http-server
Add HTTP Server
- Loading branch information
Showing
8 changed files
with
1,314 additions
and
8 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
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,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(), | ||
}; | ||
}, | ||
}, | ||
} | ||
}; |
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,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}`); | ||
}); |
Oops, something went wrong.