Browse Source

Got github's api to work by passing it through out backend.

pull/11/head
Jeffery Russell 5 years ago
parent
commit
70e9222adf
6 changed files with 97 additions and 3 deletions
  1. +5
    -0
      configManager.js
  2. +2
    -1
      package.json
  3. +1
    -1
      public/js/githubAPI.js
  4. +70
    -0
      routes/api.js
  5. +17
    -0
      routes/index.js
  6. +2
    -1
      server.js

+ 5
- 0
configManager.js View File

@ -33,6 +33,11 @@ module.exports=
getSessionSecret: function() getSessionSecret: function()
{ {
return config.sessionSecret; return config.sessionSecret;
},
getAPIUser: function()
{
return config.user;
} }
}; };

+ 2
- 1
package.json View File

@ -20,6 +20,7 @@
"dependencies": { "dependencies": {
"express": "^4.16.4", "express": "^4.16.4",
"express-session": "^1.15.6", "express-session": "^1.15.6",
"fs": "0.0.1-security"
"fs": "0.0.1-security",
"got": "^9.6.0"
} }
} }

+ 1
- 1
public/js/githubAPI.js View File

@ -7,7 +7,7 @@
*/ */
const APIROOT = "https://api.github.com";
const APIROOT = "api";
const API_USER_PATH = "/users/"; const API_USER_PATH = "/users/";

+ 70
- 0
routes/api.js View File

@ -0,0 +1,70 @@
const routes = require('express').Router();
const got = require("got");
const GITHUB_API = "https://api.github.com";
const configLoader = require('../configManager');
const authenticate = "?client_id=" + configLoader.getClientID() +
"&client_secret=" + configLoader.getClientSecret();
function queryGitHubAPI(requestURL)
{
return new Promise(function(reject, resolve)
{
const queryRUL = GITHUB_API + requestURL + authenticate;
got(queryRUL, { json: true }).then(response =>
{
resolve(response.body);
}).catch(error =>
{
resolve(response.body)
});
})
}
//https://api.github.com/users/whatever?client_id=xxxx&client_secret=yyyy
// function authenticateWithGitHub()
// {
// const authURL = GITHUB_API + "/users/" + configLoader.getAPIUser() + "?client_id=" + configLoader.getClientID() +
// "&client_secret=" + configLoader.getClientSecret();
//
// return new Promise(function(resolve, reject)
// {
// got(authURL, { json: true }).then(response =>
// {
// console.log(response);
// resolve(response);
// }).catch(error => {
// reject(error);
// console.log(error.response.body);
// });
// })
//
// }
routes.get('/*', (request, result) =>
{
const gitHubAPIURL = request.url;
queryGitHubAPI(gitHubAPIURL).then(function(data)
{
result.write(JSON.stringify(data));
result.end();
}).catch(function(error)
{
result.write(JSON.stringify(error));
result.end();
})
});
module.exports = routes;

+ 17
- 0
routes/index.js View File

@ -0,0 +1,17 @@
const routes = require('express').Router();
const api = require('./api');
routes.use('/api', api);
routes.get("/", (request, result) =>
{
result.redirect("index.html");
});
routes.get('*', (request, result) =>
{
result.redirect("404.html");
});
module.exports = routes;

+ 2
- 1
server.js View File

@ -21,12 +21,13 @@ app.use(express.json());
app.use(express.static('public')); app.use(express.static('public'));
const routes = require('./routes'); const routes = require('./routes');
app.use('/', routes); app.use('/', routes);
app.listen(configLoader.getConfiguration().port, () => app.listen(configLoader.getConfiguration().port, () =>
console.log(`App listening on port ${configLoader.getPort()}!`) console.log(`App listening on port ${configLoader.getPort()}!`)
); );

Loading…
Cancel
Save