-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (110 loc) · 3.37 KB
/
Copy pathindex.js
File metadata and controls
137 lines (110 loc) · 3.37 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
// Requires
const fastify = require('fastify')({ logger: false })
// check if it's test env
const isDevEnv = process.env.npm_lifecycle_event === 'dev'
// if is not a production environment
if (isDevEnv) {
const dotenv = require('dotenv')
dotenv.config()
}
function getConfig() {
return {
PORT: process.env.PORT || 8080,
PREFIX: process.env.VAR_PREFIX || 'URL_',
IS_ONE_LINE_SYNTAX: process.env.IS_ONE_LINE_SYNTAX == 1,
DEBUG: process.env.DEBUG == 1,
ONE_LINE_DELIMITER: process.env.ONE_LINE_DELIMITER || '|',
}
}
function getUrlsFromEnv(config) {
// Get all env keys
const envKeys = Object.keys(process.env)
// Create empty objects
const urls = {}
const temp = {}
// loop through all env variables
for (let i = 0; i < envKeys.length; i++) {
const key = envKeys[i]
const value = process.env[key]
if (key.startsWith(config.PREFIX)) {
let name = key.replace(config.PREFIX, '')
if (config.IS_ONE_LINE_SYNTAX !== true) {
let url = value;
if (key.endsWith('_GO')) {
name = name.replace('_GO', '')
if (!temp[name]) urls[name] = {}
temp[name] = { ...temp[name], go: url }
} else if (key.endsWith('_ID')) {
name = name.replace('_ID', '')
if (!temp[name]) temp[name] = {}
temp[name] = { ...temp[name], id: value }
}
const tempKeys = Object.keys(temp)
for (tempKey of tempKeys) {
const item = temp[tempKey]
if (item.id && item.go) {
urls[item.id] = item.go
}
}
} else {
const ids = value.split(config.ONE_LINE_DELIMITER)
let url = ids.pop()
for (id of ids) {
urls[id] = url
}
}
}
}
return urls
}
function formatUrl(url) {
if (!url.toLowerCase().startsWith("https://") && !url.toLowerCase().startsWith("http://")) {
url = `https://${url}`
}
return url
}
function main() {
// load config
const config = getConfig()
// get urls
const urls = getUrlsFromEnv(config)
if (config.DEBUG) {
console.log(config);
console.log(urls)
}
fastify.get('/', async (request, reply) => {
if (urls["*"]) {
reply.redirect(formatUrl(urls["*"].go))
} else {
reply.code(404).send({
message: 'Not found'
})
}
})
fastify.get("/:id", async (request, reply) => {
const id = request.params.id
if (id === 'debug' && config.DEBUG) {
reply.code(200).send({
message: JSON.stringify({
config, urls
})
})
return
}
if (urls[id]) {
reply.redirect(formatUrl(urls[id]))
return
}
reply.code(404).send({
message: 'Not found'
})
return
})
// start server
fastify.listen({ port: config.PORT }, (err, address) => {
if (err) throw err
console.log(`Server listening on ${config.PORT}`)
})
}
// run app
main()