How to detect in Deno if it is currently a `prod` deployment?
What problem are you trying to solve?
I need to configure logging for my app and I need to differentiate whether a message originated from a prod environment or from one of the preview environments.
Describe the solution you'd like
Something akin to checking an ENV var that updates as the status of the Deno isolate/instance changes
Describe alternatives you've considered
Tracing the URL of requests that generate messages as a proxy for this info
Documentation, Adoption, Migration Strategy
No response
Currently there is no smart way to determine if a particular deployment is run as a prod deployment.
One idea I've come up with is looking at host header. Let's say the project name is magurotuna-prod-host, and you deploy the following code:
Deno.serve((req) => {
const host = req.headers.get("host");
const isProd = host === "magurotuna-prod-host.deno.dev";
return Response.json({ isProd });
});
Then, if you make a request to the prod deployment via https://magurotuna-prod-host.deno.dev, you'll see isProd to be true. In contrast, if you do to a preview deployment via, say, https://magurotuna-prod-host-abc1234defg.deno.dev, isProd should be false.
Ah cool, thanks!
There is another way, which is useful if you are trying to determine the environment outside of an HTTP Request going through the middleware (or if it might be too much trouble trickling down the origin value).
- Make an API endpoint that returns the
Deno.env.get('DENO_DEPLOYMENT_ID')[1] - From your code, make a
fetchrequest to this endpoint that you just made, using the ORIGIN domain in which you expect yourproddeployment to residefetch('https://myRealProdURL') - Compare the result of the API with the
Deno.env.get('DENO_DEPLOYMENT_ID'). If they are equal that means the current instance is theproddeployment.
Kinda like asking yourself your own name, and waiting to see if it is the one you remember.
Actually you will get an error if you try to request your own API...
This is such a hack but it works... silly.
export async function isProd() {
const url = `${DenoDeploy_ORIGIN}`;
const response = await fetch(url);
if (!response.ok) {
if (response.status === 508) {
// Deno Deploy does not allow the server to send a request to itself... -_-
// https://github.com/denoland/deploy_feedback/issues/243
return true;
}
throw new Error(`isProd() HTTP error! Status: ${response.status}`);
}
return false
}
- Make an API endpoint that returns the
Deno.env.get('DENO_DEPLOYMENT_ID')[1]
It will cause
Env vars cannot begin with DENO_
error