11const express = require ( 'express' ) ;
2- const app = express ( ) ;
3- const lookup = require ( 'country-code-lookup' ) ;
42const morgan = require ( 'morgan' ) ;
53
6- const port = process . env . PORT || 3001 ;
7-
84const { getCountryTable, getJSONData, getJSONDataForCountry } = require ( './lib/byCountry' ) ;
95const { getCompleteTable } = require ( './lib/corona' ) ;
10- const { countryUpperCase, lookupCountry } = require ( './lib/helpers' ) ;
6+ const { lookupCountry } = require ( './lib/helpers' ) ;
7+ const { getLiveUpdates } = require ( './lib/reddit.js' ) ;
8+ const { getWorldoMetersTable } = require ( './lib/worldoMeters.js' ) ;
119
10+ const app = express ( ) ;
11+ const port = process . env . PORT || 3001 ;
12+ const IS_CURL_RE = / \b c u r l \b / im;
1213
1314function errorHandler ( error , res ) {
1415 console . error ( error ) ;
15- return res . send ( 'I am sorry. Something went wrong. Please report it' ) ;
16+ return res . send ( `
17+ I am sorry. Something went wrong. Please report it\n
18+ ${ error . message }
19+ ` ) ;
1620}
1721
22+ app . set ( 'json escape' , true ) ;
23+
1824app . use ( morgan ( ':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms' ) ) ;
25+ app . use ( ( req , res , next ) => {
26+ res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
27+ next ( ) ;
28+ } ) ;
1929
2030app . get ( '/' , ( req , res ) => {
31+ const isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
2132 const format = req . query . format ? req . query . format : '' ;
33+ const minimal = req . query . minimal === 'true' ;
34+ const emojis = req . query . emojis === 'true' ;
35+ const top = req . query . top ? Number ( req . query . top ) : 1000 ;
36+ const source = req . query . source ? Number ( req . query . source ) : 1 ;
37+
38+ if ( source === 2 ) {
39+ return getWorldoMetersTable ( { isCurl, emojis, minimal, top, format} )
40+ . then ( result => {
41+ return res . send ( result ) ;
42+ } ) . catch ( error => errorHandler ( error , res ) ) ;
43+ }
2244
2345 if ( format . toLowerCase ( ) === 'json' ) {
2446 return getJSONData ( ) . then ( result => {
25- res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
2647 return res . json ( result ) ;
2748 } ) . catch ( error => errorHandler ( error , res ) ) ;
2849 }
2950
30- return getCompleteTable ( ) . then ( result => {
31- res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
51+ return getCompleteTable ( { isCurl, emojis, minimal, top } )
52+ . then ( result => {
53+ return res . send ( result ) ;
54+ } ) . catch ( error => errorHandler ( error , res ) ) ;
55+ } ) ;
56+
57+ app . get ( '/updates' , ( req , res ) => {
58+ const isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
59+ const format = req . query . format ? req . query . format : '' ;
60+
61+ if ( format . toLowerCase ( ) === 'json' ) {
62+ return getLiveUpdates ( { json : true , isCurl } ) . then ( result => {
63+ return res . json ( result ) ;
64+ } ) . catch ( error => errorHandler ( error , res ) ) ;
65+ }
66+
67+ return getLiveUpdates ( { json : false , isCurl } ) . then ( result => {
3268 return res . send ( result ) ;
3369 } ) . catch ( error => errorHandler ( error , res ) ) ;
3470} ) ;
3571
3672app . get ( '/:country' , ( req , res ) => {
3773 const { country } = req . params ;
74+ const isCurl = IS_CURL_RE . test ( req . headers [ 'user-agent' ] ) ;
3875 const format = req . query . format ? req . query . format : '' ;
76+ const minimal = req . query . minimal === 'true' ;
77+ const emojis = req . query . emojis === 'true' ;
78+ const source = req . query . source ? Number ( req . query . source ) : 1 ;
3979
40- if ( ! country || 'ALL' === country . toUpperCase ( ) ) {
80+ if ( ! country || country . toUpperCase ( ) === 'ALL' ) {
4181 if ( format . toLowerCase ( ) === 'json' ) {
4282 return getJSONData ( ) . then ( result => {
43- res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
4483 return res . json ( result ) ;
4584 } ) . catch ( error => errorHandler ( error , res ) ) ;
4685 }
4786
48-
49- return getCompleteTable ( ) . then ( result => {
50- res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
51- return res . send ( result ) ;
52- } ) . catch ( error => errorHandler ( error , res ) ) ;
87+ return getCompleteTable ( { isCurl, emojis, minimal } )
88+ . then ( result => {
89+ return res . send ( result ) ;
90+ } ) . catch ( error => errorHandler ( error , res ) ) ;
5391 }
5492
55- let lookupObj = lookupCountry ( country ) ;
93+ const lookupObj = lookupCountry ( country ) ;
5694
5795 if ( ! lookupObj ) {
5896 return res . send ( `
5997 Country not found.
60- Try full country name or country code.
61- Ex :
98+ Try the full country name or country code.
99+ Example :
62100 - /UK: for United Kingdom
63101 - /US: for United States of America.
64- - /India : for India .
102+ - /Italy : for Italy .
65103 ` ) ;
66104 }
67105
106+
68107 const { iso2 } = lookupObj ;
69108
109+ if ( source === 2 ) {
110+ return getWorldoMetersTable ( { countryCode : iso2 , isCurl, emojis, minimal, format } )
111+ . then ( result => {
112+ return res . send ( result ) ;
113+ } ) . catch ( error => errorHandler ( error , res ) ) ;
114+ }
115+
70116 if ( format . toLowerCase ( ) === 'json' ) {
71117 return getJSONDataForCountry ( iso2 ) . then ( result => {
72- res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
73118 return res . json ( result ) ;
74119 } ) . catch ( error => errorHandler ( error , res ) ) ;
75120 }
76121
77- return getCountryTable ( iso2 ) . then ( result => {
78- res . setHeader ( 'Cache-Control' , 's-maxage=900' ) ;
79- return res . send ( result ) ;
80- } ) . catch ( error => errorHandler ( error , res ) ) ;
122+ return getCountryTable ( { countryCode : iso2 , isCurl , emojis , minimal } )
123+ . then ( result => {
124+ return res . send ( result ) ;
125+ } ) . catch ( error => errorHandler ( error , res ) ) ;
81126} ) ;
82127
83-
84- app . listen ( port , ( ) => console . log ( `Running on ${ port } ` ) ) ;
128+ app . listen ( port , ( ) => console . log ( `Running on ${ port } ` ) ) ;
0 commit comments