1
+ #!/usr/bin/env node
2
+
3
+ const express = require ( 'express' ) ;
4
+ const morgan = require ( 'morgan' ) ;
5
+ const { createProxyMiddleware } = require ( 'http-proxy-middleware' ) ;
6
+ const { Command, Option } = require ( 'commander' ) ;
7
+ const program = new Command ( ) ;
8
+
9
+ // Parse user variables
10
+ program
11
+ . addOption ( new Option ( '-p, --port <number>' , 'proxy server port number' ) . default ( 9090 ) . env ( 'BRIDGE_PORT' ) )
12
+ . addOption ( new Option ( '-u, --host <host>' , 'proxy server host name' ) . default ( '0.0.0.0' ) . env ( 'BRIDGE_HOST' ) )
13
+ . addOption ( new Option ( '-s, --staticFiles <dir>' , 'the root directory from which to serve static assets' ) . default ( './docs' ) )
14
+ . addOption ( new Option ( '-e, --clusterEndpoint <endpoint>' , 'drink size' ) . makeOptionMandatory ( true ) . env ( 'BRIDGE_CLUSTER_ENDPOINT' ) )
15
+ . addOption ( new Option ( '-t, --clusterThanos <thanos>' , 'drink size' ) . makeOptionMandatory ( true ) . env ( 'BRIDGE_CLUSTER_THANOS' ) )
16
+ . addOption ( new Option ( '-a, --authBearerToken <auth>' , 'port number' ) . makeOptionMandatory ( true ) . env ( 'BRIDGE_AUTH_BEARER_TOKEN' ) ) ;
17
+
18
+ program . parse ( ) ;
19
+ const options = program . opts ( ) ;
20
+
21
+ // Printout options
22
+ console . log ( { ...options , authBearerToken : 'hidden' } )
23
+
24
+ // Create Express Server
25
+ const app = express ( ) ;
26
+ app . disable ( "x-powered-by" ) ;
27
+
28
+ // Logging
29
+ app . use ( morgan ( 'dev' ) ) ;
30
+
31
+ // Info GET endpoint
32
+ app . get ( '/info' , ( req , res ) => {
33
+ res . json ( { app :'kubevirt ui bridge' , k8s_server : '' , metrics_server : '' } ) ;
34
+ } ) ;
35
+
36
+ // Proxy endpoints
37
+ app . use ( '/api/kubernetes' , createProxyMiddleware ( {
38
+ target : options . clusterEndpoint ,
39
+ secure : false ,
40
+ changeOrigin : true ,
41
+ ws : true ,
42
+ headers : { Authorization :`Bearer ${ options . authBearerToken } ` } ,
43
+ pathRewrite : {
44
+ [ `^/api/kubernetes` ] : '' ,
45
+ } ,
46
+ } ) ) ;
47
+
48
+ app . use ( '/api/prometheus' , createProxyMiddleware ( {
49
+ target : options . clusterThanos ,
50
+ secure : false ,
51
+ changeOrigin : true ,
52
+ pathRewrite : {
53
+ [ `^/api/prometheus` ] : '' ,
54
+ } ,
55
+ } ) ) ;
56
+
57
+ // Serve static files
58
+ app . use ( express . static ( options . staticFiles ) ) ;
59
+
60
+ // Start the Proxy
61
+ app . listen ( options . port , options . host , ( ) => {
62
+ console . log ( `Starting kubevirt ui bridge ${ options . host } :${ options . port } ` ) ;
63
+ console . log ( `On localhost http://localhost:${ options . port } ` ) ;
64
+ } ) ;
0 commit comments