Compute
Runs your code and answers requests at the app's own web address.
Compute is where an app's code runs. Add it once per app; it answers at the app's own address. Until you deploy, a starter page answers.
Good for: websites, APIs, webhooks. Billed: per request and processing time. Nothing while idle.
Your code
Your code is a JavaScript module whose default export has a fetch function. It gets each request, and the app's other services and variables in env:
export default {
async fetch(request, env) {
const url = new URL(request.url)
if (url.pathname === '/orders') {
const { results } = await env.DB.prepare('select * from orders').all()
return Response.json(results)
}
return new Response(`Welcome to ${env.SHOP_NAME}`)
},
}You can upload several files: the main module and any it imports (.js, .mjs, .cjs, .wasm, plus text and data files). Up to 100 files and 10 MB per deployment. If you use a bundler, upload what it builds.
Websites can also bring their pages, styles, scripts and images as static files, served at their own paths before your code runs (your code can fetch them as env.ASSETS). A site with no code at all works too: upload the folder your build makes, like dist/. Up to 20,000 files, 25 MB each. Settings for single-page apps and 404 pages are in the API reference.
Deploy
curl https://opencloudport.com/api/v1/apps/$APP_ID/deployments \
-H "Authorization: Bearer $OCP_KEY" \
-F 'metadata={"main_module": "index.js"}' \
-F 'index.js=@dist/index.js'The call answers once the new code is live. Every deployment is kept, so going back is one click: Roll back on the Deployments tab, POST …/deployments/{id}/redeploy, or the rollback tool. Details are in the API reference.
On a schedule
Your code can run on a timetable too: every day at 9:00, every 15 minutes, on weekdays. Add up to 5 schedules under Schedules; each calls your code's scheduled() handler. See Scheduled runs.
Settings and secrets
Variables are how your code gets settings and API keys without putting them in the code. Each one is env.NAME.
- Secret (the default): the value can be set and replaced, but never shown again.
- Plain: the value is shown in the dashboard and the API.
Set them on Compute's Variables tab, with PUT /api/v1/apps/{id}/env/{NAME}, or the set_variable tool. They apply at once, without a new deployment. An app can have up to 100.
Removing Compute removes the app's deployments and all its variables.
opencloudport