-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (65 loc) · 1.83 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const { parse: urlParse } = require('url')
const Koa = require('koa')
const cache = require('koa-incache')
const cors = require('@koa/cors')
const getMatches = require('livesoccertv-parser')
const app = new Koa()
const defaultTimezone = 'Europe/Paris'
const getFolderFromUrl = (url, index) =>
url
.split('?')[0]
.split('/')[index]
const getCountryFromUrl = url => getFolderFromUrl(url, 1)
const getTeamFromUrl = url => getFolderFromUrl(url, 2)
const getDataFromUrl = (url) => {
const country = getCountryFromUrl(url)
const team = getTeamFromUrl(url)
let timezone = defaultTimezone
const { query } = urlParse(url, true)
if (query && query.timezone) {
timezone = query.timezone
}
return { country, team, timezone }
}
const validDomains = [ 'https://tvrealmadrid.com', 'https://tvarsenal.com' ]
if (typeof process.env.ACCEPT === 'string') {
validDomains.push(process.env.ACCEPT)
}
app.use(cors({
origin (ctx) {
if (validDomains.includes(ctx.request.header.origin)) {
return ctx.request.header.origin
}
return validDomains[0]
}
}))
app.use(cors({
origin () { return '*' } }))
app.use(cache({maxAge: 1 * 60 * 1000})) // 1 minute cache
app.use(async (ctx, next) => {
if (ctx.path.includes('favicon')) {
ctx.throw(404)
} else if (ctx.path === '') {
ctx.throw(404)
} else if (ctx.path === '/') {
ctx.throw(404)
} else {
await next()
}
})
app.use(async ctx => {
const { country, team, timezone } = getDataFromUrl(ctx.url)
if (!country || !team || !timezone) {
ctx.throw(404)
return
}
const matches = await getMatches(country, team, {timezone})
ctx.cached({matches})
ctx.body = {matches}
})
if (process.env.NODE_ENV === 'development') {
const port = process.env.PORT || 3000
app.listen(port)
console.log('Listening on', port)
}
module.exports = app.callback()