Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add page for environment variables #29

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/components/Sidebar/sidebarElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ export const sidebarElements: Array<SidebarElement> = [
},
],
},
{
type: 'element',
label: 'Environment Variables',
href: '/documentation/environment-variables',
},
{ type: 'divider' },
{ type: 'title', label: 'API reference' },
{
Expand Down
74 changes: 74 additions & 0 deletions src/routes/documentation/environment-variables.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import MetaTags from '@/components/MetaTags'

<MetaTags
title="Tuono - Environment Variables"
canonical="https://tuono.dev/documentation/environment-variables"
/>

import Breadcrumbs from '@/components/Breadcrumbs'

<Breadcrumbs breadcrumbs={[{ label: 'Environment Variables' }]} />

# Environment Variables

## Overview

Tuono supports environment variables on both client and server, providing hot reload in both scenarios.

- Loads from `.env.*` files
- Manages precedence across multiple env files
- Supports hot reload on both client and server
- Bundles client env vars with the `TUONO_PUBLIC_` prefix

## Usage

Define your environment variables in `.env` file(s). Here we will use a `.env` and a `.env.local`.

```
# .env
foo
```

```
# .env.local

```

Here, variables set in `.env.local` will override variables set in `.env`. We cover this in more detail in the [precedence section](#precedence).

### Clientside



### Serverside

Environment variables are automatically loaded into the rust environment, allowing you to use them just like you would use a system environment variable in any other rust codebase.

```rs
use std::env;
use tuono_lib::Request;
use tuono_lib::axum::http::StatusCode;

#[tuono_lib::api(GET)]
pub async fn health_check(_req: Request) -> StatusCode {
println!("{}", env::var("DATABASE_URL").expect("DATABASE_URL not set"));
StatusCode::OK
}
```

## File Types and Precedence

System environment variables take priority, and are never overridden. After that, precedence works in order of specificity, with the most specific env file taking priority.

```text
.env # loaded in all cases
.env.local # loaded in all cases, ignored by git
.env.[mode] # only loaded in specified mode
.env.[mode].local # only loaded in specified mode, ignored by git
```


## Caveats, Gotchas and Notes

- Secrets should only be stored in `*.local` files, as those are the only files ignored by git.
- When you create a new env file, it is not automatically loaded. You will have to restart the server to achieve this.
Loading