@@ -6,7 +6,7 @@ import { OpenApiValidator } from 'express-openapi-validator';
6
6
import compression from 'compression' ;
7
7
import bodyParser from 'body-parser' ;
8
8
import cors , { CorsOptions } from 'cors' ;
9
- import { IPluginKVStorage , PluginFactory , ICactusPlugin , PluginAspect } from '@hyperledger-labs/bif-core-api' ;
9
+ import { IPluginKVStorage , PluginFactory , ICactusPlugin , PluginAspect , isIPluginWebService , IPluginWebService } from '@hyperledger-labs/bif-core-api' ;
10
10
import { CreateConsortiumEndpointV1 } from './consortium/routes/create-consortium-endpoint-v1' ;
11
11
import { IBifApiServerOptions , ConfigService } from './config/config-service' ;
12
12
import { BIF_OPEN_API_JSON } from './openapi-spec' ;
@@ -68,116 +68,122 @@ export class ApiServer {
68
68
}
69
69
}
70
70
71
- async startCockpitFileServer ( ) : Promise < void > {
72
- const cockpitWwwRoot = this . options . config . get ( 'cockpitWwwRoot' ) ;
73
- this . log . info ( `wwwRoot: ${ cockpitWwwRoot } ` ) ;
71
+ async startCockpitFileServer ( ) : Promise < void > {
72
+ const cockpitWwwRoot = this . options . config . get ( 'cockpitWwwRoot' ) ;
73
+ this . log . info ( `wwwRoot: ${ cockpitWwwRoot } ` ) ;
74
74
75
- const resolvedWwwRoot = path . resolve ( process . cwd ( ) , cockpitWwwRoot ) ;
76
- this . log . info ( `resolvedWwwRoot: ${ resolvedWwwRoot } ` ) ;
75
+ const resolvedWwwRoot = path . resolve ( process . cwd ( ) , cockpitWwwRoot ) ;
76
+ this . log . info ( `resolvedWwwRoot: ${ resolvedWwwRoot } ` ) ;
77
77
78
- const resolvedIndexHtml = path . resolve ( resolvedWwwRoot + '/index.html' ) ;
79
- this . log . info ( `resolvedIndexHtml: ${ resolvedIndexHtml } ` ) ;
78
+ const resolvedIndexHtml = path . resolve ( resolvedWwwRoot + '/index.html' ) ;
79
+ this . log . info ( `resolvedIndexHtml: ${ resolvedIndexHtml } ` ) ;
80
80
81
- const app : Express = express ( ) ;
82
- app . use ( compression ( ) ) ;
83
- app . use ( express . static ( resolvedWwwRoot ) ) ;
84
- app . get ( '/*' , ( _ , res ) => res . sendFile ( resolvedIndexHtml ) ) ;
81
+ const app : Express = express ( ) ;
82
+ app . use ( compression ( ) ) ;
83
+ app . use ( express . static ( resolvedWwwRoot ) ) ;
84
+ app . get ( '/*' , ( _ , res ) => res . sendFile ( resolvedIndexHtml ) ) ;
85
85
86
- const cockpitPort : number = this . options . config . get ( 'cockpitPort' ) ;
87
- const cockpitHost : string = this . options . config . get ( 'cockpitHost' ) ;
86
+ const cockpitPort : number = this . options . config . get ( 'cockpitPort' ) ;
87
+ const cockpitHost : string = this . options . config . get ( 'cockpitHost' ) ;
88
88
89
- await new Promise < any > ( ( resolve , reject ) => {
90
- this . httpServerCockpit = app . listen ( cockpitPort , cockpitHost , ( ) => {
91
- this . log . info ( `Cactus Cockpit UI reachable on port http://${ cockpitHost } :${ cockpitPort } ` ) ;
92
- resolve ( { cockpitPort } ) ;
89
+ await new Promise < any > ( ( resolve , reject ) => {
90
+ this . httpServerCockpit = app . listen ( cockpitPort , cockpitHost , ( ) => {
91
+ this . log . info ( `Cactus Cockpit UI reachable on port http://${ cockpitHost } :${ cockpitPort } ` ) ;
92
+ resolve ( { cockpitPort } ) ;
93
+ } ) ;
94
+ this . httpServerCockpit . on ( 'error' , ( err : any ) => reject ( err ) ) ;
93
95
} ) ;
94
- this . httpServerCockpit . on ( 'error' , ( err : any ) => reject ( err ) ) ;
95
- } ) ;
96
- }
96
+ }
97
97
98
- async startApiServer ( ) : Promise < void > {
99
- const app : Application = express ( ) ;
100
- app . use ( compression ( ) ) ;
98
+ async startApiServer ( ) : Promise < void > {
99
+ const app : Application = express ( ) ;
100
+ app . use ( compression ( ) ) ;
101
101
102
- const corsMiddleware = this . createCorsMiddleware ( )
102
+ const corsMiddleware = this . createCorsMiddleware ( )
103
103
app . use ( corsMiddleware ) ;
104
104
105
- app . use ( bodyParser . json ( { limit : '50mb' } ) ) ;
105
+ app . use ( bodyParser . json ( { limit : '50mb' } ) ) ;
106
106
107
- const openApiValidator = this . createOpenApiValidator ( ) ;
108
- await openApiValidator . install ( app ) ;
107
+ const openApiValidator = this . createOpenApiValidator ( ) ;
108
+ await openApiValidator . install ( app ) ;
109
109
110
- app . get ( '/healthcheck' , ( req : Request , res : Response , next : NextFunction ) => {
111
- res . json ( { 'success' : true , timestamp : new Date ( ) } ) ;
112
- } ) ;
110
+ app . get ( '/healthcheck' , ( req : Request , res : Response , next : NextFunction ) => {
111
+ res . json ( { 'success' : true , timestamp : new Date ( ) } ) ;
112
+ } ) ;
113
113
114
- const storage : IPluginKVStorage = await this . createStoragePlugin ( ) ;
115
- const configService = new ConfigService ( ) ;
116
- const config = configService . getOrCreate ( ) ;
114
+ const storage : IPluginKVStorage = await this . createStoragePlugin ( ) ;
115
+ const configService = new ConfigService ( ) ;
116
+ const config = configService . getOrCreate ( ) ;
117
117
{
118
- const endpoint = new CreateConsortiumEndpointV1 ( { storage, config } ) ;
119
- app . post ( endpoint . getPath ( ) , endpoint . handleRequest . bind ( endpoint ) ) ;
120
- }
121
-
122
- // FIXME
123
- // app.get('/api/v1/consortium/:consortiumId', (req: Request, res: Response, next: NextFunction) => {
124
- // res.json({ swagger: 'TODO' });
125
- // });
126
-
127
- const apiPort : number = this . options . config . get ( 'apiPort' ) ;
128
- const apiHost : string = this . options . config . get ( 'apiHost' ) ;
129
- this . log . info ( `Binding Cactus API to port ${ apiPort } ...` ) ;
130
- await new Promise < any > ( ( resolve , reject ) => {
131
- const httpServerApi = app . listen ( apiPort , apiHost , ( ) => {
132
- const address : any = httpServerApi . address ( ) ;
133
- this . log . info ( `Successfully bound API to port ${ apiPort } ` , { address } ) ;
134
- if ( address && address . port ) {
135
- resolve ( { port : address . port } ) ;
136
- } else {
137
- resolve ( { port : apiPort } ) ;
118
+ const endpoint = new CreateConsortiumEndpointV1 ( { storage, config } ) ;
119
+ app . post ( endpoint . getPath ( ) , endpoint . handleRequest . bind ( endpoint ) ) ;
138
120
}
139
- } ) ;
140
- this . httpServerApi = httpServerApi ;
141
- this . httpServerApi . on ( 'error' , ( err ) => reject ( err ) ) ;
142
- } ) ;
143
- }
144
121
145
- createOpenApiValidator ( ) : OpenApiValidator {
146
- return new OpenApiValidator ( {
147
- apiSpec : BIF_OPEN_API_JSON ,
148
- validateRequests : true ,
149
- validateResponses : false
150
- } ) ;
151
- }
122
+ this . options . plugins
123
+ . filter ( ( pluginInstance ) => isIPluginWebService ( pluginInstance ) )
124
+ . forEach ( ( pluginInstance : ICactusPlugin ) => {
125
+ ( pluginInstance as IPluginWebService ) . installWebService ( app ) ;
126
+ } ) ;
127
+
128
+ // FIXME
129
+ // app.get('/api/v1/consortium/:consortiumId', (req: Request, res: Response, next: NextFunction) => {
130
+ // res.json({ swagger: 'TODO' });
131
+ // });
132
+
133
+ const apiPort : number = this . options . config . get ( 'apiPort' ) ;
134
+ const apiHost : string = this . options . config . get ( 'apiHost' ) ;
135
+ this . log . info ( `Binding Cactus API to port ${ apiPort } ...` ) ;
136
+ await new Promise < any > ( ( resolve , reject ) => {
137
+ const httpServerApi = app . listen ( apiPort , apiHost , ( ) => {
138
+ const address : any = httpServerApi . address ( ) ;
139
+ this . log . info ( `Successfully bound API to port ${ apiPort } ` , { address } ) ;
140
+ if ( address && address . port ) {
141
+ resolve ( { port : address . port } ) ;
142
+ } else {
143
+ resolve ( { port : apiPort } ) ;
144
+ }
145
+ } ) ;
146
+ this . httpServerApi = httpServerApi ;
147
+ this . httpServerApi . on ( 'error' , ( err ) => reject ( err ) ) ;
148
+ } ) ;
149
+ }
152
150
153
- async createStoragePlugin ( ) : Promise < IPluginKVStorage > {
154
- const kvStoragePlugin = this . options . plugins . find ( ( p ) => p . getAspect ( ) === PluginAspect . KV_STORAGE ) ;
155
- if ( kvStoragePlugin ) {
156
- return kvStoragePlugin as IPluginKVStorage ;
151
+ createOpenApiValidator ( ) : OpenApiValidator {
152
+ return new OpenApiValidator ( {
153
+ apiSpec : BIF_OPEN_API_JSON ,
154
+ validateRequests : true ,
155
+ validateResponses : false
156
+ } ) ;
157
157
}
158
+
159
+ async createStoragePlugin ( ) : Promise < IPluginKVStorage > {
160
+ const kvStoragePlugin = this . options . plugins . find ( ( p ) => p . getAspect ( ) === PluginAspect . KV_STORAGE ) ;
161
+ if ( kvStoragePlugin ) {
162
+ return kvStoragePlugin as IPluginKVStorage ;
163
+ }
158
164
const storagePluginPackage = this . options . config . get ( 'storagePluginPackage' ) ;
159
- const { PluginFactoryKVStorage } = await import ( storagePluginPackage ) ;
160
- const storagePluginOptionsJson = this . options . config . get ( 'storagePluginOptionsJson' ) ;
161
- const storagePluginOptions = JSON . parse ( storagePluginOptionsJson ) ;
162
- const pluginFactory : PluginFactory < IPluginKVStorage , unknown > = new PluginFactoryKVStorage ( ) ;
163
- const plugin = await pluginFactory . create ( storagePluginOptions ) ;
164
- return plugin ;
165
- }
165
+ const { PluginFactoryKVStorage } = await import ( storagePluginPackage ) ;
166
+ const storagePluginOptionsJson = this . options . config . get ( 'storagePluginOptionsJson' ) ;
167
+ const storagePluginOptions = JSON . parse ( storagePluginOptionsJson ) ;
168
+ const pluginFactory : PluginFactory < IPluginKVStorage , unknown > = new PluginFactoryKVStorage ( ) ;
169
+ const plugin = await pluginFactory . create ( storagePluginOptions ) ;
170
+ return plugin ;
171
+ }
166
172
167
- createCorsMiddleware ( ) : RequestHandler {
168
- const apiCorsDomainCsv = this . options . config . get ( 'apiCorsDomainCsv' ) ;
169
- const allowedDomains = apiCorsDomainCsv . split ( ',' ) ;
170
- const allDomainsAllowed = allowedDomains . includes ( '*' ) ;
171
-
172
- const corsOptions : CorsOptions = {
173
- origin : ( origin : string | undefined , callback ) => {
174
- if ( allDomainsAllowed || origin && allowedDomains . indexOf ( origin ) !== - 1 ) {
175
- callback ( null , true ) ;
176
- } else {
177
- callback ( new Error ( `CORS not allowed for Origin "${ origin } ".` ) ) ;
173
+ createCorsMiddleware ( ) : RequestHandler {
174
+ const apiCorsDomainCsv = this . options . config . get ( 'apiCorsDomainCsv' ) ;
175
+ const allowedDomains = apiCorsDomainCsv . split ( ',' ) ;
176
+ const allDomainsAllowed = allowedDomains . includes ( '*' ) ;
177
+
178
+ const corsOptions : CorsOptions = {
179
+ origin : ( origin : string | undefined , callback ) => {
180
+ if ( allDomainsAllowed || origin && allowedDomains . indexOf ( origin ) !== - 1 ) {
181
+ callback ( null , true ) ;
182
+ } else {
183
+ callback ( new Error ( `CORS not allowed for Origin "${ origin } ".` ) ) ;
184
+ }
178
185
}
179
186
}
187
+ return cors ( corsOptions ) ;
180
188
}
181
- return cors ( corsOptions ) ;
182
- }
183
189
}
0 commit comments