-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.tsx
82 lines (75 loc) · 2.78 KB
/
server.tsx
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
import path from "path"
import cors from "cors"
import mime from "mime"
import {Readable} from "stream"
import fs from "fs"
import express from "express"
import webpack from "webpack"
import middleware from "webpack-dev-middleware"
import hot from "webpack-hot-middleware"
import config from "./webpack.config"
import dotenv from "dotenv"
import React from "react"
import App from "./App"
import {renderToString} from "react-dom/server"
import {StaticRouter as Router} from "react-router-dom"
import dbFunctions from "./structures/DatabaseFunctions"
import serverFunctions from "./structures/ServerFunctions"
const __dirname = path.resolve()
dotenv.config()
const app = express()
let compiler = webpack(config as any)
app.use(express.urlencoded({extended: true, limit: "1gb", parameterLimit: 50000}))
app.use(express.json({limit: "1gb"}))
app.use(cors({credentials: true, origin: true}))
app.disable("x-powered-by")
app.set("trust proxy", true)
if (process.env.TESTING === "yes") {
app.use(middleware(compiler, {
index: false,
serverSideRender: false,
writeToDisk: false,
}))
app.use(hot(compiler))
}
app.use(express.static(path.join(__dirname, "./public")))
app.use(express.static(path.join(__dirname, "./dist"), {index: false}))
app.use("/assets", express.static(path.join(__dirname, "./assets")))
app.get("/Manga/*", function(req, res, next) {
if (req.path.includes("/manga")) return next()
res.setHeader("Content-Type", mime.getType(req.path) ?? "")
res.header("Access-Control-Allow-Origin", "*")
try {
let pathname = `/Volumes/Files/${decodeURIComponent(req.path.slice(1))}`
const body = fs.readFileSync(pathname)
const contentLength = body.length
if (req.headers.range) {
const parts = req.headers.range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0])
const end = parts[1] ? parseInt(parts[1]) : contentLength - 1
res.writeHead(206, {
"Content-Range": `bytes ${start}-${end}/${contentLength}`,
"Accept-Ranges": "bytes",
"Content-Length": end - start + 1
})
const stream = Readable.from(body.slice(start, end + 1))
return stream.pipe(res)
}
res.setHeader("Content-Length", contentLength)
res.status(200).end(body)
} catch (e) {
console.log(e)
res.status(400).end()
}
})
app.get("/*", function(req, res) {
res.setHeader("Content-Type", mime.getType(req.path) ?? "")
res.header("Access-Control-Allow-Origin", "*")
const document = fs.readFileSync(path.join(__dirname, "./dist/index.html"), {encoding: "utf-8"})
res.status(200).send(document)
})
const run = async () => {
app.listen(process.env.PORT || 8080, () => console.log("Started the website server!"))
dbFunctions.logGenres()
}
run()