|
| 1 | +var express = require('express'); |
| 2 | +var app = express(); |
| 3 | +var fetch = require('node-fetch'); |
| 4 | + |
| 5 | +var DEVELOPMENT = (process.env.NODE_ENV == 'production') ? false : true; |
| 6 | + |
| 7 | +// Talking to the database |
| 8 | +var headers = {'Content-Type': 'application/json'}; |
| 9 | +var url; |
| 10 | + |
| 11 | +// When developing locally, need to access data APIs |
| 12 | +// as if admin |
| 13 | +if (DEVELOPMENT) { |
| 14 | + headers.Authorization = 'Bearer ' + process.env.ADMIN_TOKEN; |
| 15 | + url = `https://data.${process.env.PROJECT_NAME}.hasura-app.io`; |
| 16 | +} else { |
| 17 | + url = 'http://data.hasura'; |
| 18 | +} |
| 19 | + |
| 20 | +// Make a request to the data API as the admin role for full access |
| 21 | +headers['X-Hasura-Role'] = 'admin'; |
| 22 | +headers['X-Hasura-User-Id'] = 1; |
| 23 | + |
| 24 | +app.get('/', function (req, res) { |
| 25 | + var schemaFetchUrl = url + '/v1/query'; |
| 26 | + var options = { |
| 27 | + method: 'POST', |
| 28 | + headers, |
| 29 | + body: JSON.stringify({ |
| 30 | + type: 'select', |
| 31 | + args: { |
| 32 | + table: { |
| 33 | + schema: 'hdb_catalog', |
| 34 | + name: 'hdb_table' |
| 35 | + }, |
| 36 | + columns: ['*.*'], |
| 37 | + where: { table_schema: 'public' } |
| 38 | + }}) |
| 39 | + }; |
| 40 | + fetch(schemaFetchUrl, options) |
| 41 | + .then( |
| 42 | + (response) => { |
| 43 | + response.text() |
| 44 | + .then( |
| 45 | + (data) => { |
| 46 | + res.send(data); |
| 47 | + }, |
| 48 | + (e) => { |
| 49 | + res.send('Error in fetching current schema: ' + err.toString()); |
| 50 | + }) |
| 51 | + .catch((e) => { |
| 52 | + e.stack(); |
| 53 | + res.send('Error in fetching current schema: ' + e.toString()); |
| 54 | + }); |
| 55 | + }, |
| 56 | + (e) => { |
| 57 | + console.error(e); |
| 58 | + res.send('Error in fetching current schema: ' + e.toString()); |
| 59 | + }) |
| 60 | + .catch((e) => { |
| 61 | + e.stackTrace(); |
| 62 | + res.send('Error in fetching current schema: ' + e.toString()); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +/* |
| 67 | + * Sample endpoint to check the role of a user |
| 68 | + * When any user makes a request to this endpoint with |
| 69 | + * the path containing the roleName. Eg: /admin, /user, /anonymous |
| 70 | + * that path only gets served, if the user actually has that role. |
| 71 | + * To test, login to the console as an admin user. /admin, /user will work. |
| 72 | + * Make a request to /admin, /user from an incognito tab. They won't work, only /anonymous will work. |
| 73 | + */ |
| 74 | +app.get('/:role', function (req, res) { |
| 75 | + var roles = req.get('X-Hasura-Allowed-Roles'); |
| 76 | + |
| 77 | + // Check if allowed roles contains the rolename mentioned in the URL |
| 78 | + if (roles.indexOf(req.params.role) > -1) { |
| 79 | + res.send('Hey, you have the <b>' + req.params.role + '</b> role'); |
| 80 | + } else { |
| 81 | + res.status(403).send('DENIED: Only a user with the role <b>' + req.params.role + '</b> can access this endpoint'); |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +app.listen(8080, function () { |
| 86 | + console.log('Example app listening on port 8080!'); |
| 87 | +}); |
0 commit comments