Skip to content

Commit

Permalink
feat: make .env optional (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmvilas authored Jul 19, 2021
1 parent 451e7f3 commit 460d06e
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 111 deletions.
7 changes: 4 additions & 3 deletions docs/app-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Glee expects your project to have some files and folders with special names. The
├─ lifecycle (optional)
│ ├─ onConnect.js
│ └─ ...
├─ .env (required)
├─ .env (optional)
├─ asyncapi.yaml (required)
├─ glee.config.js (optional)
├─ package.json (required)
Expand All @@ -19,7 +19,8 @@ Glee expects your project to have some files and folders with special names. The
|---|---|
|functions|**Required.** This directory contains all the functions that Glee must execute when it receives a message from the server. Each file must export a default async function. [Read more about the functions signature](./functions.md).
|lifecycle|This directory contains application lifecycle functions. These functions will be executed when certain events happen in the application. E.g., `onConnect`, `onServerReady`, `onDisconnect`, etc. [Read the full list of lifecycle events](./lifecycle-events.md).
|.env|**Required.** The environment variables of your application. At the very minimum, it must contain the `GLEE_SERVER_NAMES` variable with a comma-separated list of the servers you want to load at startup. **DO NOT PUT SECRETS HERE**.
|.env|The environment variables of your application. Read more about the Glee [environment variables](./env-vars.md). **DO NOT PUT SECRETS HERE**.
|asyncapi.yaml|**Required.** The [AsyncAPI](https://www.asyncapi.com/docs/specifications/latest) file defining your API. Make sure all the `publish` operations have an assigned `operationId` that matches a file name (excluding the extension) in the `functions` directory.
|glee.config.js|The Glee configuration file. [Read more about how to use this file](./config-file.md).
|package.json|**Required.** The Node.js package definition file. Make sure you include `@asyncapi/glee` as a dependency and add two scripts: `dev` and `start`. They should be running `glee dev` and `glee start` respectively.
|package.json|**Required.** The Node.js package definition file. Make sure you include `@asyncapi/glee` as a dependency and add two scripts: `dev` and `start`. They should be running `glee dev` and `glee start` respectively.

9 changes: 9 additions & 0 deletions docs/env-vars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Environment Variables

Glee provides a few environment variables for you to customize the experience:

|Variable|Description|Example|
|---|---|---|
|GLEE_SERVER_NAMES|A comma-separated list of the servers to load on startup.|`GLEE_SERVER_NAMES=websockets,mosquitto`
|GLEE_SERVER_CERTS|A comma-separated list of `${serverName}:${pathToCertificateFile}`. These are the certificates to use when establishing the connection to the given server.|`GLEE_SERVER_CERTS=mosquitto:mosquitto.org.crt`
|GLEE_SERVER_VARIABLES|A comma-separated list of `${serverName}:${serverVariable}:${value}`. These are the values to use for each server variable.|`GLEE_SERVER_VARIABLES=websockets:namespace:public`
3 changes: 0 additions & 3 deletions examples/dummy/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
GLEE_SERVER_NAMES=mosquitto,websockets
GLEE_SERVER_CERTS=mosquitto:mosquitto.org.crt
# GLEE_USERNAME=testing
# GLEE_PASSWORD=Test1ng!
31 changes: 0 additions & 31 deletions src/adapters/mqtt/README.md

This file was deleted.

31 changes: 0 additions & 31 deletions src/adapters/socket.io/README.md

This file was deleted.

31 changes: 0 additions & 31 deletions src/adapters/ws/README.md

This file was deleted.

15 changes: 5 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'
import { readFile } from 'fs/promises'
import asyncapi from '@asyncapi/parser'
import Glee from './lib/glee.js'
import { logWelcome, logLineWithIcon } from './lib/logger.js'
import experimentalFlags from './lib/experimentalFlags.js'
Expand All @@ -19,14 +17,12 @@ import validateConnection from './middlewares/validateConnection.js'
import { startRuntimeServers } from './lib/runtimes/index.js'
import { setConstants } from './lib/constants.js'
import { triggerFunction } from './lib/runtimes/index.js'
import { getParsedAsyncAPI } from './lib/asyncapiFile.js'
import { getSelectedServerNames } from './lib/servers.js'

dotenvExpand(dotenv.config())

export default async function GleeAppInitializer (config = {}) {
if (!process.env.GLEE_SERVER_NAMES) {
throw new Error('Missing "GLEE_SERVER_NAMES" environment variable.')
}

const {
GLEE_DIR,
GLEE_LIFECYCLE_DIR,
Expand All @@ -37,7 +33,7 @@ export default async function GleeAppInitializer (config = {}) {

logWelcome({
dev: process.env.NODE_ENV === 'development',
servers: process.env.GLEE_SERVER_NAMES.split(','),
servers: await getSelectedServerNames(),
dir: GLEE_DIR,
functionsDir: GLEE_FUNCTIONS_DIR,
experimentalFlags,
Expand All @@ -60,13 +56,12 @@ export default async function GleeAppInitializer (config = {}) {
}
}

const asyncapiFileContent = await readFile(ASYNCAPI_FILE_PATH, 'utf-8')
const parsedAsyncAPI = await asyncapi.parse(asyncapiFileContent)
const parsedAsyncAPI = await getParsedAsyncAPI()
const channelNames = parsedAsyncAPI.channelNames()

const app = new Glee(config)

registerAdapters(app, parsedAsyncAPI, config)
await registerAdapters(app, parsedAsyncAPI, config)

app.use(existsInAsyncAPI(parsedAsyncAPI))
app.useOutbound(existsInAsyncAPI(parsedAsyncAPI))
Expand Down
9 changes: 9 additions & 0 deletions src/lib/asyncapiFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { readFile } from 'fs/promises'
import asyncapi from '@asyncapi/parser'
import { getConstants } from './constants.js'

export async function getParsedAsyncAPI() {
const { ASYNCAPI_FILE_PATH } = getConstants()
const asyncapiFileContent = await readFile(ASYNCAPI_FILE_PATH, 'utf-8')
return asyncapi.parse(asyncapiFileContent)
}
14 changes: 14 additions & 0 deletions src/lib/servers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getParsedAsyncAPI } from './asyncapiFile.js'

export async function getSelectedServerNames() {
const parsedAsyncAPI = await getParsedAsyncAPI()

if (!process.env.GLEE_SERVER_NAMES) {
return parsedAsyncAPI.serverNames()
}

const arrayOfNames = process.env.GLEE_SERVER_NAMES.split(',')
return parsedAsyncAPI.serverNames().filter(name => {
return arrayOfNames.includes(name)
})
}
5 changes: 3 additions & 2 deletions src/registerAdapters.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import MqttAdapter from './adapters/mqtt/index.js'
import WebSocketAdapter from './adapters/ws/index.js'
import SocketIOAdapter from './adapters/socket.io/index.js'
import { getSelectedServerNames } from './lib/servers.js'

export default (app, parsedAsyncAPI, config) => {
const serverNames = process.env.GLEE_SERVER_NAMES.split(',')
export default async (app, parsedAsyncAPI, config) => {
const serverNames = await getSelectedServerNames()

serverNames.forEach(serverName => {
const server = parsedAsyncAPI.server(serverName)
Expand Down

0 comments on commit 460d06e

Please sign in to comment.