Variaveis de Ambiente
Variaveis de Ambiente
Seção intitulada “Variaveis de Ambiente”Acesse variaveis de ambiente para valores de configuração, secrets e configuracoes de runtime.
Variaveis devem ser definidas no Environment System antes de poderem ser acessadas. O sistema controla quais backends de armazenamento (OS, arquivo, memoria) fornecem valores e se variaveis sao somente leitura.
Carregamento
Seção intitulada “Carregamento”local env = require("env")Obtem um valor de variavel de ambiente.
-- Obter string de conexão do bancolocal db_url = env.get("DATABASE_URL")if not db_url then return nil, errors.new("INVALID", "DATABASE_URL not configured")end
-- Obter com fallbacklocal port = env.get("PORT") or "8080"local host = env.get("HOST") or "localhost"
-- Obter secretslocal api_key = env.get("API_SECRET_KEY")local jwt_secret = env.get("JWT_SECRET")
-- Configuraçãolocal log_level = env.get("LOG_LEVEL") or "info"local debug_mode = env.get("DEBUG") == "true"| Parâmetro | Tipo | Descrição |
|---|---|---|
key | string | Nome da variavel |
Retorna: string, error
Retorna nil, error se variavel não existe.
Define uma variavel de ambiente.
-- Definir configuração de runtimeenv.set("APP_MODE", "production")
-- Sobrescrever para testesenv.set("API_URL", "http://localhost:8080")
-- Definir baseado em condicoesif is_development then env.set("LOG_LEVEL", "debug")end| Parâmetro | Tipo | Descrição |
|---|---|---|
key | string | Nome da variavel |
value | string | Valor a definir |
Retorna: boolean, error
get_all
Seção intitulada “get_all”Obtem todas as variaveis de ambiente acessiveis.
local vars = env.get_all()
-- Logar configuração (cuidado para não logar secrets)for key, value in pairs(vars) do if not key:match("SECRET") and not key:match("KEY") then logger.debug("env", {[key] = value}) endend
-- Verificar variaveis obrigatoriaslocal required = {"DATABASE_URL", "REDIS_URL", "API_KEY"}for _, key in ipairs(required) do if not vars[key] then return nil, errors.new("INVALID", "Missing required env var: " .. key) endendRetorna: table, error
Permissões
Seção intitulada “Permissões”Acesso a ambiente está sujeito a avaliação de política de segurança.
Acoes de Segurança
Seção intitulada “Acoes de Segurança”| Ação | Recurso | Descrição |
|---|---|---|
env.get | Nome da variavel | Ler variavel de ambiente |
env.set | Nome da variavel | Escrever variavel de ambiente |
env.get_all | * | Listar todas as variaveis |
Verificando Acesso
Seção intitulada “Verificando Acesso”local security = require("security")
if security.can("env.get", "DATABASE_URL") then local url = env.get("DATABASE_URL")endVeja Security Model para configuração de políticas.
| Condição | Tipo | Retentável |
|---|---|---|
| Chave vazia | errors.INVALID | não |
| Variavel não encontrada | errors.NOT_FOUND | não |
| Permissão negada | errors.PERMISSION_DENIED | não |
Veja Error Handling para trabalhar com erros.
Veja Também
Seção intitulada “Veja Também”- Environment System - Configurar backends de armazenamento e definicoes de variaveis