Skip to content

Commit 67c729e

Browse files
committed
first changes
1 parent e19bfe0 commit 67c729e

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM mhart/alpine-node:4.4
2+
3+
#Create the npm install layer independently
4+
RUN mkdir /app
5+
COPY app/package.json /app
6+
RUN cd /app && npm install
7+
8+
# Add app source files
9+
ADD app /app/
10+
11+
WORKDIR /app
12+
ENV NODE_ENV production
13+
CMD ["node", "newserver.js"]

app/newserver.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var express = require('express');
2+
var pg = require('pg');
3+
var app = express();
4+
5+
app.get('/', function (req, res) {
6+
7+
var conString = "pg://admin:[email protected]:5432/hasuradb";
8+
var client = new pg.Client(conString);
9+
client.connect();
10+
11+
var query = client.query("SELECT * from test");
12+
query.on("row", function (row, result) {
13+
result.addRow(row);
14+
});
15+
query.on("end", function (result) {
16+
console.log(JSON.stringify(result.rows, null, " "));
17+
client.end();
18+
19+
res.send("Data in table 'test':</br>" + JSON.stringify(result.rows, null, " "));
20+
});
21+
query.on("error",function(error) {
22+
console.log(error);
23+
res.send('Yo momma not so fat she crashed this query!');
24+
});
25+
});
26+
27+
app.listen(8080, function () {
28+
console.log('Example app listening on port 8080!');
29+
});

app/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "hasura-express-example",
3+
"version": "1.0.0",
4+
"description": "Demo to show git push build and deploy",
5+
"scripts": {
6+
"start": "node newserver.js"
7+
},
8+
"author": "",
9+
"license": "ISC",
10+
"dependencies": {
11+
"express": "^4.14.0",
12+
"pg": "^6.1.2",
13+
"node-fetch": "^1.6.3"
14+
}
15+
}

app/server.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)