diff --git a/next.config.mjs b/next.config.mjs index ceddf3d4..196635c4 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -12,6 +12,20 @@ const remotes = (isServer) => { }; }; +const PORTAL_BASE_URL = "https://staging.sunbirded.org"; + +const routes = { + API: { + GENERAL: { + CONTENT_PREVIEW: "/content/preview/:path*", + CONTENT_PLUGINS: "/content-plugins/:path*", + GENERIC_EDITOR: "/generic-editor/:path*", + CONTENT_EDITOR: "/editor/content/:path*", + ASSET_IMAGE: "/assets/images/:path*", + }, + }, +}; + const nextConfig = { eslint: { // Disabling on production builds because we're running checks on PRs via GitHub Actions. @@ -33,12 +47,24 @@ const nextConfig = { return [ { source: "/action/asset/v1/upload/:identifier*", // Match asset upload routes - destination: `${process.env.WORKSPACE_BASE_URL}/api/fileUpload`, // Forward asset uploads to fileUpload.js + destination: '/api/fileUpload' // Forward asset uploads to fileUpload proxy + }, + { + source: "/action/content/v3/upload/url/:identifier*", // Match content upload with 'url' in the path + destination: `${process.env.WORKSPACE_BASE_URL}/api/proxy?path=/action/content/v3/upload/url/:identifier*`, // Forward to proxy route with path as query param + }, + { + source: '/action/content/v3/upload/:identifier*', // Match content upload routes + destination: '/api/fileUpload', // Forward asset uploads to fileUpload proxy }, { source: "/action/asset/:path*", // Match other /action/asset routes destination: `${process.env.WORKSPACE_BASE_URL}/api/proxy?path=/action/asset/:path*`, // Forward other /action/asset requests to proxy.js }, + { + source: "/action/content/:path*", // Match other /action/asset routes + destination: `${process.env.WORKSPACE_BASE_URL}/api/proxy?path=/action/content/:path*`, // Forward other /action/asset requests to proxy.js + }, { source: "/action/:path*", // Match any other routes starting with /action/ destination: `${process.env.WORKSPACE_BASE_URL}/api/proxy?path=/action/:path*`, // Forward them to proxy.js @@ -48,8 +74,32 @@ const nextConfig = { destination: `${process.env.WORKSPACE_BASE_URL}/api/proxy?path=/api/:path*`, // Forward them to proxy.js }, { - source: "/assets/public/:path*", // Match any URL starting with /assets/public/ - destination: `${process.env.CLOUD_STORAGE_URL}/:path*`, // Forward to S3, stripping "/assets/public" + source: '/assets/public/:path*', // Match any URL starting with /assets/public/ + destination: `${process.env.WORKSPACE_BASE_URL}/assets/public/:path*`, // Forward to workspace proxy + }, + { + source: routes.API.GENERAL.CONTENT_PREVIEW, + destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.CONTENT_PREVIEW}`, // Proxy to portal + }, + { + source: routes.API.GENERAL.CONTENT_PLUGINS, + destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.CONTENT_PLUGINS}`, // Proxy to portal + }, + { + source: routes.API.GENERAL.GENERIC_EDITOR, + destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.GENERIC_EDITOR}`, // Proxy to portal + }, + { + source: routes.API.GENERAL.CONTENT_EDITOR, + destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.CONTENT_EDITOR}`, // Proxy to portal + }, + { + source: routes.API.GENERAL.ASSET_IMAGE, + destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.ASSET_IMAGE}`, // Proxy to portal + }, + { + source: "/app/telemetry", // Match telemetry route + destination: `${process.env.WORKSPACE_BASE_URL}/api/telemetry`, // Redirect to telemetry proxy }, ]; }, diff --git a/src/pages/_document.tsx b/src/pages/_document.tsx index c5f5fea9..5779813f 100644 --- a/src/pages/_document.tsx +++ b/src/pages/_document.tsx @@ -7,10 +7,10 @@ export default function Document() { diff --git a/src/pages/api/fileUpload.ts b/src/pages/api/fileUpload.ts new file mode 100644 index 00000000..635dc738 --- /dev/null +++ b/src/pages/api/fileUpload.ts @@ -0,0 +1,56 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; + +export const config = { + api: { + bodyParser: false, // Disable body parsing so we can handle it manually + }, +}; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + console.log('req.url ====>', req.url) + if (req.method === 'POST') { + try { + // Create a new Headers object to ensure valid header types + const headers = new Headers(); + + // Forward the incoming request headers, filtering out invalid headers + for (const [key, value] of Object.entries(req.headers)) { + // Exclude 'content-length' as it can cause issues + if (key.toLowerCase() !== 'content-length') { + headers.append(key, value as string); + } + } + + // Read the request body as a stream + const body = await new Promise((resolve, reject) => { + const chunks: Buffer[] = []; + req.on('data', (chunk) => { + chunks.push(Buffer.from(chunk)); + }); + req.on('end', () => { + resolve(Buffer.concat(chunks)); + }); + req.on('error', (err) => { + reject(err); + }); + }); + + // Forward the request to the middleware API + const response = await fetch(`${process.env.WORKSPACE_BASE_URL}` + `${req.url}`, { + method: 'POST', + headers: headers, // Use the new Headers object + body, // Pass the request body as a Buffer + }); + + // Forward the middleware response back to the client + const data = await response.json(); + res.status(response.status).json(data); + } catch (error) { + console.error('Error proxying request:', error); + res.status(500).json({ error: 'Internal Server Error' }); + } + } else { + res.setHeader('Allow', ['POST']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } +} diff --git a/src/pages/editor.tsx b/src/pages/editor.tsx index 5a39e126..209b67e3 100644 --- a/src/pages/editor.tsx +++ b/src/pages/editor.tsx @@ -1,24 +1,21 @@ -import React from 'react' -import dynamic from 'next/dynamic'; -import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; - +import React from "react"; +import dynamic from "next/dynamic"; +import { serverSideTranslations } from "next-i18next/serverSideTranslations"; // @ts-ignore -const Editors = dynamic(() => import('editor/Editor'), { ssr: false }); +const Editors = dynamic(() => import("editor/Editor"), { ssr: false }); const Editor = () => { - return ( - - ) -} - -export default Editor + return ; +}; +export default Editor; export async function getStaticProps({ locale }: any) { - return { - props: { - ...(await serverSideTranslations(locale, ["common"])), - }, - }; - } \ No newline at end of file + return { + props: { + noLayout: true, + ...(await serverSideTranslations(locale, ["common"])), + }, + }; +} diff --git a/src/pages/importCsv.tsx b/src/pages/importCsv.tsx index 3cb17426..77754a20 100644 --- a/src/pages/importCsv.tsx +++ b/src/pages/importCsv.tsx @@ -350,7 +350,7 @@ const ImportCsv = () => { > {t("COURSE_PLANNER.IMPORT_PLANNER")} - + */}