CiEL7 commited on
Commit
7de9db0
·
verified ·
1 Parent(s): 0f74ccc

Rename fast.js to index.js

Browse files
Files changed (2) hide show
  1. fast.js +0 -79
  2. index.js +34 -0
fast.js DELETED
@@ -1,79 +0,0 @@
1
- const fastify = require('fastify')({ logger: true });
2
- const fs = require('fs');
3
- const path = require('path');
4
- const chokidar = require('chokidar');
5
-
6
- const pluginsDir = path.join(__dirname, 'plugins');
7
-
8
- // 1. Registrasi Multipart (WAJIB untuk POST file/OCR)
9
- fastify.register(require('@fastify/multipart'), {
10
- limits: { fileSize: 10 * 1024 * 1024 } // Batas 10MB
11
- });
12
-
13
- // 2. Registrasi EJS
14
- fastify.register(require('@fastify/view'), {
15
- engine: { ejs: require('ejs') },
16
- root: pluginsDir
17
- });
18
-
19
- let availablePlugins = [];
20
-
21
- async function loadPlugins() {
22
- // Membersihkan rute lama agar tidak duplikat saat reload (Opsional: Restart server lebih aman)
23
- availablePlugins = [];
24
- if (!fs.existsSync(pluginsDir)) return;
25
-
26
- const files = fs.readdirSync(pluginsDir);
27
- files.forEach(file => {
28
- if (file.endsWith('.js')) {
29
- const filePath = path.join(pluginsDir, file);
30
- delete require.cache[require.resolve(filePath)];
31
-
32
- try {
33
- const plugin = require(filePath);
34
- if (plugin.path && plugin.handler) {
35
- // Daftarkan route
36
- fastify.route({
37
- method: plugin.method || 'GET',
38
- url: plugin.path,
39
- handler: plugin.handler
40
- });
41
-
42
- availablePlugins.push({
43
- name: file.replace('.js', ''),
44
- path: plugin.path,
45
- method: plugin.method || 'GET',
46
- description: plugin.description || 'No description'
47
- });
48
- }
49
- } catch (err) {
50
- console.error(`Error loading ${file}:`, err.message);
51
- }
52
- }
53
- });
54
- }
55
-
56
- fastify.get('/', async (request, reply) => {
57
- return reply.view('index.ejs', { plugins: availablePlugins });
58
- });
59
-
60
- const start = async () => {
61
- try {
62
- await loadPlugins();
63
- // Port 7860 untuk Hugging Face, atau 3000 untuk lokal
64
- const port = process.env.PORT || 7860;
65
- await fastify.listen({ port: port, host: '0.0.0.0' });
66
- console.log(`Server running on port ${port}`);
67
- } catch (err) {
68
- fastify.log.error(err);
69
- process.exit(1);
70
- }
71
- };
72
-
73
- chokidar.watch(pluginsDir, { ignoreInitial: true }).on('all', () => {
74
- console.log("Plugins changed, reloading...");
75
- // Catatan: Fastify tidak mendukung penghapusan route secara native tanpa plugin tambahan.
76
- // Untuk hasil terbaik di produksi, proses harus di-restart.
77
- });
78
-
79
- start();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
index.js ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import express from 'express';
2
+ import { solveCloudflare } from './solve.js'; // Asumsikan kode kamu disimpan di solve.js
3
+
4
+ const app = express();
5
+ app.use(express.json());
6
+
7
+ const PORT = process.env.PORT || 7860;
8
+
9
+ app.get('/', (req, res) => {
10
+ res.send({ status: 'Server is running' });
11
+ });
12
+
13
+ app.post('/solve', async (req, res) => {
14
+ const { url } = req.body;
15
+ if (!url) {
16
+ return res.status(400).json({ error: 'URL parameter is required' });
17
+ }
18
+
19
+ try {
20
+ // Panggil fungsi solveCloudflare
21
+ const result = await solveCloudflare({
22
+ url: url,
23
+ headless: 'shell', // Atau false (karena puppeteer-real-browser akan auto-handle Xvfb)
24
+ timeout: 60
25
+ });
26
+ res.json(result);
27
+ } catch (error) {
28
+ res.status(500).json({ error: error.message });
29
+ }
30
+ });
31
+
32
+ app.listen(PORT, '0.0.0.0', () => {
33
+ console.log(`Server listening on port ${PORT}`);
34
+ });