Startec

Startec

Como lidar com as variáveis de ambiente usadas em .env mas agora em produção? · samuhmatos

Mai 24, às 12:01

·

2 min de leitura

·

0 leituras

Recentemente, aprendi sobre .env e a possibilidade de usar variaveis definidas durante todo o projeto e é bastante legal! Estou desenolvendo um projeto com php e slim e durante o projeto,...
Como lidar com as variáveis de ambiente usadas em .env mas agora em produção? · samuhmatos

Depende do jeito que a pessoa gosta ou precisa trabalhar.

As mais comuns são:

  1. Ter um arquivo em produção e outro em desenvolvimento.
  2. Ter variáveis diferentes no mesmo arquivo e em código pega as adequadas

O primeiro é mais simples e se você sobe para a produção de forma automatizada ou tem algo que não vai subir o arquivo de dev inadvertidamente é mais interessante, fácil e é ligeiramente mais eficiente.

É só ter nomes diferentes para o arquivo .env. Ainda terá que saber em que ambiente está, mas assim fica mais fácil organizar. E mesmo que tenha os dois arquivos não corre riscos subindo.

O segundo não te dá preocupação de sobrescrever o arquivo de produção, mas tem que por em código algo para descobrir que está em produção e pegar as variáveis diferentes.

Para fazer isso pode verificar se existe algum arquivo que você nunca tem em dev, então não tem risco de subir sem querer, aí se o arquivo existe sabe que está em produção e sabe que variáveis pegar.

Dependendo de como estão os ambientes, pode verificar o nome do servidor ($_ENV['HOSTNAME'] oi $_SERVER["COMPUTERNAME"]) ou outra informação que só existe em produção. Ou pegar uma configuração do php.ini que só tem em produção, o que é normal quando você nem tem acesso a ele. Só cuidado porque o ambiente de produção você pode não ter tanto controlem então a verificaçaõ pode ser mais interessante ser negativa, ou seja, você verifica se está em dev, onde você tem controle.

Sò tem que lembrar de atualizar os dois arquivos ou locais do arquivo se algo tiver que mudar nos dois ambientes. Por isso, o ideal seria ter um arquivo separado só para o conteúdo que realmente muda e outro para o que vale igual, assim trata-se diferente.

Melhor ainda é não precisar fazer isso, mas nem sempre é possível.

Faz sentido para você?

Espero ter ajudado.

Farei algo que muitos pedem para aprender a programar corretamente, gratuitamente. Para saber quando, me segue nas suas plataformas preferidas. Quase não as uso, não terá infindas notificações (links aqui).


Continue lendo

DEV

Authentication system using Golang and Sveltekit - Dockerization and deployments
Introduction Having built out all the features of our application, preparing it for deployment is the next step so that everyone around the world will easily access it. We will deploy our apps (backend and...

Hoje, às 19:52

DEV

LEARN API AND ITS MOST POPULAR TYPE
An API (Application Programming Interface) is a set of rules and protocols that allows different software applications to communicate and interact with each other. It defines the methods, data structures, and...

Hoje, às 19:26

AI | Techcrunch

Investors take note: Wildfire smoke will spark a surge in East Coast climate tech startups
As smoke from Canadian wildfires has enveloped large swathes of the East Coast, millions of people have found themselves trapped inside, gazing out on orange skies and hazy cityscapes. The air quality index —...

Hoje, às 18:08

DEV

A Plain English Guide to Reverse-Engineering the Twitter Algorithm with LangChain, Activeloop, and DeepInfra
Imagine writing a piece of software that could understand, assist, and even generate code, similar to how a seasoned developer would. Well, that’s possible with LangChain. Leveraging advanced models such as...

Hoje, às 18:08

DEV

Finding Harmony in Marketing and UX
When we think of teamwork in the world of user experience (UX), we often imagine design and engineering working together. However, the idea of design and marketing working together is not as common. While...

Hoje, às 17:02

DEV

💡 Where to Find Inspiration for Building Your Next App
The first steps before turning your ideas into code. Whenever I’m trying to think of an idea to build a new application or website and I get stumped on what to do, there’s one phrase that always comes to...

Hoje, às 16:58

DEV

How to create 700+ SEO optimised pages for website in 1 h using Next.JS, OpenAI, Postgres
Small intro, I started learning coding couple of months before and since then experimenting with different small side projects. So this I show coding still looks for me:) What did I build this...

Hoje, às 16:37

DEV

Angular Project Mongodb database Connect | Angular Website Project | Angular App
Angular Project Mongodb database Connect | Angular Website Project | Angular App - YouTube ​ @softwaretechit Download Our App:- https://blog.softwaretechit.com/p/download.htmlWhat will we Learn In This...

Hoje, às 16:10

AI | Techcrunch

Meta warned it faces 'heavy sanctions' in EU if it fails to fix child protection issues on Instagram
The European Union has fired a blunt warning at Meta, saying it must quickly clean up its act on child protection or face the risk of “heavy sanctions”. The warning follows a report by the Wall Street...

Hoje, às 16:03

DEV

Taking Control with PostgreSQL Functions: Closing the Gap to ORM Functionality
Unveiling the Disparity: Understanding the Divide Between Direct Driver and ORM Functionality When it comes to choosing the technologies for developing a backend and manipulating data in a database like...

Hoje, às 16:02