Skip to content

Commit

Permalink
feat: expand URL variables (#9)
Browse files Browse the repository at this point in the history
* feat: expand URL variables

* Rename .env.local to .env
  • Loading branch information
fmvilas authored Jul 13, 2021
1 parent 4c8f006 commit ca49bb9
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 17 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 15 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@
"bufferutil": "^4.0.3",
"chalk": "^4.1.1",
"debug": "^4.3.1",
"dotenv": "^10.0.0",
"dotenv-expand": "^5.1.0",
"emojis": "^1.0.10",
"localenv": "^0.2.2",
"mqtt": "^4.2.6",
"node-ipc": "^10.0.1",
"nodemon": "^2.0.7",
"path-to-regexp": "^6.2.0",
"socket.io": "^4.1.2",
"terminal-image": "^2.0.0",
"uri-templates": "^0.2.0",
"utf-8-validate": "^5.0.5",
"uuid": "^8.3.2",
"walkdir": "^0.4.1",
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/ws/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WebSocketsAdapter extends Adapter {
_connect () {
return new Promise((resolve, reject) => {
const channelNames = this.parsedAsyncAPI.channelNames()
const serverUrl = new URL(this.AsyncAPIServer.url())
const serverUrl = new URL(this.serverUrlExpanded)
const wsHttpServer = this.glee.options?.websocket?.httpServer || http.createServer()
const asyncapiServerPort = serverUrl.port || 80

Expand Down
2 changes: 1 addition & 1 deletion src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if (command === 'dev') {
script: path.resolve(__dirname, 'start.js'),
ext: '*',
watch: [
'.env.local',
'.env',
],
env: {
NODE_ENV: 'development',
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'localenv'
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'
Expand All @@ -19,6 +20,8 @@ import { startRuntimeServers } from './lib/runtimes/index.js'
import { setConstants } from './lib/constants.js'
import { triggerFunction } from './lib/runtimes/index.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.')
Expand Down
9 changes: 9 additions & 0 deletions src/lib/adapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EventEmitter from 'events'
import uriTemplates from 'uri-templates'
import GleeConnection from './connection.js'

class GleeAdapter extends EventEmitter {
Expand All @@ -16,9 +17,17 @@ class GleeAdapter extends EventEmitter {
this.glee = glee
this.serverName = serverName
this.AsyncAPIServer = server

this.parsedAsyncAPI = parsedAsyncAPI
this.connections = []

const uriTemplateValues = {}
process.env.GLEE_SERVER_VARIABLES?.split(',').forEach(t => {
const [server, variable, value] = t.split(':')
if (server === this.serverName) uriTemplateValues[variable] = value
})
this.serverUrlExpanded = uriTemplates(this.AsyncAPIServer.url()).fill(uriTemplateValues)

this.on('error', err => { this.glee.injectError(err) })
this.on('message', (message, connection) => {
const conn = new GleeConnection({
Expand Down

0 comments on commit ca49bb9

Please sign in to comment.