File size: 2,648 Bytes
4c0b9f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
require("../../../configs")
/**
 * Copyright © 2025 [ balxzzy ]
 *
 * All rights reserved. This source code is the property of [ Shiina Team ].
 * Unauthorized copying, distribution, modification, or use of this file,
 * via any medium, is strictly prohibited without prior written permission.
 *
 * This software is protected under international copyright laws.
 *
 * Contact: [ pa424013@gmail.com ]
 * GitHub: https://github.com/balxz
 */

let home = path.join(__dirname, "../../")
let mainten  = path.join(home, "pages/maintenance/index.html")

exports.loadRoutes = async function loadRoutes(dir) {
    for (const file of fs.readdirSync(dir)) {
        let filePath = path.join(dir, file)
        if (fs.statSync(filePath).isDirectory()) {
            loadRoutes(filePath)
            continue
        }

        try {
            delete require.cache[require.resolve(filePath)]
            let route = require(filePath)
            if (!route.path || !route.type) {
                console.warn(chalk.red(`[ ROUTE ERROR ] ${file} - Missing "path" or "type"`))
                continue
            }

            const {
                path: routePath,
                type,
                isDisable = false,
                hidden = false,
                code
            } = route

            let endpoint = routePath.startsWith("/") ? routePath : "/" + routePath

            app[type](endpoint, async (req, res) => {
                if (isDisable) {
                    return res.sendFile(mainten)
                }

                try {
                    let scrap = require("./scrap").loadScrapers(
                        path.join(__dirname, "../scraper")
                    )
                    //console.log(scrap)
                    let config = require("../../../configs")
                    code(req, res, { axios, scrap, config })
                } catch (e) {
                    console.error(chalk.red(`${file}\n${e}`))
                    res.status(500).json({
                        status: 500,
                        message: "Try Again Next Time."
                    })
                }
            })

            if (!hidden) {
                console.log(
                    chalk.blue("[ ROUTE LIST → ]"),
                    chalk.yellow(endpoint),
                    isDisable ? chalk.red("<disabled>") : chalk.green("<active>")
                )
            }
        } catch (err) {
            console.error(`[ ROUTE ERROR ]\n${filePath}\n${err}`)
        }
    }
}

let file = require.resolve(__filename)
fs.watchFile(file, () => {
    fs.unwatchFile(file)
    delete require.cache[file]
})