github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/compute/testdata/build/assemblyscript/assembly/index.ts (about)

     1  import { Request,  Response, Fastly } from "@fastly/as-compute";
     2  
     3  // The name of a backend server associated with this service.
     4  //
     5  // This should be changed to match the name of your own backend. See the the
     6  // `Hosts` section of the Fastly Wasm service UI for more information.
     7  const BACKEND_NAME = "backend_name";
     8  
     9  /// The name of a second backend associated with this service.
    10  const OTHER_BACKEND_NAME = "other_backend_name";
    11  
    12  // The entry point for your application.
    13  //
    14  // Use this function to define your main request handling logic. It could be
    15  // used to route based on the request properties (such as method or path), send
    16  // the request to a backend, make completely new requests, and/or generate
    17  // synthetic responses.
    18  function main(req: Request): Response {
    19      // Make any desired changes to the client request.
    20      req.headers().set("Host", "example.com");
    21  
    22      // We can filter requests that have unexpected methods.
    23      const VALID_METHODS = ["HEAD", "GET", "POST"];
    24      if (!VALID_METHODS.includes(req.method())) {
    25          return new Response(String.UTF8.encode("This method is not allowed"), {
    26              status: 405,
    27          });
    28      }
    29  
    30      let method = req.method();
    31      let urlParts = req.url().split("//").pop().split("/");
    32      let host = urlParts.shift();
    33      let path = "/" + urlParts.join("/");
    34  
    35      // If request is a `GET` to the `/` path, send a default response.
    36      if (method == "GET" && path == "/") {
    37          return new Response(String.UTF8.encode("Welcome to Fastly Compute!"), {
    38            status: 200,
    39          });
    40      }
    41  
    42      // If request is a `GET` to the `/backend` path, send to a named backend.
    43      if (method == "GET" && path == "/backend") {
    44          // Request handling logic could go here...
    45          // E.g., send the request to an origin backend and then cache the
    46          // response for one minute.
    47          let cacheOverride = new Fastly.CacheOverride();
    48          cacheOverride.setTTL(60);
    49          return Fastly.fetch(req, {
    50              backend: BACKEND_NAME,
    51              cacheOverride,
    52          }).wait();
    53      }
    54  
    55      // If request is a `GET` to a path starting with `/other/`.
    56      if (method == "GET" && path.startsWith("/other/")) {
    57          // Send request to a different backend and don't cache response.
    58          let cacheOverride = new Fastly.CacheOverride();
    59          cacheOverride.setPass();
    60          return Fastly.fetch(req, {
    61              backend: OTHER_BACKEND_NAME,
    62              cacheOverride,
    63          }).wait();
    64      }
    65  
    66      // Catch all other requests and return a 404.
    67      return new Response(String.UTF8.encode("The page you requested could not be found"), {
    68          status: 200,
    69      });
    70  }
    71  
    72  // Get the request from the client.
    73  let req = Fastly.getClientRequest();
    74  
    75  // Pass the request to the main request handler function.
    76  let resp = main(req);
    77  
    78  // Send the response back to the client.
    79  Fastly.respondWith(resp);