Skip to content

Commit

Permalink
updated README.md (fixed tutorial), added more logging for config ini…
Browse files Browse the repository at this point in the history
…tialization and middlewares (#1)
  • Loading branch information
NikitosnikN authored Sep 10, 2023
1 parent 76252f6 commit 4a349dd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ yarn run wrangler kv:namespace create CONFIG_KV --preview
yarn run wrangler kv:namespace create CONFIG_KV
```

add config from console output into `wrangler.toml` file, example:

* dev:
```toml
kv_namespaces = [
{ binding = "CONFIG_KV", preview_id = "PREVIEW_ID" }
]
```

* prod:
```toml
kv_namespaces = [
{ binding = "CONFIG_KV", id = "ID" },
]
```

2. Prepare node list data with your list of endpoints

Example and structure could be found in [node-config-example.json](node-config-example.json)
Expand All @@ -62,7 +78,7 @@ Tips:
* go to Cloudflare UI -> Workers KV page
* select your created namespace
* create new item(entry) into namespace
* key - `origin`
* key - `origins`
* value - valid JSON as array, example could be found in [node-config-example.json](node-config-example.json).

### Develop
Expand Down
21 changes: 19 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ export default {
env: Env,
ctx: ExecutionContext
): Promise<Response> {
let config: Config = await loadConfig(env);
let config: Config;

try {
config = await loadConfig(env);
} catch (e) {
console.log("Failed to initialize config", e);
return getInternalServerErrorResponse();
}

if (!config || !config.origins || config.origins.length === 0) {
console.log("Config is empty");
return getInternalServerErrorResponse();
}

const requestContext: RequestContext = {
request,
Expand All @@ -34,7 +46,12 @@ export default {

for (middleware of middlewares) {
if (middleware.shouldBeProcessed(requestContext)) {
await middleware.process(requestContext);
try {
await middleware.process(requestContext);
} catch (e) {
console.log(`Failed to process ${middleware.slug} middleware`, e);
break;
}
}
}

Expand Down

0 comments on commit 4a349dd

Please sign in to comment.