简体   繁体   English

Nim如何检查环境是否在生产中?

[英]Nim how to check if environment is in production?

Is there any convention for flagging a production env in Nim?在 Nim 中标记生产环境是否有任何约定?
Like process.env.NODE_ENV === 'production' in JavaScript for instance.例如 JavaScript 中的process.env.NODE_ENV === 'production'
Or should I use pragmas like或者我应该使用像

when defined(release):

The when defined(release) line only checks at compile time if you are building a binary for release/non release, which is a compile time symbol you get by compiling your source code with the -d:release switch. when defined(release)行仅在编译时检查您是否正在为发布/非发布构建二进制文件,这是通过使用-d:release开关编译源代码获得的编译时符号 As such, this is not telling you anything about the runtime environment at all, but about the compilation.因此,这根本没有告诉您有关运行时环境的任何信息,而是有关编译的信息。 You could compile both debug/release binaries and use them both in production, maybe because you have a crash and need to replace your release binary with a debug version to get a meaningful stack trace/explanation.您可以编译调试/发布二进制文件并在生产中同时使用它们,这可能是因为您遇到了崩溃并且需要用调试版本替换您的发布二进制文件以获得有意义的堆栈跟踪/解释。

These compile time symbols are usually embedded through the use of an external script/build machinery, like NimScript , Nimble , nake , or just a simple .bat/.sh file.这些编译时符号通常通过使用外部脚本/构建机制嵌入,例如NimScriptNimblenake或只是一个简单的.bat/.sh文件。

A line like process.env.NODE_ENV === 'production' from javascript probably comes from somewhere , be it a static file in your project or something else. process.env.NODE_ENV === 'production' from javascript 之类的行可能来自某个地方,它可能是您项目中的 static 文件或其他文件。 You need to figure out what exactly defines your runtime environment in production and add the Nim code necessary to detect that difference.您需要找出在生产环境中究竟是什么定义了您的运行时环境,并添加必要的 Nim 代码来检测该差异。 For instance, your Nim program could run at startup a GET JSON query against a server you own and parse a isProduction: true value somewhere.例如,您的 Nim 程序可以在启动时针对您拥有的服务器运行 GET JSON 查询,并在某处解析isProduction: true值。 This server could be parametrised in a configuration file where end users specify the environment they are actually running (or just avoid servers and read a local configuration file).该服务器可以在配置文件中进行参数化,最终用户在其中指定他们实际运行的环境(或者只是避开服务器并读取本地配置文件)。

Or if you like the compile time symbols, you could have a shell script that builds your binary N times for as many different environments you want to have through the use of user-defined pragmas (and then you need to be careful about using the appropriate binary where applicable).或者如果你喜欢编译时符号,你可以有一个 shell 脚本,它通过使用 用户定义的 pragma为你想要的许多不同环境构建你的二进制文件 N 次(然后你需要小心使用适当的适用的二进制文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM