Skip to content

Commit

Permalink
chore: cleanup async http server stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 committed Aug 9, 2024
1 parent 391d2d8 commit b0bea4b
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,28 +448,20 @@ class Server {
const primaryPort = getPrimaryPort(app)
debug(`primary port:${primaryPort}`)

serverListen('signalk-server running at', app.config.settings.networkInterfaces, servers, primaryPort, (err:any) => {
if ( !err ) {
app.started = true
resolve(self)
} else {
reject(err)
}
})
await serverListen('signalk-server running at', app.config.settings.networkInterfaces, servers, primaryPort)

const secondaryPort = getSecondaryPort(app)
debug(`secondary port:${primaryPort}`)
if (app.config.settings.ssl && secondaryPort) {
startRedirectToSsl(
app.redirectServers = await startRedirectToSsl(
secondaryPort,
getExternalPort(app),
(anErr: any, aServers: any[]) => {
if (!anErr) {
app.redirectServers = aServers
}
},
app.config.settings.networkInterfaces
)
}

app.started = true
resolve(self)
})
})
}
Expand Down Expand Up @@ -603,7 +595,7 @@ function createServer(app: any, cb: (err: any, servers?: any[]) => void) {
cb(null, servers)
}

function serverListen(msg:string, networkInterfaces: string[] | undefined, servers: any[], port: number | { fd: number; }, cb: (err: any) => void)
function serverListen(msg:string, networkInterfaces: string[] | undefined, servers: any[], port: number | { fd: number; })
{
let interfaces : any

Expand Down Expand Up @@ -631,15 +623,12 @@ function serverListen(msg:string, networkInterfaces: string[] | undefined, serve
}
}))
})
Promise.all(promises)
.then( () => { cb(null) } )
.catch( cb )
return Promise.all(promises)
}

function startRedirectToSsl(
async function startRedirectToSsl(
port: number,
redirectPort: number,
cb: (e: unknown, servers: any[]) => void,
networkInterfaces: string[] | undefined,
) {
const redirectApp = express()
Expand All @@ -651,9 +640,12 @@ function startRedirectToSsl(
return http.createServer(redirectApp)
})

serverListen('Redirect server running at', networkInterfaces, servers, port, (err:any) => {
cb(err, servers)
})
await serverListen('signalk-server redirect server running at', networkInterfaces, servers, port)

throw new Error('Configuration is immutable')


return servers
}

function startMdns(app: ServerApp & WithConfig) {
Expand Down

0 comments on commit b0bea4b

Please sign in to comment.