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 authored and GitHub committed 2024-07-08 17:44:36 +00:00
1 parent 04886ea122
commit 366231f658
4 files changed
+24 -6

No files matched your search

+7 -1
View File
@@ -89,7 +89,13 @@ export default async function ({ port, directory }) {
this.watcher = chokidar.watch(["."], { this.watcher = chokidar.watch(["."], {
ignoreInitial: true, ignoreInitial: true,
cwd: process.cwd(), cwd: process.cwd(),
ignored: [/(^|[\\/\\])\../, "node_modules", "dist", "src/admin/**/*"], ignored: [
/(^|[\\/\\])\../,
"node_modules",
"dist",
"static",
"src/admin/**/*",
],
}) })
this.watcher.on("add", (file) => { this.watcher.on("add", (file) => {
+7 -1
View File
@@ -1,19 +1,22 @@
import { ConfigModule } from "@medusajs/types" import { ConfigModule } from "@medusajs/types"
import createStore from "connect-redis" import createStore from "connect-redis"
import cookieParser from "cookie-parser" import cookieParser from "cookie-parser"
import { Express } from "express" import express, { Express } from "express"
import session from "express-session" import session from "express-session"
import Redis from "ioredis" import Redis from "ioredis"
import morgan from "morgan" import morgan from "morgan"
import path from "path"
type Options = { type Options = {
app: Express app: Express
configModule: ConfigModule configModule: ConfigModule
rootDirectory: string
} }
export default async ({ export default async ({
app, app,
configModule, configModule,
rootDirectory,
}: Options): Promise<{ }: Options): Promise<{
app: Express app: Express
shutdown: () => Promise<void> shutdown: () => Promise<void>
@@ -67,6 +70,9 @@ export default async ({
app.use(cookieParser()) app.use(cookieParser())
app.use(session(sessionOpts)) 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) => { app.get("/health", (req, res) => {
res.status(200).send("OK") res.status(200).send("OK")
}) })
+5 -1
View File
@@ -92,7 +92,11 @@ async function loadEntrypoints(
return async () => {} 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) => { expressApp.use((req: Request, res: Response, next: NextFunction) => {
req.scope = container.createScope() as MedusaContainer req.scope = container.createScope() as MedusaContainer
req.requestId = (req.headers["x-request-id"] as string) ?? v4() req.requestId = (req.headers["x-request-id"] as string) ?? v4()
@@ -10,8 +10,8 @@ export class LocalFileService extends AbstractFileProviderService {
constructor(_, options: LocalFileServiceOptions) { constructor(_, options: LocalFileServiceOptions) {
super() super()
this.uploadDir_ = options?.upload_dir || path.join(__dirname, "uploads") this.uploadDir_ = options?.upload_dir || path.join(process.cwd(), "static")
this.backendUrl_ = options?.backend_url || "http://localhost:9000" this.backendUrl_ = options?.backend_url || "http://localhost:9000/static"
} }
async upload( async upload(
@@ -83,7 +83,9 @@ export class LocalFileService extends AbstractFileProviderService {
} }
private getUploadFileUrl = (fileKey: string) => { 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) { private async ensureDirExists(dirPath: string) {