File tree 1 file changed +58
-0
lines changed
1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ require ( 'es6-promise' ) . polyfill ( ) ;
2
+ require ( 'isomorphic-fetch' ) ;
3
+
4
+ const LRU = require ( 'lru-cache' ) ;
5
+ const express = require ( 'express' ) ;
6
+ const router = express . Router ( ) ;
7
+
8
+ const Rollbar = require ( "rollbar" ) ;
9
+ const rollbar = new Rollbar ( process . env . ROLLBAR_ACCESS_TOKEN ) ;
10
+
11
+ /**
12
+ * Cache requests for 3 hours
13
+ *
14
+ * @var NodeCache
15
+ */
16
+ const cache = new LRU ( {
17
+ max : 1000 ,
18
+ maxAge : 7 * 60 * 60 * 1000 ,
19
+ } ) ;
20
+
21
+ /**
22
+ * Origin for api.x requests.
23
+ *
24
+ * @const {string}
25
+ */
26
+ // const apiOrigin = 'https://snyk.io/api/v1';
27
+ const apiOrigin = 'http://snyk-widget.herokuapp.com' ;
28
+
29
+ router . get ( '/*' , async ( req , res ) => {
30
+ const cachedResponse = cache . get ( req . url ) ;
31
+
32
+ if ( cachedResponse ) {
33
+ res . json ( cachedResponse ) ;
34
+ return ;
35
+ }
36
+
37
+ try {
38
+ const url = `${ apiOrigin } /${ req . params [ 0 ] } ` ;
39
+ const response = await fetch ( url , {
40
+ headers : {
41
+ 'Authorization' : `${ process . env . SNYK_WIDGET_TOKEN } ` ,
42
+ } ,
43
+ } ) ;
44
+ const json = await response . json ( ) ;
45
+ cache . set ( req . url , json ) ;
46
+
47
+ res . json ( json ) ;
48
+ }
49
+ catch ( e ) {
50
+ console . error ( e ) ;
51
+ rollbar . error ( e ) ;
52
+ res . status ( 500 ) . json ( {
53
+ error : 'Error fetching Snyk resource'
54
+ } ) ;
55
+ }
56
+ } ) ;
57
+
58
+ module . exports = router ;
You can’t perform that action at this time.
0 commit comments