#1334 | 2024-03-20 13:10:46

成功用workerd把gdshare在vps上跑起来了

docker pull dcard/workerd

docker run -d -p 8081:8081 -v .:/app dcard/workerd serve /app/workerd.capnp

第二条命令执行的时候要确保当前目录有两个文件,一个是workerd.capnp:

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

const helloWorldExample :Workerd.Config = (
  services = [ (name = "main", worker = .helloWorld) ],
    sockets = [ ( name = "http", address = "*:8081", http = (), service = "main" ) ]
);

const helloWorld :Workerd.Worker = (
  serviceWorkerScript = embed "worker.js",
  compatibilityDate = "2023-02-28",
);

另一个是worker.js

addEventListener('fetch', event => {
  event.respondWith(handle(event.request));
});

async function handle(request) {
  return new Response("Hello World\n");
}

(实际上我是用了gdshare

其实不一定要用docker,我本地迷你主机就成功用官方发布的二进制的workerd跑起来了,但是我的vps ubuntu版本比较老,直接跑二进制会报错……

具体命令则是 ./workerd serve workerd.capnp