File size: 10,111 Bytes
2e61727
 
 
 
 
 
4c2631d
 
 
2e61727
 
 
4c2631d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e61727
4c2631d
 
 
 
 
 
 
 
 
 
 
 
 
2e61727
 
4c2631d
 
 
 
 
 
 
2e61727
4c2631d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e61727
 
4c2631d
2e61727
 
 
 
 
 
4c2631d
2e61727
 
 
4c2631d
2e61727
 
 
 
 
 
 
 
 
4c2631d
 
 
 
2e61727
 
 
 
4c2631d
 
 
 
 
 
 
 
 
 
 
 
 
2e61727
ff883f5
2e61727
 
ff883f5
2e61727
ff883f5
2e61727
 
 
 
ff883f5
2e61727
ff883f5
 
 
 
 
 
 
 
 
2e61727
 
 
 
 
ff883f5
2e61727
ff883f5
 
2e61727
 
 
 
 
ff883f5
 
 
 
 
 
 
 
2e61727
ff883f5
 
 
 
 
2e61727
 
 
 
 
 
 
 
 
 
 
 
 
4c2631d
 
 
 
 
 
 
 
 
ff883f5
2e61727
ff883f5
2e61727
ff883f5
4c2631d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e61727
 
 
4c2631d
2e61727
4c2631d
2e61727
 
 
 
4c2631d
2e61727
4c2631d
2e61727
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const logger = require("koa-logger");
const responseTime = require("koa-response-time");
const bodyParser = require("koa-bodyparser");
const Router = require("koa-router");
const Koa = require("koa");
const os = require("os");
const fs = require("fs");
const https = require("https");
const path = require("path");

const app = new Koa();

// Daftar assets yang perlu didownload
const requiredAssets = [
  {
    url: "https://raw.githubusercontent.com/LyoSU/quote-api/master/assets/pattern_02.png",
    dest: "assets/pattern_02.png",
  },
  {
    url: "https://raw.githubusercontent.com/LyoSU/quote-api/master/assets/pattern_ny.png",
    dest: "assets/pattern_ny.png",
  },
  {
    url: "https://raw.githubusercontent.com/LyoSU/quote-api/master/assets/pattern_ny_old.png",
    dest: "assets/pattern_ny_old.png",
  },
  {
    url: "https://raw.githubusercontent.com/LyoSU/quote-api/master/assets/emoji/emoji-apple-image.json",
    dest: "assets/emoji/emoji-apple-image.json",
  },
  {
    url: "https://raw.githubusercontent.com/LyoSU/quote-api/master/assets/emoji/emoji-google-image.json",
    dest: "assets/emoji/emoji-google-image.json",
  },
  {
    url: "https://raw.githubusercontent.com/LyoSU/quote-api/master/assets/emoji/emoji-joypixels-image.json",
    dest: "assets/emoji/emoji-joypixels-image.json",
  },
  {
    url: "https://raw.githubusercontent.com/LyoSU/quote-api/master/assets/emoji/emoji-twitter-image.json",
    dest: "assets/emoji/emoji-twitter-image.json",
  },
];

// Fungsi untuk download file
function downloadFile(url, dest) {
  return new Promise((resolve, reject) => {
    const dir = path.dirname(dest);
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir, { recursive: true });
    }

    if (fs.existsSync(dest)) {
      console.log(`βœ“ ${dest} already exists`);
      return resolve();
    }

    const file = fs.createWriteStream(dest);
    console.log(`⏬ Downloading ${path.basename(dest)}...`);

    https
      .get(url, (response) => {
        if (response.statusCode === 301 || response.statusCode === 302) {
          return downloadFile(response.headers.location, dest)
            .then(resolve)
            .catch(reject);
        }

        if (response.statusCode !== 200) {
          reject(
            new Error(`Failed to download ${url}: ${response.statusCode}`)
          );
          return;
        }

        response.pipe(file);

        file.on("finish", () => {
          file.close();
          console.log(`βœ… Downloaded ${dest}`);
          resolve();
        });
      })
      .on("error", (err) => {
        fs.unlink(dest, () => {});
        reject(err);
      });

    file.on("error", (err) => {
      fs.unlink(dest, () => {});
      reject(err);
    });
  });
}

// Download semua assets yang diperlukan
async function ensureAssets() {
  console.log("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
  console.log("πŸ” Checking required assets...");
  console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

  try {
    for (const asset of requiredAssets) {
      await downloadFile(asset.url, asset.dest);
    }
    console.log("\n✨ All assets ready!\n");
  } catch (err) {
    console.error("\n❌ Error downloading assets:");
    console.error(err.message);
    throw err;
  }
}

// Custom logger untuk melihat request body dan error detail
app.use(async (ctx, next) => {
  const start = Date.now();

  // Log request
  console.log("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
  console.log("πŸ“¨ INCOMING REQUEST");
  console.log("Time:", new Date().toISOString());
  console.log("Method:", ctx.method);
  console.log("URL:", ctx.url);

  if (ctx.request.body && Object.keys(ctx.request.body).length > 0) {
    console.log("Body:", JSON.stringify(ctx.request.body, null, 2));
  }

  try {
    await next();

    // Log successful response
    const ms = Date.now() - start;
    console.log("\nβœ… RESPONSE SUCCESS");
    console.log("Status:", ctx.status);
    console.log("Time:", ms + "ms");
    console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
  } catch (err) {
    const ms = Date.now() - start;

    // Log error dengan detail lengkap
    console.log("\n❌ ERROR OCCURRED");
    console.log("Time:", new Date().toISOString());
    console.log("Duration:", ms + "ms");
    console.log("Method:", ctx.method);
    console.log("URL:", ctx.url);
    console.log("Status:", err.status || err.statusCode || 500);
    console.log("Error Name:", err.name);
    console.log("Error Message:", err.message);

    if (err.errors) {
      console.log("Validation Errors:", JSON.stringify(err.errors, null, 2));
    }

    console.log("\nπŸ“š STACK TRACE:");
    console.log(err.stack);
    console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    // Set response untuk client
    ctx.status = err.status || err.statusCode || 500;
    ctx.body = {
      success: false,
      error: {
        message: err.message || "Internal Server Error",
        status: ctx.status,
        timestamp: new Date().toISOString(),
        ...(err.errors && { details: err.errors }),
      },
    };

    // Emit error event
    ctx.app.emit("error", err, ctx);
  }
});

app.use(logger());
app.use(responseTime());
app.use(
  bodyParser({
    onerror: (err, ctx) => {
      console.error("\n⚠️  BODY PARSER ERROR");
      console.error("Message:", err.message);
      console.error("Stack:", err.stack);
      console.error("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
      ctx.throw(422, "Invalid JSON body");
    },
  })
);

// Tambahkan middleware untuk log response body pada error
app.use(async (ctx, next) => {
  await next();

  // Log response body jika status error (4xx atau 5xx)
  if (ctx.status >= 400) {
    console.log("\n⚠️  ERROR RESPONSE BODY:");
    console.log(JSON.stringify(ctx.body, null, 2));
    console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
  }
});

app.use(require("./helpers").helpersApi);

const route = new Router();
const routes = require("./routes");

// Helper untuk format memory
function formatBytes(bytes) {
  const sizes = ["B", "KB", "MB", "GB", "TB"];
  if (bytes === 0) return "0 B";
  const i = Math.floor(Math.log(bytes) / Math.log(1024));
  return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
}

function formatUptime(seconds) {
  const days = Math.floor(seconds / (24 * 3600));
  seconds %= 24 * 3600;
  const hours = Math.floor(seconds / 3600);
  seconds %= 3600;
  const minutes = Math.floor(seconds / 60);
  seconds = Math.floor(seconds % 60);

  const parts = [];
  if (days) parts.push(`${days} day${days > 1 ? "s" : ""}`);
  if (hours) parts.push(`${hours} hour${hours > 1 ? "s" : ""}`);
  if (minutes) parts.push(`${minutes} minute${minutes > 1 ? "s" : ""}`);
  if (seconds || parts.length === 0)
    parts.push(`${seconds} second${seconds > 1 ? "s" : ""}`);

  return parts.join(" ");
}

route.get(["/", "/ping"], async (ctx) => {
  const totalMem = os.totalmem();
  const freeMem = os.freemem();
  const usedMem = totalMem - freeMem;
  const usagePercent = ((usedMem / totalMem) * 100).toFixed(2);

  const info = {
    hostname: os.hostname(),
    platform: os.platform(),
    arch: os.arch(),
    uptime: formatUptime(os.uptime()),
    cpu: {
      model: os.cpus()[0].model,
      cores: os.cpus().length,
    },
    memory: {
      total: formatBytes(totalMem),
      used: formatBytes(usedMem),
      free: formatBytes(freeMem),
      usage: `${usagePercent}%`,
    },
  };

  ctx.type = "application/json";
  ctx.body = JSON.stringify(info, null, 2);
});

route.use("/*", routes.routeApi.routes());
app.use(route.routes());

// Global error event listener
app.on("error", (err, ctx) => {
  // Additional logging bisa ditambahkan di sini
  // Misal: kirim ke Sentry, CloudWatch, dll
  if (process.env.DEBUG) {
    console.log("πŸ” Additional error context:", {
      url: ctx.url,
      method: ctx.method,
      status: ctx.status,
      headers: ctx.headers,
    });
  }
});

const port = process.env.PORT || 7860;

// Start server dengan download assets dulu
async function startServer() {
  try {
    // Download assets dulu sebelum start server
    await ensureAssets();

    // Baru start server
    app.listen(port, () => {
      console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
      console.log("βœ… Server started successfully");
      console.log("πŸ“ Listening on localhost, port", port);
      console.log("🌐 Health check: http://localhost:" + port + "/ping");
      console.log("πŸ› Debug mode:", process.env.DEBUG ? "ON" : "OFF");
      console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
    });
  } catch (err) {
    console.error("❌ Failed to start server:", err.message);
    process.exit(1);
  }
}

// Jalankan server
startServer();

// Handle unhandled promise rejections
process.on("unhandledRejection", (reason, promise) => {
  console.error("\nπŸ”₯ Unhandled Rejection at:", promise);
  console.error("Reason:", reason);
  console.error("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
});

// Handle uncaught exceptions
process.on("uncaughtException", (err) => {
  console.error("\nπŸ’₯ Uncaught Exception:");
  console.error(err);
  console.error("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
  process.exit(1);
});