@@ -5,33 +5,66 @@ import getPort from 'get-port';
55import { contractDefinition } from './contractDefinition.js' ;
66import { contractVerification } from './contractVerification.js' ;
77import { versionString } from '../../versionString.js' ;
8+ import { maintainerLog } from '../../domain/maintainerLog.js' ;
9+
10+ type ServerInfo = { port : string ; address : string } ;
11+
12+ const bind = (
13+ server : Server ,
14+ host : string ,
15+ freePort : number ,
16+ ) : Promise < ServerInfo > =>
17+ new Promise ( ( resolve , reject ) => {
18+ const address = `${ host } :${ freePort } ` ;
19+ maintainerLog ( `Attempting to bind server: ${ address } ` ) ;
20+ server . bindAsync (
21+ address ,
22+ ServerCredentials . createInsecure ( ) ,
23+ ( error , port ) => {
24+ if ( error != null ) {
25+ reject ( error ) ;
26+ } else {
27+ resolve ( { port : `${ port } ` , address } ) ;
28+ }
29+ } ,
30+ ) ;
31+ } ) ;
832
933/**
1034 * Starts a gRPC server for defining and verifying ContractCase contracts
1135 */
1236export function main ( ) : void {
37+ maintainerLog ( `Starting ContractCase connector @ ${ versionString } ` ) ;
1338 const server = new Server ( ) ;
1439
1540 getPort ( ) . then ( ( freePort ) => {
1641 server . addService ( ContractCaseService , {
1742 contractDefinition,
1843 contractVerification,
1944 } ) ;
20- server . bindAsync (
21- `0.0.0.0: ${ freePort } ` ,
22- ServerCredentials . createInsecure ( ) ,
23- ( error , port ) => {
24- if ( error != null ) {
25- // Console used here because there's nothing sensible we can do with this error
26- // eslint-disable-next-line no-console
27- console . error ( `[ ${ versionString } ]` , `Unable to start: ${ error } ` ) ;
28- } else {
45+
46+ return bind ( server , '[::1]' , freePort )
47+ . catch ( ( error ) => {
48+ maintainerLog ( 'Server bind failed with error: ' , error ) ;
49+ return bind ( server , '127.0.0.1' , freePort ) ;
50+ } )
51+ . then (
52+ ( { port , address } ) => {
53+ maintainerLog ( `Successfully started server at ${ address } ` ) ;
2954 // This is needed to communicate with clients
3055 // Must have the port number after the `:`
3156 // eslint-disable-next-line no-console
3257 console . log ( '[SERVER]' , `Started on port: ${ port } ` ) ;
33- }
34- } ,
35- ) ;
58+ } ,
59+ ( error ) => {
60+ // Console used here because there's nothing sensible we can do with this error
61+ // eslint-disable-next-line no-console
62+ console . error (
63+ `[${ versionString } ]` ,
64+ `ContractCase's internal server was unable to start: ${ error } ` ,
65+ ) ;
66+ process . exit ( 1 ) ;
67+ } ,
68+ ) ;
3669 } ) ;
3770}
0 commit comments