Skip to content

Commit

Permalink
chore: fix express start
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Aug 8, 2023
1 parent 36f270d commit 27c8a3e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions packages/ssi-express-support/src/express-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ExpressBuilder {

public static fromServerOpts(opts: IExpressServerOpts & { envVarPrefix?: string }) {
const builder = new ExpressBuilder({ existingExpress: opts?.existingExpress, envVarPrefix: opts?.envVarPrefix })
return builder.withEnableListenOpts({...opts, hostnameOrIP: opts.hostname, startOnBuild: opts.startListening ?? false})
return builder.withEnableListenOpts({ ...opts, hostnameOrIP: opts.hostname, startOnBuild: opts.startListening ?? false })
}

public enableListen(startOnBuild?: boolean): this {
Expand Down Expand Up @@ -179,7 +179,12 @@ export class ExpressBuilder {
handlers?: ApplicationRequestHandler<T> | ApplicationRequestHandler<T>[]
}): ExpressSupport {
const express = this.buildExpress(opts)
const startListening = opts?.startListening === undefined ? this._startListen === true : opts.startListening
const startListening = opts?.startListening === undefined ? this._startListen !== true : opts.startListening
let started = false
if (startListening) {
this.startListening(express)
started = true
}
return {
express,
port: this.getPort(),
Expand All @@ -188,10 +193,15 @@ export class ExpressBuilder {
startListening,
enforcer: this._enforcer,
start: (opts) => {
if (startListening) {
this.startListening(express)
if (opts?.doNotStartListening) {
console.log('Express will not start listening. You will have to start it yourself')
} else {
throw Error('Express already started during build')
if (!started) {
this.startListening(express)
started = true
} else {
throw Error('Express already started during build')
}
}

if (opts?.disableErrorHandler !== true) {
Expand Down Expand Up @@ -235,10 +245,6 @@ export class ExpressBuilder {

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

if (this._startListen === true) {
this.startListening(app)
}
return app
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ssi-express-support/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface ExpressSupport {
userIsInRole?: string | string[]
startListening: boolean
enforcer?: Enforcer
start: (opts?: { disableErrorHandler?: boolean }) => Express
start: (opts?: { disableErrorHandler?: boolean; doNotStartListening?: boolean }) => Express
}

export interface ISingleEndpointOpts extends GenericAuthArgs {
Expand Down

0 comments on commit 27c8a3e

Please sign in to comment.