Skip to content

.env file support#612

Merged
Casimir255 merged 2 commits intoTorchAPI:masterfrom
HnZGaming:feat/dotnetenv
Oct 8, 2025
Merged

.env file support#612
Casimir255 merged 2 commits intoTorchAPI:masterfrom
HnZGaming:feat/dotnetenv

Conversation

@ryo0ka
Copy link
Copy Markdown
Contributor

@ryo0ka ryo0ka commented Oct 7, 2025

Enhancement

  • Added DotNetEnv to load environment variables from .env file.

Context

  • Goal is to help plugin devs/users isolate credentials from config files.
  • If you haven't seen .env files before, please shoot me a DM and I will explain further.

I'm maintaining a live Torch project which you've added to Torch discord:
image

That project is public so I can't push config files like below:

Files Credentials
FoogsRewards.cfg space-engineers.com API key
SEDiscordBridge.cfg Discord bot token
TorchInfluxDbPlugin.cfg API key
TorchMonitorPlugin.cfg API key

They're currently .gitignored like this:

Instance/FoogsRewards.cfg
Instance/SEDiscordBridge.cfg
Instance/TorchInfluxDbPlugin.cfg
Instance/TorchMonitorPlugin.cfg

but this is taking away the entirity of these files from the project's users.

Naturally, I would send PR to these plugins to read from env vars (if the credential text was empty in their config file). I've already sent one to SEDB for example. That allows me to publish these config files without exposing my own credentials.

Then there's a new problem: if multiple Torch instances had to run in the same host server, those credentials would potentially collide with each other. For example, each instance may wish to use different Discord tokens,

.env file would help mitigate that problem. In SEDB's case, user could create an .env file in the working directory as follows:

# .env
DISCORD_TOKEN=foobar

This PR makes Torch load this file and overwrite (or load new) DISCORD_TOKEN environment variable foobar, so that SEDB plugin could pick up and use that token, even if the host system/user env vars aren't set.

.env file can be .gitignored, so the token won't go public.

From plugins' perspective, these variables are accessible as regular environment variables like Environment.GetEnvironmentVariable(key). So the plugin devs don't have to know where the variables are coming from.

Implementation Details

  • Added DotNetEnv which helps read the .env file from the working directory (i.e. same folder as Torch.Server.exe).
  • This library has no dependency.

Tests

  • Tested on the live server with SEDB.
@ryo0ka
Copy link
Copy Markdown
Contributor Author

ryo0ka commented Oct 8, 2025

@Casimir255 embedded the code, also added friendly log with mask:

12:31:10.1386 [INFO]   SteamCMD: -- type 'quit' to exit --
12:31:12.8919 [INFO]   SteamCMD: Loading Steam API...OK
12:31:13.1283 [INFO]   Initializer: Loading config Torch.cfg
12:31:13.1751 [INFO]   Torch.Utils.DotEnv: DISCORD_TOKEN = ********************************************************************** (masked)
12:31:17.7787 [INFO]   Torch.Patches.MessageSizeLimitPatch: Patched MessageSizeLimit
12:31:17.7797 [INFO]   Torch.Managers.PatchManager.PatchManager: Patching begins...
12:31:17.8397 [INFO]   Torch.Managers.PatchManager.PatchManager: Patched 2/13.  (15%)
@Casimir255 Casimir255 merged commit 5f3118f into TorchAPI:master Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants