@@ -3,52 +3,109 @@ const _ = require("lodash");
3
3
const axios = require ( "axios" ) ;
4
4
const express = require ( "express" ) ;
5
5
const cors = require ( "cors" ) ;
6
+ const resumeSchema = require ( "resume-schema" ) ;
6
7
const fs = require ( "fs" ) ;
7
8
const app = express ( ) ;
9
+ // Import Admin SDK
10
+ var admin = require ( "firebase-admin" ) ;
8
11
12
+ if ( process . env . NODE_ENV === "production" ) {
13
+ admin . initializeApp ( functions . config ( ) . firebase ) ;
14
+ } else {
15
+ var serviceAccount = require ( "../creds.json" ) ;
16
+ admin . initializeApp ( {
17
+ credential : admin . credential . cert ( serviceAccount ) ,
18
+ databaseURL : "https://jsonresume-registry.firebaseio.com"
19
+ } ) ;
20
+ }
21
+
22
+ var db = admin . database ( ) ;
9
23
app . use ( cors ( { origin : true } ) ) ;
10
24
25
+ const makeTemplate = message => {
26
+ const template = fs . readFileSync ( __dirname + "/template.html" , "utf8" ) ;
27
+ return template . replace ( "{MESSAGE}" , message ) ;
28
+ } ;
29
+
11
30
app . get ( "/:username" , async ( req , res ) => {
12
31
const username = req . params . username ;
13
- if ( username === "favicon.ico" ) {
32
+ if (
33
+ [
34
+ "favicon.ico" ,
35
+ "competition" ,
36
+ "stats" ,
37
+ "apple-touch-icon.png" ,
38
+ "apple-touch-icon-precomposed.png" ,
39
+ "robots.txt"
40
+ ] . indexOf ( username ) !== - 1
41
+ ) {
14
42
return res . send ( null ) ;
15
43
}
16
- console . log ( `https://api.github.com/users/${ req . params . username } /gists` ) ;
17
- const gistData = await axios . get (
18
- `https://api.github.com/users/${ req . params . username } /gists`
19
- ) ;
20
- if ( ! gistData . data ) {
21
- res . send ( "This username does not exist on Github" ) ;
22
- }
23
- const resumeUrl = _ . find ( gistData . data , f => {
24
- return f . files [ "resume.json" ] ;
25
- } ) ;
26
- if ( ! resumeUrl ) {
27
- res . send ( "You have no gists named resume.json" ) ;
28
- }
29
- const gistId = resumeUrl . id ;
30
- const options =
31
- resumeUrl . description . length > 0 ? JSON . parse ( resumeUrl . description ) : { } ;
32
- const theme = options . theme || "flat" ;
33
- const fullResumeGistUrl = `https://gist.githubusercontent.com/${ username } /${ gistId } /raw/` ;
34
- console . log ( fullResumeGistUrl ) ;
35
- const resumeRes = await axios ( {
36
- method : "GET" ,
37
- headers : { "content-type" : "application/json" } ,
38
- url : fullResumeGistUrl
44
+ var ref = db . ref ( ) ;
45
+ var usersRef = ref . child ( "gists/" + username ) ;
46
+ usersRef . on ( "value" , async dataSnapshot => {
47
+ console . log ( "=======" ) ;
48
+ console . log ( dataSnapshot . val ( ) ) ;
49
+ let gistId ;
50
+ if ( ! dataSnapshot . val ( ) || ! dataSnapshot . val ( ) . gistId ) {
51
+ console . log ( "Fetching gistId" ) ;
52
+ console . log ( `https://api.github.com/users/${ req . params . username } /gists` ) ;
53
+ let gistData = { } ;
54
+ try {
55
+ gistData = await axios . get (
56
+ `https://api.github.com/users/${ req . params . username } /gists`
57
+ ) ;
58
+ } catch ( e ) {
59
+ return res . send ( makeTemplate ( "This is not a valid Github username" ) ) ;
60
+ }
61
+ if ( ! gistData . data ) {
62
+ return res . send ( makeTemplate ( "This is not a valid Github username" ) ) ;
63
+ }
64
+ const resumeUrl = _ . find ( gistData . data , f => {
65
+ return f . files [ "resume.json" ] ;
66
+ } ) ;
67
+ if ( ! resumeUrl ) {
68
+ return res . send ( makeTemplate ( "You have no gists named resume.json" ) ) ;
69
+ }
70
+ gistId = resumeUrl . id ;
71
+ } else {
72
+ console . log ( "Using cached gistId" ) ;
73
+ gistId = dataSnapshot . val ( ) . gistId ;
74
+ }
75
+
76
+ usersRef . set ( { gistId : gistId } , ( ) => { } ) ;
77
+ const fullResumeGistUrl =
78
+ `https://gist.githubusercontent.com/${ username } /${ gistId } /raw?cachebust=` +
79
+ new Date ( ) . getTime ( ) ;
80
+ console . log ( fullResumeGistUrl ) ;
81
+ const resumeRes = await axios ( {
82
+ method : "GET" ,
83
+ headers : { "content-type" : "application/json" } ,
84
+ url : fullResumeGistUrl
85
+ } ) ;
86
+ if ( ! resumeRes . data ) {
87
+ return res . send ( makeTemplate ( "Something went wrong fetching resume" ) ) ;
88
+ }
89
+ resumeSchema . validate ( resumeRes . data , async ( err , report ) => {
90
+ console . log ( "validation finished" ) ;
91
+ if ( err ) {
92
+ console . log ( err ) ;
93
+ return res . send (
94
+ makeTemplate ( "Resume json invalid - " + JSON . stringify ( err ) )
95
+ ) ;
96
+ }
97
+ const theme =
98
+ ( resumeRes . data . meta && resumeRes . data . meta . theme ) || "flat" ;
99
+ const resumeHTMLRes = await axios . post (
100
+ `https://themes.jsonresume.org/theme/${ theme } ` ,
101
+ { resume : resumeRes . data }
102
+ ) ;
103
+ if ( ! resumeHTMLRes . data ) {
104
+ res . send ( "There was an error generatoring your resume" ) ;
105
+ }
106
+ res . send ( resumeHTMLRes . data ) ;
107
+ } ) ;
39
108
} ) ;
40
- if ( ! resumeRes . data ) {
41
- res . send ( "Something went wrong fetching resume" ) ;
42
- }
43
- console . log ( resumeRes ) ;
44
- const resumeHTMLRes = await axios . post (
45
- `https://themes.jsonresume.org/theme/${ theme } ` ,
46
- { resume : resumeRes . data }
47
- ) ;
48
- if ( ! resumeHTMLRes . data ) {
49
- res . send ( "There was an error generatoring your resume" ) ;
50
- }
51
- res . send ( resumeHTMLRes . data ) ;
52
109
} ) ;
53
110
app . listen ( 3000 ) ;
54
111
exports . registry = functions . https . onRequest ( app ) ;
0 commit comments