-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (80 loc) · 2.48 KB
/
app.js
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
import express from "express";
import http from "http";
import path from "path";
import cookieParser from "cookie-parser";
import logger from "morgan";
import { config } from "dotenv";
import { default as axios } from "axios";
import { Client } from "@notionhq/client";
import { useNotionLoader } from "./utils/notionLoader.js";
config();
const app = express();
const port = process.env.PORT || 3000;
const httpServer = http.createServer(app);
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.set("trust proxy", true);
app.set("views", path.join(process.cwd(), "views"));
app.set("view engine", "ejs");
/*
Routes
*/
app.get("/", async (req, res) => {
const GEOLOCATION_API_KEY = process.env.GEOLOCATION_API_KEY;
const ip = req.ip;
const { data } = await axios.get(
`https://ipgeolocation.abstractapi.com/v1/?api_key=${GEOLOCATION_API_KEY}&ip_address=${ip}`
);
console.log(data);
res.json(data);
});
// app.get("/", async (req, res) => {
// let documents = [];
// if (req.query.code) {
// const { code } = req.query;
// const clientId = process.env.NOTION_CLIENT_ID;
// const clientSecret = process.env.NOTION_CLIENT_SECRET;
// const redirectURL = process.env.NOTION_REDIRECT_URL;
// // encode in base 64
// const encoded = Buffer.from(`${clientId}:${clientSecret}`).toString(
// "base64"
// );
// const response = await axios.post(
// "https://api.notion.com/v1/oauth/token",
// {
// grant_type: "authorization_code",
// code: code,
// redirect_uri: redirectURL,
// },
// {
// headers: {
// Accept: "application/json",
// "Content-Type": "application/json",
// Authorization: `Basic ${encoded}`,
// },
// }
// );
// const { access_token } = await response.data;
// const notion = new Client({ auth: access_token });
// const { results } = await notion.search();
// documents = await useNotionLoader(access_token, results);
// }
// res.render("index", {
// documents,
// });
// });
app.get("/auth/notion/callback", async (req, res) => {
if (req.query.error) return res.sendStatus(401);
const { code } = req.query;
res.redirect("/?code=" + code);
});
/*
Static Files
*/
app.use("/api/public", express.static(path.join(process.cwd(), "public")));
httpServer.listen(port, () =>
console.log(`Server is listening on port ${port}`)
);
export default app;