-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (103 loc) · 3.33 KB
/
index.js
File metadata and controls
124 lines (103 loc) · 3.33 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
require("dotenv").config();
const cors = require("cors");
const express = require("express");
const rateLimit = require("express-rate-limit");
const fs = require("fs");
const path = require("path");
const port = process.env.PORT || 8080;
const app = express();
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100
});
const api_key = process.env.API_KEY;
app.use(express.static("public"));
app.use(limiter);
app.use(cors());
app.get("/", (req, res) => {
const apps = [
{
name: "cat",
endpoint: "https://api.skillzl.dev/cat?key=YOUR_API_KEY"
},
{
name: "dog",
endpoint: "https://api.skillzl.dev/dog?key=YOUR_API_KEY"
},
{
name: "test",
endpoint: "https://api.eres.fun/test"
}
];
res.json(apps);
});
app.get("/cat", (req, res) => {
const key = req.query.key;
const result = {}
result.code = 200;
const imageList = fs.readdirSync(path.join(__dirname, ".", "public", "cat"));
const randomImage = imageList[Math.floor(Math.random() * imageList.length)];
if (!imageList.length) {
result.code = 404;
result.url = `error: no images available`;
} else {
result.url = `https://api.skillzl.dev/cat/${randomImage}`;
result.key = key;
res.header("Content-type", "application/json; charset=utf-8");
}
res.header("Content-type", "application/json; charset=utf-8");
if (key === api_key) {
res.send(JSON.stringify(result, null, 2));
console.log(result);
} else {
const result = {
code: 403,
message: 'error: invalid API key, try again',
};
res.send(JSON.stringify(result, null, 2));
}
})
app.get("/dog", (req, res) => {
const key = req.query.key;
const result = {}
result.code = 200;
const imageList = fs.readdirSync(path.join(__dirname, ".", "public", "dog"));
const randomImage = imageList[Math.floor(Math.random() * imageList.length)];
if (!imageList.length) {
result.code = 404;
result.url = `error: no images available`;
} else {
result.url = `https://api.skillzl.dev/dog/${randomImage}`;
result.key = key;
res.header("Content-type", "application/json; charset=utf-8");
}
res.header("Content-type", "application/json; charset=utf-8");
if (key === api_key) {
res.send(JSON.stringify(result, null, 2));
console.log(result);
} else {
const result = {
code: 403,
message: 'error: invalid API key, try again',
};
res.send(JSON.stringify(result, null, 2));
}
});
app.get("/test", (req, res) => {
const result = {};
result.code = 200;
const imageList = fs.readdirSync(path.join(__dirname, ".", "public", "test"));
const randomImage = imageList[Math.floor(Math.random() * imageList.length)];
if (!imageList.length) {
result.code = 404
result.url = `error: no images available`
} else {
result.url = `https://api.skillzl.dev/test/${randomImage}`
res.header("Content-type", "application/json; charset=utf-8")
}
res.send(JSON.stringify(result, null, 2));
console.log(result);
})
app.listen(port, "0.0.0.0", function () {
console.log(`Server listening on port ${port}\n`);
});