Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hari #68

Open
wants to merge 133 commits into
base: master
Choose a base branch
from
Open

hari #68

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
d3ea5e8
[imad-console] Updates ui/index.html
coco98 Sep 16, 2016
f0e1fbb
Made some changes locally
coco98 Sep 16, 2016
f726846
[imad-console] Updates server.js
coco98 Sep 17, 2016
5185b6e
Create article-one.html
coco98 Sep 17, 2016
89681e3
[imad-console] Updates server.js
coco98 Sep 17, 2016
b9f57f4
[imad-console] Updates ui/article-one.html
coco98 Sep 17, 2016
ca2569b
[imad-console] Updates ui/article-one.html
coco98 Sep 17, 2016
f1fe8b4
Create article-two.html
coco98 Sep 17, 2016
b84ceb8
Create article-three.html
coco98 Sep 17, 2016
f4342f1
[imad-console] Updates server.js
coco98 Sep 17, 2016
73e3ae4
[imad-console] Updates ui/article-one.html
coco98 Sep 17, 2016
74db34a
[imad-console] Updates ui/article-one.html
coco98 Sep 17, 2016
9ccddc9
[imad-console] Updates ui/article-three.html
coco98 Sep 17, 2016
b0416dd
[imad-console] Updates ui/article-two.html
coco98 Sep 17, 2016
78e87d9
[imad-console] Updates ui/article-three.html
coco98 Sep 17, 2016
c4bb47a
[imad-console] Updates ui/article-two.html
coco98 Sep 17, 2016
49ddbef
[imad-console] Updates ui/article-three.html
coco98 Sep 17, 2016
b9ae3fc
[imad-console] Updates ui/style.css
coco98 Sep 17, 2016
d855a9b
[imad-console] Updates ui/article-one.html
coco98 Sep 17, 2016
d1cf3bc
[imad-console] Updates ui/article-two.html
coco98 Sep 17, 2016
1ac69de
[imad-console] Updates server.js
coco98 Sep 17, 2016
7a59284
[imad-console] Updates ui/style.css
coco98 Sep 17, 2016
7a6265a
[imad-console] Updates server.js
coco98 Sep 17, 2016
aab0fa0
[imad-console] Updates server.js
coco98 Sep 17, 2016
c53428d
Delete article-one.html
coco98 Sep 17, 2016
8ebf6f7
Delete article-two.html
coco98 Sep 17, 2016
98573cb
Delete article-three.html
coco98 Sep 17, 2016
e8b88c0
[imad-console] Updates server.js
coco98 Sep 20, 2016
95d64cb
[imad-console] Updates ui/index.html
coco98 Sep 20, 2016
37de981
[imad-console] Updates server.js
coco98 Sep 20, 2016
3664053
[imad-console] Updates ui/index.html
coco98 Sep 20, 2016
7f5ab96
[imad-console] Updates ui/main.js
coco98 Sep 20, 2016
4c8d5e9
[imad-console] Updates ui/index.html
coco98 Sep 20, 2016
e1bb15e
[imad-console] Updates ui/main.js
coco98 Sep 20, 2016
4d42d7a
[imad-console] Updates ui/main.js
coco98 Sep 20, 2016
02d61ac
[imad-console] Updates ui/main.js
coco98 Sep 20, 2016
95110e2
[imad-console] Updates ui/main.js
coco98 Sep 20, 2016
9c25a10
[imad-console] Updates ui/index.html
coco98 Sep 21, 2016
8ccc23c
[imad-console] Updates ui/index.html
coco98 Sep 21, 2016
80d55cd
[imad-console] Updates server.js
coco98 Sep 21, 2016
22758f2
[imad-console] Updates ui/index.html
coco98 Sep 21, 2016
48f0d88
[imad-console] Updates ui/main.js
coco98 Sep 21, 2016
eba3c47
[imad-console] Updates ui/main.js
coco98 Sep 21, 2016
781472e
[imad-console] Updates ui/main.js
coco98 Sep 21, 2016
f264913
[imad-console] Updates ui/main.js
coco98 Sep 21, 2016
69da610
[imad-console] Updates ui/main.js
coco98 Sep 21, 2016
1783d0a
[imad-console] Updates ui/index.html
coco98 Sep 21, 2016
55b8ad5
[imad-console] Updates ui/index.html
coco98 Sep 21, 2016
e7141d4
[imad-console] Updates ui/main.js
coco98 Sep 21, 2016
d847ea4
[imad-console] Updates server.js
coco98 Sep 21, 2016
cb9e88c
[imad-console] Updates server.js
coco98 Sep 21, 2016
28e1ea1
[imad-console] Updates server.js
coco98 Sep 21, 2016
3bef1bc
[imad-console] Updates server.js
coco98 Sep 21, 2016
a1643e4
[imad-console] Updates server.js
coco98 Sep 21, 2016
222cf9d
[imad-console] Updates server.js
coco98 Sep 21, 2016
c0f3fd1
[imad-console] Updates ui/main.js
coco98 Sep 21, 2016
0fa2f9a
[imad-console] Updates ui/main.js
coco98 Sep 21, 2016
3c9b82f
[imad-console] Updates server.js
coco98 Oct 22, 2016
b369c59
[imad-console] Updates server.js
coco98 Oct 22, 2016
dc09cb0
[imad-console] Updates server.js
coco98 Oct 22, 2016
e85d9e0
[imad-console] Updates server.js
coco98 Oct 22, 2016
b16f2af
[imad-console] Updates server.js
coco98 Oct 22, 2016
920225e
[imad-console] Updates server.js
coco98 Oct 22, 2016
43fe343
[imad-console] Updates server.js
coco98 Oct 22, 2016
a38dd5f
[imad-console] Updates server.js
coco98 Oct 22, 2016
d7ce820
[imad-console] Updates server.js
coco98 Nov 2, 2016
383908b
[imad-console] Updates server.js
coco98 Nov 2, 2016
6181493
[imad-console] Updates server.js
coco98 Nov 3, 2016
e327c73
[imad-console] Updates server.js
coco98 Nov 3, 2016
5114886
[imad-console] Updates server.js
coco98 Nov 3, 2016
09d33f5
[imad-console] Updates server.js
coco98 Nov 3, 2016
4f2faa2
[imad-console] Updates server.js
coco98 Nov 3, 2016
3c4125a
[imad-console] Updates ui/index.html
coco98 Nov 3, 2016
4b07e90
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
bea2ecf
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
f81465f
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
0731884
[imad-console] Updates server.js
coco98 Nov 3, 2016
a98a4e2
[imad-console] Updates server.js
coco98 Nov 3, 2016
8725b8b
[imad-console] Updates server.js
coco98 Nov 3, 2016
b199c72
[imad-console] Updates server.js
coco98 Nov 3, 2016
5084656
[imad-console] Updates server.js
coco98 Nov 3, 2016
60a0372
[imad-console] Updates ui/index.html
coco98 Nov 3, 2016
7b3a45c
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
7405780
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
d87bb9a
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
ac26fa4
[imad-console] Updates ui/index.html
coco98 Nov 3, 2016
ea45541
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
d27640b
[imad-console] Updates ui/index.html
coco98 Nov 3, 2016
127de7c
[imad-console] Updates server.js
coco98 Nov 3, 2016
f51d2ae
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
3be99db
[imad-console] Updates server.js
coco98 Nov 3, 2016
647b6d0
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
fd04629
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
425206b
[imad-console] Updates server.js
coco98 Nov 3, 2016
d837038
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
cd35019
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
9bfd041
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
4533fb0
[imad-console] Updates server.js
coco98 Nov 3, 2016
0b0c056
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
645886e
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
44d4183
[imad-console] Updates ui/index.html
coco98 Nov 3, 2016
5bc2da9
[imad-console] Updates server.js
coco98 Nov 3, 2016
8d11f6d
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
61cdd94
[imad-console] Updates ui/index.html
coco98 Nov 3, 2016
6b4c484
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
2b91c12
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
ded9e4b
[imad-console] Updates ui/index.html
coco98 Nov 3, 2016
18cdcff
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
9314e56
Create article.js
coco98 Nov 3, 2016
35120aa
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
969c0bb
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
3c5add9
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
c6513a6
[imad-console] Updates server.js
coco98 Nov 3, 2016
c136e61
[imad-console] Updates server.js
coco98 Nov 3, 2016
8d6bec1
[imad-console] Updates server.js
coco98 Nov 3, 2016
853afdc
[imad-console] Updates ui/style.css
coco98 Nov 3, 2016
e42dba8
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
f0f65b0
[imad-console] Updates server.js
coco98 Nov 3, 2016
f72324c
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
a839197
[imad-console] Updates ui/style.css
coco98 Nov 3, 2016
4033324
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
727f87e
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
3794ac0
[imad-console] Updates server.js
coco98 Nov 3, 2016
3708e00
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
8722b13
[imad-console] Updates server.js
coco98 Nov 3, 2016
b479575
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
6c85b3c
[imad-console] Updates ui/main.js
coco98 Nov 3, 2016
bc40132
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
0b36095
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
f89207b
[imad-console] Updates ui/article.js
coco98 Nov 3, 2016
84b0ce7
Adds schema
coco98 Nov 3, 2016
ba56e59
[imad-console] Updates server.js
coco98 Nov 19, 2016
5cd93d4
Update server.js
coco98 Nov 21, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
--
-- Name: article; Type: TABLE; Schema: public; Owner: coco98
--

CREATE TABLE article (
id integer NOT NULL,
title text NOT NULL,
heading text NOT NULL,
date date NOT NULL,
content text NOT NULL
);

--
-- Name: article_id_seq; Type: SEQUENCE; Schema: public; Owner: coco98
--

CREATE SEQUENCE article_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

--
-- Name: comment; Type: TABLE; Schema: public; Owner: coco98
--

CREATE TABLE comment (
id integer NOT NULL,
article_id integer NOT NULL,
user_id integer NOT NULL,
comment text NOT NULL,
"timestamp" timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: comment_id_seq; Type: SEQUENCE; Schema: public; Owner: coco98
--

CREATE SEQUENCE comment_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: user; Type: TABLE; Schema: public; Owner: coco98
--

CREATE TABLE "user" (
id integer NOT NULL,
username text NOT NULL,
password text NOT NULL
);


--
-- Name: user_id_seq; Type: SEQUENCE; Schema: public; Owner: coco98
--

CREATE SEQUENCE user_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: id; Type: DEFAULT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY article ALTER COLUMN id SET DEFAULT nextval('article_id_seq'::regclass);


--
-- Name: id; Type: DEFAULT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY comment ALTER COLUMN id SET DEFAULT nextval('comment_id_seq'::regclass);


--
-- Name: id; Type: DEFAULT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY "user" ALTER COLUMN id SET DEFAULT nextval('user_id_seq'::regclass);

--
-- Name: article_id; Type: CONSTRAINT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY article
ADD CONSTRAINT article_id PRIMARY KEY (id);


--
-- Name: article_title; Type: CONSTRAINT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY article
ADD CONSTRAINT article_title UNIQUE (title);


--
-- Name: comment_id; Type: CONSTRAINT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY comment
ADD CONSTRAINT comment_id PRIMARY KEY (id);


--
-- Name: user_id; Type: CONSTRAINT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY "user"
ADD CONSTRAINT user_id PRIMARY KEY (id);


--
-- Name: user_username; Type: CONSTRAINT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY "user"
ADD CONSTRAINT user_username UNIQUE (username);


--
-- Name: comment_article_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY comment
ADD CONSTRAINT comment_article_id_fkey FOREIGN KEY (article_id) REFERENCES article(id);


--
-- Name: comment_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: coco98
--

ALTER TABLE ONLY comment
ADD CONSTRAINT comment_user_id_fkey FOREIGN KEY (user_id) REFERENCES "user"(id);
222 changes: 218 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,234 @@
var express = require('express');
var morgan = require('morgan');
var path = require('path');
var Pool = require('pg').Pool;
var crypto = require('crypto');
var bodyParser = require('body-parser');
var session = require('express-session');

var config = {
user: 'coco98',
database: 'coco98',
host: 'db.imad.hasura-app.io',
port: '5432',
password: process.env.DB_PASSWORD
};

var app = express();
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(session({
secret: 'someRandomSecretValue',
cookie: { maxAge: 1000 * 60 * 60 * 24 * 30}
}));

function createTemplate (data) {
var title = data.title;
var date = data.date;
var heading = data.heading;
var content = data.content;

var htmlTemplate = `
<html>
<head>
<title>
${title}
</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="/ui/style.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div>
<a href="/">Home</a>
</div>
<hr/>
<h3>
${heading}
</h3>
<div>
${date.toDateString()}
</div>
<div>
${content}
</div>
<hr/>
<h4>Comments</h4>
<div id="comment_form">
</div>
<div id="comments">
<center>Loading comments...</center>
</div>
</div>
<script type="text/javascript" src="/ui/article.js"></script>
</body>
</html>
`;
return htmlTemplate;
}

app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'index.html'));
});

app.get('/ui/style.css', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'style.css'));

function hash (input, salt) {
// How do we create a hash?
var hashed = crypto.pbkdf2Sync(input, salt, 10000, 512, 'sha512');
return ["pbkdf2", "10000", salt, hashed.toString('hex')].join('$');
}


app.get('/hash/:input', function(req, res) {
var hashedString = hash(req.params.input, 'this-is-some-random-string');
res.send(hashedString);
});

app.post('/create-user', function (req, res) {
// username, password
// {"username": "tanmai", "password": "password"}
// JSON
var username = req.body.username;
var password = req.body.password;
var salt = crypto.randomBytes(128).toString('hex');
var dbString = hash(password, salt);
pool.query('INSERT INTO "user" (username, password) VALUES ($1, $2)', [username, dbString], function (err, result) {
if (err) {
res.status(500).send(err.toString());
} else {
res.send('User successfully created: ' + username);
}
});
});

app.post('/login', function (req, res) {
var username = req.body.username;
var password = req.body.password;

pool.query('SELECT * FROM "user" WHERE username = $1', [username], function (err, result) {
if (err) {
res.status(500).send(err.toString());
} else {
if (result.rows.length === 0) {
res.status(403).send('username/password is invalid');
} else {
// Match the password
var dbString = result.rows[0].password;
var salt = dbString.split('$')[2];
var hashedPassword = hash(password, salt); // Creating a hash based on the password submitted and the original salt
if (hashedPassword === dbString) {

// Set the session
req.session.auth = {userId: result.rows[0].id};
// set cookie with a session id
// internally, on the server side, it maps the session id to an object
// { auth: {userId }}

res.send('credentials correct!');

} else {
res.status(403).send('username/password is invalid');
}
}
}
});
});

app.get('/check-login', function (req, res) {
if (req.session && req.session.auth && req.session.auth.userId) {
// Load the user object
pool.query('SELECT * FROM "user" WHERE id = $1', [req.session.auth.userId], function (err, result) {
if (err) {
res.status(500).send(err.toString());
} else {
res.send(result.rows[0].username);
}
});
} else {
res.status(400).send('You are not logged in');
}
});

app.get('/logout', function (req, res) {
delete req.session.auth;
res.send('<html><body>Logged out!<br/><br/><a href="/">Back to home</a></body></html>');
});

var pool = new Pool(config);

app.get('/get-articles', function (req, res) {
// make a select request
// return a response with the results
pool.query('SELECT * FROM article ORDER BY date DESC', function (err, result) {
if (err) {
res.status(500).send(err.toString());
} else {
res.send(JSON.stringify(result.rows));
}
});
});

app.get('/get-comments/:articleName', function (req, res) {
// make a select request
// return a response with the results
pool.query('SELECT comment.*, "user".username FROM article, comment, "user" WHERE article.title = $1 AND article.id = comment.article_id AND comment.user_id = "user".id ORDER BY comment.timestamp DESC', [req.params.articleName], function (err, result) {
if (err) {
res.status(500).send(err.toString());
} else {
res.send(JSON.stringify(result.rows));
}
});
});

app.post('/submit-comment/:articleName', function (req, res) {
// Check if the user is logged in
if (req.session && req.session.auth && req.session.auth.userId) {
// First check if the article exists and get the article-id
pool.query('SELECT * from article where title = $1', [req.params.articleName], function (err, result) {
if (err) {
res.status(500).send(err.toString());
} else {
if (result.rows.length === 0) {
res.status(400).send('Article not found');
} else {
var articleId = result.rows[0].id;
// Now insert the right comment for this article
pool.query(
"INSERT INTO comment (comment, article_id, user_id) VALUES ($1, $2, $3)",
[req.body.comment, articleId, req.session.auth.userId],
function (err, result) {
if (err) {
res.status(500).send(err.toString());
} else {
res.status(200).send('Comment inserted!')
}
});
}
}
});
} else {
res.status(403).send('Only logged in users can comment');
}
});

app.get('/articles/:articleName', function (req, res) {
// SELECT * FROM article WHERE title = '\'; DELETE WHERE a = \'asdf'
pool.query("SELECT * FROM article WHERE title = $1", [req.params.articleName], function (err, result) {
if (err) {
res.status(500).send(err.toString());
} else {
if (result.rows.length === 0) {
res.status(404).send('Article not found');
} else {
var articleData = result.rows[0];
res.send(createTemplate(articleData));
}
}
});
});

app.get('/ui/madi.png', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'madi.png'));
app.get('/ui/:fileName', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', req.params.fileName));
});


Expand Down
Loading