CiEL7 commited on
Commit
3850789
·
verified ·
1 Parent(s): 0ac7ecf

Update fast.js

Browse files
Files changed (1) hide show
  1. fast.js +70 -70
fast.js CHANGED
@@ -1,71 +1,71 @@
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
- // Registrasi EJS
9
- fastify.register(require('@fastify/view'), {
10
- engine: { ejs: require('ejs') },
11
- root: path.join(__dirname, 'plugins') // Template ditaruh di folder plugins
12
- });
13
-
14
- let availablePlugins = [];
15
-
16
- async function loadPlugins() {
17
- availablePlugins = [];
18
- if (!fs.existsSync(pluginsDir)) return;
19
-
20
- const files = fs.readdirSync(pluginsDir);
21
-
22
- files.forEach(file => {
23
- if (file.endsWith('.js')) {
24
- const filePath = path.join(pluginsDir, file);
25
- delete require.cache[require.resolve(filePath)];
26
-
27
- try {
28
- const plugin = require(filePath);
29
- if (plugin.path && plugin.handler) {
30
- // Daftarkan route ke Fastify
31
- fastify.route({
32
- method: plugin.method || 'GET',
33
- url: plugin.path,
34
- handler: plugin.handler
35
- });
36
-
37
- // Simpan metadata untuk UI
38
- availablePlugins.push({
39
- name: file.replace('.js', ''),
40
- path: plugin.path,
41
- method: plugin.method || 'GET',
42
- description: plugin.description || 'Tidak ada deskripsi'
43
- });
44
- }
45
- } catch (err) {
46
- console.error(`Error loading ${file}:`, err.message);
47
- }
48
- }
49
- });
50
- }
51
-
52
- // Endpoint Utama untuk dokumentasi EJS
53
- fastify.get('/', async (request, reply) => {
54
- return reply.view('index.ejs', { plugins: availablePlugins });
55
- });
56
-
57
- const start = async () => {
58
- try {
59
- await loadPlugins();
60
- await fastify.listen({ port: 3000, host: '0.0.0.0' });
61
- console.log('Dokumentasi manual ready di http://localhost:3000');
62
- } catch (err) {
63
- fastify.log.error(err);
64
- process.exit(1);
65
- }
66
- };
67
-
68
- // Auto-reload saat ada file baru
69
- chokidar.watch(pluginsDir, { ignoreInitial: true }).on('add', () => loadPlugins());
70
-
71
  start();
 
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
+ // Registrasi EJS
9
+ fastify.register(require('@fastify/view'), {
10
+ engine: { ejs: require('ejs') },
11
+ root: path.join(__dirname, 'plugins') // Template ditaruh di folder plugins
12
+ });
13
+
14
+ let availablePlugins = [];
15
+
16
+ async function loadPlugins() {
17
+ availablePlugins = [];
18
+ if (!fs.existsSync(pluginsDir)) return;
19
+
20
+ const files = fs.readdirSync(pluginsDir);
21
+
22
+ files.forEach(file => {
23
+ if (file.endsWith('.js')) {
24
+ const filePath = path.join(pluginsDir, file);
25
+ delete require.cache[require.resolve(filePath)];
26
+
27
+ try {
28
+ const plugin = require(filePath);
29
+ if (plugin.path && plugin.handler) {
30
+ // Daftarkan route ke Fastify
31
+ fastify.route({
32
+ method: plugin.method || 'GET',
33
+ url: plugin.path,
34
+ handler: plugin.handler
35
+ });
36
+
37
+ // Simpan metadata untuk UI
38
+ availablePlugins.push({
39
+ name: file.replace('.js', ''),
40
+ path: plugin.path,
41
+ method: plugin.method || 'GET',
42
+ description: plugin.description || 'Tidak ada deskripsi'
43
+ });
44
+ }
45
+ } catch (err) {
46
+ console.error(`Error loading ${file}:`, err.message);
47
+ }
48
+ }
49
+ });
50
+ }
51
+
52
+ // Endpoint Utama untuk dokumentasi EJS
53
+ fastify.get('/', async (request, reply) => {
54
+ return reply.view('index.ejs', { plugins: availablePlugins });
55
+ });
56
+
57
+ const start = async () => {
58
+ try {
59
+ await loadPlugins();
60
+ await fastify.listen({ port: 7860, host: '0.0.0.0' });
61
+ console.log('Dokumentasi manual ready di http://localhost:3000');
62
+ } catch (err) {
63
+ fastify.log.error(err);
64
+ process.exit(1);
65
+ }
66
+ };
67
+
68
+ // Auto-reload saat ada file baru
69
+ chokidar.watch(pluginsDir, { ignoreInitial: true }).on('add', () => loadPlugins());
70
+
71
  start();