Created
April 2, 2021 14:53
-
-
Save mattbloomfield/b037cad5b341c8cd51e4bacb20f9ef1a to your computer and use it in GitHub Desktop.
Lambda Function to retrieve the .env file for a site based on sitename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
module.exports.endpoint = async (event, context, callback) => { | |
const queryParams = event.queryStringParameters; | |
if (!queryParams) callback("No query params"); | |
const devUser = queryParams.user; | |
const site = queryParams.site; | |
const key = queryParams.key; | |
if (!key) callback("no auth"); | |
if (key !== process.env.KEY) callback("failed auth"); | |
if (!devUser || !site) { | |
callback("no site or no devUser provided"); | |
} | |
const config = await getConfig(site, devUser); | |
const response = { | |
statusCode: 200, | |
body: config, | |
}; | |
callback(null, response); | |
}; | |
const getConfig = (site, user) => { | |
return new Promise((resolve, reject) => { | |
try { | |
resolve(configs[site].replace(/_USER_TO_BE_REPLACED_/g, user)); | |
} catch (error) { | |
reject(new Error("File does not exist")); | |
} | |
}); | |
}; | |
const configs = { | |
sitename1: ` | |
# The environment Craft is currently running in ('dev', 'staging', 'production', etc.) | |
ENVIRONMENT="local" | |
# The secure key Craft will use for hashing and encrypting data | |
SECURITY_KEY=${process.env.SITENAME1_SEC_KEY} | |
# The database driver that will be used ('mysql' or 'pgsql') | |
DB_DRIVER="mysql" | |
# The database server name or IP address (usually this is 'localhost' or '127.0.0.1') | |
DB_SERVER="" | |
# The database username to connect with | |
DB_USER="root" | |
# The database password to connect with | |
DB_PASSWORD="root" | |
# The name of the database to select | |
DB_DATABASE="my_site" | |
# The database schema that will be used (PostgreSQL only) | |
DB_SCHEMA="public" | |
# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database) | |
DB_TABLE_PREFIX="" | |
# The port to connect to the database with. Will default to 5432 for PostgreSQL and 3306 for MySQL. | |
DB_PORT="33062" | |
# FASTLY SETTINGS | |
UPPER_DRIVER="fastly" | |
FASTLY_API_TOKEN="${process.env.SITENAME2_FASTLY_TOKEN}" | |
FASTLY_SERVICE_ID="xxxxxxxxxxxx" | |
FASTLY_DOMAIN="https://www.xxxxxxxxxxx.com" | |
`, | |
sitename2: ` | |
# The environment Craft is currently running in ('dev', 'staging', 'production', etc.) | |
ENVIRONMENT="local" | |
# The secure key Craft will use for hashing and encrypting data | |
SECURITY_KEY="${process.env.SITENAME2_SEC_KEY}" | |
# The Data Source Name (“DSN”) that tells Craft how to connect to the database | |
DB_DSN="mysql:host=192.168.64.100;port=33061;dbname=my_db" | |
# The database username to connect with | |
DB_USER="root" | |
# The database password to connect with | |
DB_PASSWORD="root" | |
# The database schema that will be used (PostgreSQL only) | |
DB_SCHEMA="public" | |
# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database) | |
DB_TABLE_PREFIX="" | |
DEFAULT_SITE_URL="https://mysite.docksal" | |
SYSTEM_NAME="LOCAL DEV" | |
`, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment