-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjadwal.js
More file actions
180 lines (161 loc) · 4.91 KB
/
jadwal.js
File metadata and controls
180 lines (161 loc) · 4.91 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const axios = require('axios');
const NodeCache = require('node-cache');
const cache = new NodeCache({ stdTTL: 3600, checkperiod: 600 });
const BASE_URL = 'https://api.myquran.com/v2/sholat';
exports.handler = async (event, context) => {
try {
const path = event.path.replace('/.netlify/functions/jadwal', '');
const method = event.httpMethod;
// Get all cities
if (path === '/kota/semua' && method === 'GET') {
const cacheKey = 'jadwal_kota_semua';
const cachedData = cache.get(cacheKey);
if (cachedData) {
return {
statusCode: 200,
body: JSON.stringify(cachedData),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
const response = await axios.get(`${BASE_URL}/kota/semua`);
const data = {
status: true,
data: response.data.data
};
cache.set(cacheKey, data);
return {
statusCode: 200,
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
// Get daily prayer schedule (segmented format)
const segmentedMatch = path.match(/^\/kota\/(\d+)\/tahun\/(\d+)\/bulan\/(\d+)\/tanggal\/(\d+)$/);
if (segmentedMatch && method === 'GET') {
const kotaId = segmentedMatch[1];
const tahun = segmentedMatch[2];
const bulan = segmentedMatch[3];
const tanggal = segmentedMatch[4];
const cacheKey = `jadwal_${kotaId}_${tahun}_${bulan}_${tanggal}`;
const cachedData = cache.get(cacheKey);
if (cachedData) {
return {
statusCode: 200,
body: JSON.stringify(cachedData),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
const response = await axios.get(`${BASE_URL}/jadwal/${kotaId}/${tahun}/${bulan}/${tanggal}`);
const data = {
status: true,
data: response.data.data
};
cache.set(cacheKey, data);
return {
statusCode: 200,
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
// Get monthly prayer schedule
const monthlyMatch = path.match(/^\/kota\/(\d+)\/tahun\/(\d+)\/bulan\/(\d+)$/);
if (monthlyMatch && method === 'GET') {
const kotaId = monthlyMatch[1];
const tahun = monthlyMatch[2];
const bulan = monthlyMatch[3];
const cacheKey = `jadwal_bulanan_${kotaId}_${tahun}_${bulan}`;
const cachedData = cache.get(cacheKey);
if (cachedData) {
return {
statusCode: 200,
body: JSON.stringify(cachedData),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
const response = await axios.get(`${BASE_URL}/jadwal/${kotaId}/${tahun}/${bulan}`);
const data = {
status: true,
data: response.data.data
};
cache.set(cacheKey, data);
return {
statusCode: 200,
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
// Alternative daily prayer schedule (single date format)
const singleDateMatch = path.match(/^\/kota\/(\d+)\/tanggal\/(.+)$/);
if (singleDateMatch && method === 'GET') {
const kotaId = singleDateMatch[1];
const date = singleDateMatch[2];
const cacheKey = `jadwal_${kotaId}_${date}`;
const cachedData = cache.get(cacheKey);
if (cachedData) {
return {
statusCode: 200,
body: JSON.stringify(cachedData),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
const response = await axios.get(`${BASE_URL}/jadwal/${kotaId}/${date}`);
const data = {
status: true,
data: response.data.data
};
cache.set(cacheKey, data);
return {
statusCode: 200,
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
return {
statusCode: 404,
body: JSON.stringify({
status: false,
message: 'Endpoint not found'
}),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({
status: false,
message: error.message
}),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
}
};