Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/medusa/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion packages/medusa/src/loaders/express.ts
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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")
})
Expand Down
6 changes: 5 additions & 1 deletion packages/medusa/src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
Loading