feat: Add static server and adjust file local to work out of the box (#8019)

REF CORE-2496
Closes #8000
This commit is contained in:
Stevche Radevski
2024-07-08 17:44:36 +00:00
committed by GitHub
parent 04886ea122
commit 366231f658
4 changed files with 24 additions and 6 deletions
+7 -1
View File
@@ -89,7 +89,13 @@ export default async function ({ port, directory }) {
this.watcher = chokidar.watch(["."], {
ignoreInitial: true,
cwd: process.cwd(),
ignored: [/(^|[\\/\\])\../, "node_modules", "dist", "src/admin/**/*"],
ignored: [
/(^|[\\/\\])\../,
"node_modules",
"dist",
"static",
"src/admin/**/*",
],
})
this.watcher.on("add", (file) => {
+7 -1
View File
@@ -1,19 +1,22 @@
import { ConfigModule } from "@medusajs/types"
import createStore from "connect-redis"
import cookieParser from "cookie-parser"
import { Express } from "express"
import express, { Express } from "express"
import session from "express-session"
import Redis from "ioredis"
import morgan from "morgan"
import path from "path"
type Options = {
app: Express
configModule: ConfigModule
rootDirectory: string
}
export default async ({
app,
configModule,
rootDirectory,
}: Options): Promise<{
app: Express
shutdown: () => Promise<void>
@@ -67,6 +70,9 @@ export default async ({
app.use(cookieParser())
app.use(session(sessionOpts))
// Currently we don't allow configuration of static files, but this can be revisited as needed.
app.use("/static", express.static(path.join(rootDirectory, "static")))
app.get("/health", (req, res) => {
res.status(200).send("OK")
})
+5 -1
View File
@@ -92,7 +92,11 @@ async function loadEntrypoints(
return async () => {}
}
const { shutdown } = await expressLoader({ app: expressApp, configModule })
const { shutdown } = await expressLoader({
app: expressApp,
configModule,
rootDirectory,
})
expressApp.use((req: Request, res: Response, next: NextFunction) => {
req.scope = container.createScope() as MedusaContainer
req.requestId = (req.headers["x-request-id"] as string) ?? v4()
@@ -10,8 +10,8 @@ export class LocalFileService extends AbstractFileProviderService {
constructor(_, options: LocalFileServiceOptions) {
super()
this.uploadDir_ = options?.upload_dir || path.join(__dirname, "uploads")
this.backendUrl_ = options?.backend_url || "http://localhost:9000"
this.uploadDir_ = options?.upload_dir || path.join(process.cwd(), "static")
this.backendUrl_ = options?.backend_url || "http://localhost:9000/static"
}
async upload(
@@ -83,7 +83,9 @@ export class LocalFileService extends AbstractFileProviderService {
}
private getUploadFileUrl = (fileKey: string) => {
return path.join(this.backendUrl_, this.getUploadFilePath(fileKey))
const baseUrl = new URL(this.backendUrl_)
baseUrl.pathname = path.join(baseUrl.pathname, fileKey)
return baseUrl.href
}
private async ensureDirExists(dirPath: string) {