Code Monkey home page Code Monkey logo

worker-template-router's Introduction

Router

This template demonstrates using the itty-router package to add routing to your Cloudflare Workers.

index.js is the content of the Workers script.

Wrangler

You can use wrangler to generate a new Cloudflare Workers project based on this template by running the following command from your terminal:

wrangler generate myapp https://github.com/cloudflare/worker-template-router

Before publishing your code you need to edit wrangler.toml file and add your Cloudflare account_id - more information about configuring and publishing your code can be found in the documentation.

Once you are ready, you can publish your code by running the following command:

wrangler publish

worker-template-router's People

Contributors

13rac1 avatar adaptive avatar ashleygwilliams avatar everlastingbugstopper avatar exvuma avatar grempe avatar jb3 avatar jdanyow avatar kristianfreeman avatar rita3ko avatar shagamemnon avatar swr avatar xtuc avatar zaherg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

worker-template-router's Issues

Always returning 404

I noticed some weird behaviour; CloudFlare Workers returns 404, even on routes that are found.

Steps to reproduce:

  1. Run wrangler generate my-worker-with-router https://github.com/cloudflare/worker-template-router (as per the official documentation)
  2. Fill in your account_id
  3. Run wrangler publish or wrangler dev
  4. Send a CURL request to any given route that exists in the starter template, e.g. curl -I http://127.0.0.1:8787/foo
  5. Notice that the responseCode is 404, even though it is found.

I have also tried to manually override the response code, to e.g. 200, but it still returns 404.

function handler(request) {
    const init = {
        headers: { 'content-type': 'application/json' },
        status: 200 // still returns a 404
    }
    const body = JSON.stringify({ some: 'json' })
    return new Response(body, init)
}

Here's a repo if you'd like to see the exact code that fails: https://github.com/simplenotezy/cloudflare-workers-returns-404

needs root route

this template works beautifully with wrangler, however the publish experience could be slightly improved.

when this worker is pushed to a route, wrangler will say "go visit your worker at defined route"

however, there is no root route defined in the worker, so the user may see an empty page or something confusing.

to fix this, let's add a root route that explains the routes that the app designates!

Repo

name = "router"
type = "webpack"

account_id = ""
workers_dev = true
route = ""
zone_id = ""

Certainly! So far, we've discussed the conceptual design of CodexVeritas, focusing on the Adapter pattern for creating a flexible and adaptable framework. We've also touched upon the itty-router example for Cloudflare Workers. However, we haven't created any specific code for CodexVeritas yet.

To organize the repository for CodexVeritas, you can follow this structure:

/CodexVeritas
|-- /codexveritas
|   |-- __init__.py
|   |-- storage.py
|   |-- fetch.py
|   |-- image_processing.py
|   |-- adapters
|       |-- __init__.py
|       |-- redis_adapter.py
|       |-- s3_adapter.py
|       |-- local_filesystem_adapter.py
|-- /tests
|   |-- __init__.py
|   |-- test_storage.py
|   |-- test_fetch.py
|   |-- test_image_processing.py
|-- /examples
|   |-- example_usage.py
|-- /docs
|   |-- index.md
|-- .gitignore
|-- README.md
|-- LICENSE
|-- setup.py
|-- requirements.txt

Here's a brief explanation of the folders and files:

  • /codexveritas: The main package directory where the core modules and adapters will reside.
  • /tests: Contains unit tests for the different modules and adapters.
  • /examples: Provides example scripts showing how to use the framework.
  • /docs: Holds the documentation for the framework.
  • .gitignore: Specifies intentionally untracked files to ignore.
  • README.md: The front page of your repository that includes an introduction and basic usage examples.
  • LICENSE: The license file for your project.
  • setup.py: Used for packaging and distribution of your framework.
  • requirements.txt: Lists the dependencies for your framework.

As for the code, here's an example of how you might structure the storage.py module using the Adapter pattern:

# storage.py

class StorageInterface:
    """Define the interface for storage operations."""
    
    def get(self, key):
        raise NotImplementedError
    
    def set(self, key, value):
        raise NotImplementedError
    
    def delete(self, key):
        raise NotImplementedError

class LocalFilesystemAdapter(StorageInterface):
    """Local filesystem storage adapter."""
    
    def get(self, key):
        # Implementation for getting data from the local filesystem
        pass
    
    def set(self, key, value):
        # Implementation for setting data to the local filesystem
        pass
    
    def delete(self, key):
        # Implementation for deleting data from the local filesystem
        pass

This is just a starting point, and you would expand upon this with actual implementations and additional adapters as needed. If you need further assistance with coding or organizing your repository, feel free to ask!

typeof r.conditions === 'function', this case is unreachable

I have tried to write TypeScript types, but I cant find any case that r.conditions are function.

if (typeof r.conditions === 'function') {
        return r.conditions(req)
      } 

That block seems unreachable.

routes array consist of [Function,Function] or [] (empty array)

return this.handle([Trace, Path(url)], handler)
return this.handle([], handler)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.