Skip to content

Allow "fetch" to use IP address instead of a domain #93

Closed

Description

@uneknown

Is there an option to allow using an IP instead of a full domain when using fetch()?

Edit: I found the option, but I cannot get it to work.


Config:

using Workerd = import "/workerd/workerd.capnp";

# A constant of type `Workerd.Config` will be recognized as the top-level configuration.
const config :Workerd.Config = (
  services = [
    # The site worker contains JavaScript logic to serve static files from a directory. The logic
    # includes things like setting the right content-type (based on file name), defaulting to
    # `index.html`, and so on.
    (name = "site-worker", worker = .siteWorker),
    
    # Underneath the site worker we have a second service which provides direct access to files on
    # disk. We only configure site-worker to be able to access this (via a binding, below), so it
    # won't be served publicly as-is. (Note that disk access is read-only by default, but there is
    # a `writable` option which enables PUT requests.)
    (name = "site-files", disk = "content-dir"),
	
    # Fetch
    # (name = "internal-network", external = ( address = "127.0.0.1:7379" ) ), # First try
    (name = "internal-network", network = ( allow = ["public", "private"], ) ), # Second try

    #KV
    (name = "kv", disk = ( path = "kv", writable = true, allowDotfiles = false ) )
  ],

  # We export it via HTTP on port 8080.
  sockets = [ ( name = "http", address = "*:8080", http = (), service = "site-worker" ) ],
);

# For legibility we define the Worker's config as a separate constant.
const siteWorker :Workerd.Worker = (
  # All Workers must declare a compatibility date, which ensures that if `workerd` is updated to
  # a newer version with breaking changes, it will emulate the API as it existed on this date, so
  # the Worker won't break.
  compatibilityDate = "2022-09-16",

  # This worker is modules-based.
  modules = [
    (name = "hello.js", esModule = embed "hello.js"),
  ],

  bindings = [
    # Give this worker permission to request files on disk, via the "site-files" service we
    # defined earlier.
    ( name = "files", service = "site-files"),

    # Fetch
    ( name ="internalNetwork", service = "internal-network" ),    

    # KV
    ( name = "KV", kvNamespace = ( name = "kv" ) ),

    # This worker supports some configuration options via a JSON binding. Here we set the option
    # so that we hide the `.html` extension from URLs. (See the code for all config options.)
    (name = "config", json = "{\"hideHtmlExtension\": true}")
  ],
);

This is how I call the fetch function in the code: env.internalNetwork.fetch("http://127.0.0.1:7379/endpoint")

Initially, I used the ExternalServer option. I then tried using the Network option. Neither worked.

Activity

  1. changed the title [-]Allow "fetch" to use IP address[/-] [+]Allow "fetch" to use IP address instead of a domain[/+] on Oct 11, 2022
  2. OnurGvnc commented on Oct 12, 2022

    @OnurGvnc

    I didn't test it. Can you try it like this

    using Workerd = import "/workerd/workerd.capnp";
    
    const config :Workerd.Config = (
      services = [
        (name = "site-worker", worker = .siteWorker),
        (name = "internal-api", external = ( address = "127.0.0.1:7379") ),
      ],
      sockets = [ ( name = "http", address = "*:8080", http = (), service = "site-worker" ) ],
    );
    
    const siteWorker :Workerd.Worker = (
      compatibilityDate = "2022-09-16",
      modules = [
        (name = "hello.js", esModule = embed "hello.js"),
      ],
      bindings = [
        ( name ="API", service = "internal-api" ),
      ],
    );

    env.API.fetch("http://api/endpoint")

  3. kentonv commented on Oct 13, 2022

    @kentonv
    Member

    Sounds like you may have been hitting the bug described in #62?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions