1+ const express = require ( 'express' ) ;
2+ const router = express . Router ( ) ;
3+ const cors = require ( 'cors' ) ;
4+ const ImageKit = require ( 'imagekit' ) ;
5+ const uuid = require ( 'uuid' ) ;
6+ const fs = require ( 'fs' ) ;
7+ const path = require ( 'path' ) ;
8+
9+
10+ //const html = fs.readFileSync(path.join(__dirname, "../views/index.html"));
11+ const js = fs . readFileSync ( path . join ( __dirname , "../../../dist/imagekit.js" ) ) ;
12+ const pugTemplatePath = path . join ( __dirname , "../views/index.pug" ) ;
13+
14+
15+ const app = express ( ) ;
16+ app . use ( cors ( ) ) ;
17+ app . set ( 'view engine' , 'pug' ) ;
18+
19+ const startServer = ( port = 3000 , PUBLIC_KEY , PRIVATE_KEY , URL_ENDPOINT ) => {
20+ return new Promise ( ( resolve , reject ) => {
21+ try {
22+ const imagekit = new ImageKit ( {
23+ publicKey : PUBLIC_KEY ,
24+ privateKey : PRIVATE_KEY ,
25+ urlEndpoint : URL_ENDPOINT
26+ } ) ;
27+
28+
29+ router . get ( "/auth" , ( req , res ) => {
30+ try {
31+ const token = req . query . token || uuid . v4 ( ) ;
32+ const expiration = req . query . expire || parseInt ( Date . now ( ) / 1000 ) + ( 60 * 10 ) ; // Default expiration in 10 mins
33+
34+ const signatureObj = imagekit . getAuthenticationParameters ( token , expiration ) ;
35+
36+ // Alternate method for genrating signature
37+ /*
38+ const crypto = require('crypto');
39+ const signatureObj = {
40+ token,
41+ expire: expiration,
42+ signature :crypto.createHmac('sha1', privateAPIKey).update(token+expire).digest('hex')
43+ }
44+ */
45+
46+ res . status ( 200 ) . send ( signatureObj ) ;
47+
48+ } catch ( err ) {
49+ console . error ( "Error while responding to auth request:" , JSON . stringify ( err , undefined , 2 ) ) ;
50+ res . status ( 500 ) . send ( "Internal Server Error" ) ;
51+ }
52+ } ) ;
53+
54+ router . get ( "/imagekit.js" , ( req , res ) => {
55+ try {
56+ res . set ( 'Content-Type' , 'text/javascript' ) ;
57+ res . send ( Buffer . from ( js ) ) ;
58+ } catch ( err ) {
59+ console . error ( "Error while responding to static page request:" , JSON . stringify ( err , undefined , 2 ) ) ;
60+ res . status ( 500 ) . send ( "Internal Server Error" ) ;
61+ }
62+ } ) ;
63+
64+ router . get ( "/" , ( req , res ) => {
65+ try {
66+ res . render ( pugTemplatePath , { publicKey : PUBLIC_KEY , urlEndpoint : URL_ENDPOINT , authenticationEndpoint : `http://localhost:3000/auth` } ) ;
67+ } catch ( err ) {
68+ console . error ( "Error while responding to static page request:" , JSON . stringify ( err , undefined , 2 ) ) ;
69+ res . status ( 500 ) . send ( "Internal Server Error" ) ;
70+ }
71+ } ) ;
72+
73+
74+
75+
76+
77+ app . use ( "/" , router ) ;
78+
79+ app . listen ( port , ( ) => {
80+ console . info ( `Auth server running on port ${ port } .` ) ;
81+ resolve ( ) ;
82+ } ) ;
83+ } catch ( err ) {
84+ console . error ( JSON . stringify ( err , undefined , 2 ) ) ;
85+ reject ( "Error starting auth server." )
86+ }
87+
88+ } ) ;
89+ }
90+
91+ module . exports = {
92+ startServer
93+ }
0 commit comments