Cloudflare Workers - Matrix

From Irregularpedia
Revision as of 05:34, 7 September 2024 by Maintenance script (talk | contribs) (Initial)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Return to server-guides Return to matrix-server-guide

Source:https://appelman.se/matrix-on-cloudflare/ Cloudflare Workers are a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.

Here’s a step-by-step guide:

  1. Log in to your Cloudflare account.

    Navigate to the account and domain where you want to create the worker.

  2. Navigate to the Workers tab.

    This is located on the top menu. Clicking it will bring you to the Workers’ dashboard.

  3. Click on the “Create a Worker” button.

    This will open a new page with a script template for your worker.

  4. Replace the default script with your script.

    In the editor, delete the existing script and paste your script from my previous message.

    Remember to replace "https://matrix.irregulars.io,%22https://vector.im,” and "matrix-fed.irregulars.io:8443" with your actual homeserver URL, identity server URL, and federation server, respectively.

  5. Give your worker a name.

    The name is used to identify your worker, and it’s also used in the worker’s URL.

  6. Click “Save and Deploy”.

    Your worker is now deployed and live. You can test it directly in the Workers dashboard by requesting your worker’s URL.

Now, your worker is ready. In the next step (which is the Step 2 from my previous message), you’ll need to set up routes to your worker so that the requests to /.well-known/matrix/client and /.well-known/matrix/server on your domain are handled by this worker.

<syntaxhighlight lang="javascript">const HOMESERVER_URL = "https://matrix.example.com"; const IDENTITY_SERVER_URL = "https://vector.im"; const FEDERATION_SERVER = "matrix-fed.example.com:8443";

export default {

 async fetch(request, env) {
   const path = new URL(request.url).pathname;
   switch (path) {
     case "/.well-known/matrix/client":
       return new Response(
         {"m.homeserver": {"base_url": "${HOMESERVER_URL}"},"m.identity_server": {"base_url": "${IDENTITY_SERVER_URL}"}}
       );
     case "/.well-known/matrix/server":
       return new Response({"m.server": "${FEDERATION_SERVER}"});
     default:
       return new Response("Invalid request");
   }
 },

};</syntaxhighlight>