Environment Variables
Ecewo has built-in env
parser, which is dotenv-c. Let’s see how it works.
First, create a .env
file in the root directory of your project. Project structure should be like this:
├── ecewo/├── .env└── src/ ├── main.c └── CMakeLists.txt
We define a PORT
in .env
:
PORT=4000
Let’s parse it in main.c
:
#include "ecewo.h"#include "dotenv.h"
int main(){ env_load(ENV, false); // Load ".env" file const char *port = getenv("PORT"); // Get the "PORT" printf("PORT: %s\n", port); // Print the "PORT"
ecewo(4000); // Start the server return 0; // Exit}
We’ll see the PORT
in the console when building the program. However, getenv
returns a const char *
everytime, and if we need to retrieve a non-character type variable, we must convert it first.
For example, ecewo()
takes an unsigned short
type variable. So, if we want to use the PORT
from the .env
file, we can do it like this:
#include "ecewo.h"#include "dotenv.h"
int main(){ env_load(ENV, false);
const char *port = getenv("PORT"); const unsigned short PORT = (unsigned short)atoi(port);
ecewo(PORT); return 0;}