Configuration
App configuration in Entropy is stored in two main files: src/main.ts
and .env
.
Environment Configuration
A default Entropy project contains a file called .env
. This file is the place where things like database credentials and environment-specific settings should be stored. Entropy automatically reads all environment variables. The default .env
file contains following variables:
.env
CACHE_MAX_AGE=0 # days COOKIE_MAX_AGE=30 # days DATABASE_URL=mysql://root:@localhost/entropy ENCRYPTION_KEY= HOST=localhost PORT=5050 SESSION_LIFETIME=30 # days
Reading Environment Data
You can read environment variables using env
generic function:
src/main.ts
import { env } from '@entropy/configurator'; const port = env<number>('PORT'); // 5050 const host = env<string | null>('HOST'); // 'localhost' or null
Example .env
File
Developers often use version control systems like Git to work in teams. You should remember not to store .env
files in repositories for security reasons (they contain database passwords etc.). That's why we should exclude these files from version control and only publish an example .env.example
file synced with the original one.
App Configuration
Any other, app-specific configuration is passed into the createServer
function in src/main.ts
file. The configuration looks like this:
src/main.ts
import { createServer } from '@entropy/server'; import { RootModule } from './root.module.ts'; if (import.meta.main) { const server = await createServer({ config: { contentSecurityPolicy: { allowedOrigins: [ 'https://fonts.googleapis.com', ], }, envFile: '.env', locales: { default: 'en', }, }, modules: [RootModule], }); await server.start(); }
To customize app settings, pass configuration options through the config
field. Available options are listed below.
Cache
To enable caching and to set cache lifetime, use the cache.enabled
and cache.maxAge
options.
src/main.ts
const server = await createServer({ config: { cache: { enabled: true, maxAge: 20, // default is set in CACHE_MAX_AGE option in .env file }, }, // ... });
Content Security Policy
To control Content Security Policy configuration, use following options:
src/main.ts
const server = await createServer({ config: { contentSecurityPolicy: { allowInlineScripts: false, allowInlineStyles: true, allowedOrigins: [ 'https://fonts.googleapis.com', ], }, }, // ... });
Cookies
Managing cookies is simple. You can leverage the cookies.maxAge
option to set cookie expiry time.
src/main.ts
const server = await createServer({ config: { cookies: { maxAge: 30, // default is set in COOKIE_MAX_AGE option in .env file }, }, // ... });
CORS
Setting CORS is done using cors.allowed{Headers,Methods,Origins}
options. You can set expiry time with cors.maxAge
option.
src/main.ts
import { HttpMethod } from '@entropy/http'; const server = await createServer({ config: { cors: { allowedMethods: [ HttpMethod.Get, ], allowedOrigins: [ 'https://some-website.com', ], maxAge: 15, }, }, // ... });
CSRF Protection
You can disable the built-in CSRF protection for form requests using csrfProtection
option.
src/main.ts
const server = await createServer({ config: { cookies: { maxAge: 30, // default is set in COOKIE_MAX_AGE option in .env file }, }, // ... });
Locales
You may set the application supported locales along with the default one for localization.
src/main.ts
const server = await createServer({ config: { locales: { default: 'en', supported: ['de', 'en', 'fr', 'pl'], }, }, // ... });
Logger
You can entirely disable logger logs setting logger.enabled
to false
.
src/main.ts
const server = await createServer({ config: { logger: { enabled: false, staticFileRequests: true, // Log server file requests }, }, // ... });
Automatic Sitemap and robots.txt
File Generation
Entropy has built-in feature of auto-generating sitemap.xml
and robots.txt
SEO files.
src/main.ts
const server = await createServer({ config: { seo: { robots: true, sitemap: true, }, }, // ... });