-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use express instead of fastify
- Loading branch information
Showing
8 changed files
with
755 additions
and
734 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
import { fastify } from "./constants"; | ||
import express from "express"; | ||
import cors from "cors"; | ||
import { FilesAPI } from "./routes/files"; | ||
import { PexelsAPI } from "./routes/pexels"; | ||
|
||
import cors from "@fastify/cors"; | ||
const app = express(); | ||
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 8080; | ||
|
||
fastify.register(cors, { | ||
origin: ["http://localhost:5173", "http://localhost", "https://localhost", "capacitor://localhost"], | ||
app.use( | ||
cors({ | ||
origin: ["http://localhost:5173", "http://localhost", "https://localhost", "capacitor://localhost"], | ||
}), | ||
); | ||
|
||
app.use(FilesAPI); | ||
app.use(PexelsAPI); | ||
|
||
const server = app.listen(PORT, () => { | ||
console.log(`Server listening on port ${PORT}`); | ||
}); | ||
fastify.register(FilesAPI); | ||
fastify.register(PexelsAPI); | ||
|
||
const main = async () => { | ||
try { | ||
await fastify.listen({ | ||
host: "0.0.0.0", | ||
port: process.env.PORT ? parseInt(process.env.PORT) : 8080, | ||
}); | ||
} catch (err) { | ||
fastify.log.error(err); | ||
process.exit(1); | ||
} | ||
}; | ||
main(); | ||
// Optional: Handle errors and clean up resources | ||
server.on("error", (error) => { | ||
console.error("Server error:", error); | ||
}); | ||
|
||
process.on("SIGINT", () => { | ||
server.close(() => { | ||
console.log("Server closed"); | ||
process.exit(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,60 @@ | ||
import { FastifyPluginCallback } from "fastify"; | ||
import multipart from "@fastify/multipart"; | ||
import express, { Request, Response } from "express"; | ||
import multer from "multer"; | ||
import { getFileDownload, getFileLink, uploadFiles } from "../telegram"; | ||
import { filesGeneratorToArray } from "../utils/array"; | ||
|
||
export const FilesAPI: FastifyPluginCallback = async (fastify, _, done) => { | ||
await fastify.register(multipart, { | ||
limits: { | ||
fieldNameSize: 1000, | ||
fileSize: 5e6, // 5 MB | ||
files: 50, | ||
fields: 0, | ||
}, | ||
}); | ||
|
||
fastify.post("/upload", async (req, res) => { | ||
console.log("Loading files..."); | ||
const files = await filesGeneratorToArray(req.files()); | ||
console.log(`Start upload of ${files.length} files...`); | ||
const ret = await uploadFiles(files); | ||
|
||
res.code(201); | ||
return ret; | ||
}); | ||
|
||
fastify.get("/file", async (req, res) => { | ||
if (!req.query || typeof req.query !== "object" || !("id" in req.query)) return res.code(404).send(); | ||
const { id } = req.query; | ||
if (typeof id !== "string") return res.code(401).send(); | ||
|
||
const file = await getFileLink(id); | ||
if (!file) return res.code(404).send(); | ||
|
||
return file; | ||
}); | ||
|
||
fastify.get("/download", async (req, res) => { | ||
if (!req.query || typeof req.query !== "object" || !("id" in req.query)) return res.code(404).send(); | ||
const { id } = req.query; | ||
if (typeof id !== "string") return res.code(401).send(); | ||
|
||
const file = await getFileDownload(id); | ||
if (!file) return res.code(404).send(); | ||
|
||
return file; | ||
}); | ||
|
||
done(); | ||
}; | ||
|
||
export const FilesAPI = express.Router(); | ||
const upload = multer({ | ||
limits: { | ||
fieldNameSize: 1000, | ||
fileSize: 5e6, // 5 MB | ||
files: 50, | ||
}, | ||
storage: multer.memoryStorage(), | ||
}); | ||
|
||
FilesAPI.post("/upload", upload.any(), async (req: Request, res: Response) => { | ||
console.log("Loading files..."); | ||
const files = req.files as Express.Multer.File[]; | ||
console.log(`Start upload of ${files.length} files...`); | ||
const ret = await uploadFiles(files); | ||
|
||
res.status(201).json(ret); | ||
}); | ||
|
||
FilesAPI.get("/file", async (req: Request, res: Response) => { | ||
if (!req.query || typeof req.query !== "object" || !("id" in req.query)) { | ||
return res.status(404).send(); | ||
} | ||
|
||
const { id } = req.query; | ||
|
||
if (typeof id !== "string") { | ||
return res.status(401).send(); | ||
} | ||
|
||
const file = await getFileLink(id); | ||
if (!file) { | ||
return res.status(404).send(); | ||
} | ||
|
||
res.json(file); | ||
}); | ||
|
||
FilesAPI.get("/download", async (req: Request, res: Response) => { | ||
if (!req.query || typeof req.query !== "object" || !("id" in req.query)) { | ||
return res.status(404).send(); | ||
} | ||
|
||
const { id } = req.query; | ||
|
||
if (typeof id !== "string") { | ||
return res.status(401).send(); | ||
} | ||
|
||
const file = await getFileDownload(id); | ||
if (!file) { | ||
return res.status(404).send(); | ||
} | ||
|
||
res.json(file); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
import { FastifyPluginCallback } from "fastify"; | ||
import express, { Request, Response } from "express"; | ||
import { PEXELS_API_KEY } from "../constants"; | ||
|
||
const RESULTS = "30"; | ||
export const PexelsAPI = express.Router(); | ||
|
||
const RESULTS = 30; | ||
const PEXELS_ENDPOINT = "https://api.pexels.com/v1/search"; | ||
|
||
export const PexelsAPI: FastifyPluginCallback = async (fastify, _, done) => { | ||
fastify.get("/pexels", async (req, res) => { | ||
if (!req.query || typeof req.query !== "object" || !("query" in req.query)) return res.code(404).send(); | ||
const { query } = req.query; | ||
if (typeof query !== "string") return res.code(401).send(); | ||
PexelsAPI.get("/pexels", async (req: Request, res: Response) => { | ||
if (!req.query || typeof req.query !== "object" || !("query" in req.query)) { | ||
return res.status(404).send(); | ||
} | ||
|
||
const params = new URLSearchParams({ | ||
query, | ||
per_page: RESULTS, | ||
}); | ||
const { query } = req.query; | ||
|
||
if (typeof query !== "string") { | ||
return res.status(401).send(); | ||
} | ||
|
||
try { | ||
const response = await fetch(`${PEXELS_ENDPOINT}?${params}`, { | ||
headers: { | ||
Authorization: PEXELS_API_KEY, | ||
}, | ||
}); | ||
|
||
return await response.json(); | ||
} catch (err) { | ||
console.log(`[ERR]: ${err}`); | ||
return { error: true }; | ||
} | ||
const params = new URLSearchParams({ | ||
query, | ||
per_page: RESULTS.toString(), | ||
}); | ||
|
||
done(); | ||
}; | ||
try { | ||
const response = await fetch(`${PEXELS_ENDPOINT}?${params}`, { | ||
headers: { | ||
Authorization: PEXELS_API_KEY, | ||
}, | ||
}); | ||
|
||
const data = await response.json(); | ||
res.json(data); | ||
} catch (err) { | ||
console.error(`[ERR]: ${err}`); | ||
res.status(500).json({ error: true }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters