This repository was archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
218 lines (182 loc) · 6.43 KB
/
server.js
File metadata and controls
218 lines (182 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const path = require("path");
var mysql = require("mysql");
const axios = require("axios");
var userCourseData;
require('dotenv').config();
const { Client } = require("pg");
// Create a client using the connection information provided on bit.io.
const client = new Client({
user: "CodeTrainer_demo_db_connection",
host: "db.bit.io",
database: "bitdotio",
password: "3J6Aq_zgHd9USQbTExT9jh7uwBDKJ",
port: 5432,
});
client.connect().catch((e) => {});
client.on("error", (e) => {});
// Require the fastify framework and instantiate it
const fastify = require("fastify")({
// set this to true for detailed logging:
logger: false,
});
// Setup our static files
fastify.register(require("fastify-static"), {
root: path.join(__dirname, "public"),
prefix: "/", // optional: default '/'
});
// fastify-formbody lets us parse incoming forms
fastify.register(require("fastify-formbody"));
// point-of-view is a templating manager for fastify
fastify.register(require("point-of-view"), {
engine: {
handlebars: require("handlebars"),
},
});
fastify.get("/", function (request, reply) {
reply.view("/src/pages/index.html", {client_id: process.env.Client_ID});
});
var access_token = "";
//Our callback route.
fastify.get("/github/callback", function (req, res) {
const requestToken = req.query.code;
axios({
method: "post",
url: `https://github.com/login/oauth/access_token?client_id=${process.env.Client_ID}&client_secret=${process.env.Client_Secret}&code=${requestToken}`,
headers: {
accept: "application/json",
},
}).then((response) => {
access_token = response.data.access_token;
res.redirect("/success");
});
});
fastify.get("/success", function (req, res) {
axios({
method: "get",
url: `https://api.github.com/user`,
headers: {
Authorization: "token " + access_token,
},
}).then((response) => {
client.query(
'SELECT courses FROM "CodeTrainer/codetrainermain"."userdata" WHERE EXISTS (SELECT username FROM "CodeTrainer/codetrainermain"."userdata" WHERE username=\'' +
response.data.name +
"');",
(err, respo) => {
if (respo == undefined || respo.rowCount == 0) {
client.query(
'INSERT INTO "CodeTrainer/codetrainermain"."userdata" (username, courses) VALUES (\'' +
response.data.name +
'\', \'{"html": 0}\');',
(err, resp) => {});
} else {
userCourseData = respo.rows[0].courses;
console.log(userCourseData);
}
userCourseData = JSON.stringify(userCourseData);
console.log(userCourseData);
res.view("src/pages/auth.html", {
userData: response.data,
userCourses: userCourseData != undefined ? JSON.stringify(userCourseData) : "{html: 0}",
});
}
);
});
});
fastify.get("/test/code/azh", function (request, reply) {
reply.view("/src/pages/test.html", {client_id: process.env.Client_ID});
});
fastify.get("/en-US/landing", function (request, reply) {
reply.view("/src/pages/en-US/landing.html", {client_id: process.env.Client_ID});
});
fastify.get("/de-DE/landing", function (request, reply) {
// request.query.paramName <-- a querystring example
let params = {
lu: false
};
reply.view("/src/pages/de-DE/landing.html", params);
});
fastify.get("/en-US/explore", function (request, reply) {
reply.view("/src/pages/en-US/explore.html", {client_id: process.env.Client_ID});
});
fastify.get("/en-US/courses/html", function (request, reply) {
// request.query.paramName <-- a querystring example
let params = {
lessonname: ["What is HTML?", "The basics of tags"],
lessondesc: ["Let's go over some use cases as well as what HTML means.", "How do tags work and what are attributes?"]
};
reply.view("/src/pages/en-US/course.html", params);
});
fastify.get("/ga-IE/landing", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/src/pages/ga-IE/landing.html");
});
fastify.get("/en-US/courses/html/:course", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/src/pages/en-US/lessons/"+request.params['course']+".html");
});
fastify.get("/auth", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/src/pages/auth.html");
});
fastify.get("/license", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/src/pages/license.html");
});
fastify.get("/certifications", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/src/pages/certifications/certlanding.html");
});
fastify.get("/certifications/webdeveloper", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/src/pages/certifications/webdevcert.html");
});
fastify.get("/about/terms", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/src/pages/about/termsofservice.html");
});
fastify.get("/tempnav", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/src/pages/navbardesign.html");
});
fastify.get("/trainercss", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("/trainer.css");
});
fastify.get("/trainercss/info", function (request, reply) {
// request.query.paramName <-- a querystring example
reply.view("src/pages/trainercss.html");
});
fastify.get("/lu", function (request, reply) {
// request.query.paramName <-- a querystring example
let params = {
lu: true
};
reply.view("/src/pages/de-DE/landing.html", params);
});
fastify.setNotFoundHandler(function (request, reply) {
reply.view("/src/pages/404.html");
})
fastify.register(function (instance, options, done) {
instance.setNotFoundHandler(function (request, reply) {
// Handle not found request without preValidation and preHandler hooks
// to URLs that begin with '/v1'
})
done()
}, { prefix: '/v1' })
// A POST route to handle form submissions
fastify.post("/", function (request, reply) {
let params = {
greeting: "Hello Form!",
};
// request.body.paramName <-- a form post example
reply.view("/src/pages/index.html", params);
});
// Run the server and report out to the logs
fastify.listen(process.env.PORT, "0.0.0.0", function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
fastify.log.info(`server listening on ${address}`);
});