-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
64 lines (59 loc) · 2.22 KB
/
index.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
const express = require('express');
var cors = require('cors');
var app = express();
const Base64 = require('base-64');
const KEY = process.env.GITHUB_TOKEN
app.use(cors())
const {
default: Axios
} = require('axios');
app.get("/headers", async (req, res) => {
const headers = req.headers;
return res.json({data: headers['user-agent']})
})
app.get('/weather/:city', async (req, res) => { // Because URL PARAMS ARE COOLER
const city = req.params.city;
await Axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=84f0c05e16abc57b03ca8fa00b59f78e&units=metric`).then(weather => {
return res.json({
description: weather.data.weather[0].description,
temperature: weather.data.main.temp
})
}).catch(e => {
console.log("Cant get weather data lol", e)
return res.status(404).json({
message: "NF"
})
})
})
app.get('/add-sha/:sha', (req, res) => {
const encoded = Base64.encode(1)
Axios.put("https://api.github.com/repos/ujjwal-kr-data/ip-data/contents/" + req.params.sha, {
message: "Added a document",
content: encoded
}, {
headers: { 'Authorization': "token " + KEY }
}).then(result => {
res.json({ visits: 1 })
}).catch(e => {
Axios.get("https://api.github.com/repos/ujjwal-kr-data/ip-data/contents/" + req.params.sha, {
headers: { 'Authorization': "token " + KEY }
}).then(result => {
const contentSHA = result.data.sha;
let visits = Base64.decode(result.data.content)
let newVisit = Base64.encode(parseInt(visits) + 1)
Axios.put("https://api.github.com/repos/ujjwal-kr-data/ip-data/contents/" + req.params.sha, {
message: "UPDATED COUNT",
sha: contentSHA,
content: newVisit
}, { headers: { 'Authorization': "token " + KEY } }).then(done => {
res.json({ visits: Base64.decode(newVisit) })
})
.catch(e => console.log(e))
})
.catch(e => { console.log(e) })
})
})
const port = process.env.PORT || 4000;
app.listen(port, async () => {
console.log("Listening on http://localhost:4000")
});