File
Blob: samples/static-files-from-disk/config.capnp
| 1 | # This is a simple demo showing how to serve a static web site from disk. |
| 2 | # |
| 3 | # By default this will serve a subdirectory called `content-dir` from whatever directory `workerd` |
| 4 | # runs in, but you can override the directory on the command-line like so: |
| 5 | # |
| 6 | # workerd serve config.capnp --directory-path site-files=/path/to/files |
| 7 | |
| 8 | using Workerd = import "/workerd/workerd.capnp"; |
| 9 | |
| 10 | # A constant of type `Workerd.Config` will be recognized as the top-level configuration. |
| 11 | const config :Workerd.Config = ( |
| 12 | services = [ |
| 13 | # The site worker contains JavaScript logic to serve static files from a directory. The logic |
| 14 | # includes things like setting the right content-type (based on file name), defaulting to |
| 15 | # `index.html`, and so on. |
| 16 | (name = "site-worker", worker = .siteWorker), |
| 17 | |
| 18 | # Underneath the site worker we have a second service which provides direct access to files on |
| 19 | # disk. We only configure site-worker to be able to access this (via a binding, below), so it |
| 20 | # won't be served publicly as-is. (Note that disk access is read-only by default, but there is |
| 21 | # a `writable` option which enables PUT requests.) |
| 22 | (name = "site-files", disk = "content-dir"), |
| 23 | ], |
| 24 | |
| 25 | # We export it via HTTP on port 8080. |
| 26 | sockets = [ ( name = "http", address = "*:8080", http = (), service = "site-worker" ) ], |
| 27 | ); |
| 28 | |
| 29 | # For legibility we define the Worker's config as a separate constant. |
| 30 | const siteWorker :Workerd.Worker = ( |
| 31 | # All Workers must declare a compatibility date, which ensures that if `workerd` is updated to |
| 32 | # a newer version with breaking changes, it will emulate the API as it existed on this date, so |
| 33 | # the Worker won't break. |
| 34 | compatibilityDate = "2023-02-28", |
| 35 | |
| 36 | # This worker is modules-based. |
| 37 | modules = [ |
| 38 | (name = "static.js", esModule = embed "static.js"), |
| 39 | ], |
| 40 | |
| 41 | bindings = [ |
| 42 | # Give this worker permission to request files on disk, via the "site-files" service we |
| 43 | # defined earlier. |
| 44 | (name = "files", service = "site-files"), |
| 45 | |
| 46 | # This worker supports some configuration options via a JSON binding. Here we set the option |
| 47 | # so that we hide the `.html` extension from URLs. (See the code for all config options.) |
| 48 | (name = "config", json = "{\"hideHtmlExtension\": true}") |
| 49 | ], |
| 50 | ); |
| 51 |