Skip to content

Commit

Permalink
Make the 1-click-bom.tsv file accessible to the 1-click-bom extension.
Browse files Browse the repository at this point in the history
The 1-click-bom extension tries loading the `1-click-BOM.tsv` file by
`GET`ing `[...projecURL]/1-click-BOM.tsx`.
But we have the file in the processor (or later on s3) so using this
approach will let us control the actual `1-click-BOM.tsv` file URL
without re-modifying the extension.
  • Loading branch information
AbdulrhmnGhanem committed Nov 17, 2022
1 parent d7411d4 commit 643d0ea
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
7 changes: 6 additions & 1 deletion frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ module.exports = async phase => {
source: '/:user/:repo/_',
destination: '/:user/:repo',
permanent: true,
}
},
{
source: '/:user/:repo/1-click-BOM.tsv',
destination: '/:user/:repo/_/1-click-BOM.tsv',
permanent: true,
},
]
},
async rewrites() {
Expand Down
37 changes: 37 additions & 0 deletions frontend/src/pages/_middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// middleware.ts
import { NextResponse } from 'next/server'

// The redirection between the different proxied services corrupts SSL somehow.
// As a workaround we redirect using http:// and nginx will automatically upgrade it to https://.
const sslWorkaround = url => {
url = new URL(url)
url.protocol = 'http:'
return url.toString().slice(0, -1) // remove the trailing slash
}

const No_SSL_KITSPACE_PROCESSOR_URL = sslWorkaround(
process.env.KITSPACE_PROCESSOR_URL,
)

/*
* Make the 1-click-bom.tsv file accessible to the 1-click-bom extension.
* We can't use the following snippet `next.config.js` redirects: it generates the redirect URLs during the build time of the container.
* But the KITSPACE_PROCESSOR_URL is only available at runtime.
{
source: '/:user/:repo/:project/1-click-BOM.tsv',
destination: `${process.env.KITSPACE_PROCESSOR_URL}/files/:user/:repo/HEAD/:project/1-click-BOM.tsv`,
permanent: true,
}
*/
export function middleware(request) {
// We are using the pattern because simply using ':user/:repo/:project/1-click-BOM.tsv' matches `/static/` files as well.
const matches = request.nextUrl.pathname.match(
/^\/(?<user>.+)\/(?<repo>.+)\/(?<project>.+)\/(?:1-click-BOM.tsv)$/,
)
if (matches) {
const { user, repo, project } = matches.groups
return NextResponse.redirect(
`${No_SSL_KITSPACE_PROCESSOR_URL}/files/${user}/${repo}/HEAD/${project}/1-click-BOM.tsv`,
)
}
}
3 changes: 2 additions & 1 deletion frontend/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const next = require('next')
const fetch = require('node-fetch')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const hostname = process.env.KITSPACE_DOMAIN

const app = next({ dev, port })
const app = next({ dev, port, hostname })
const nextHandler = app.getRequestHandler()

main().catch(e => {
Expand Down

0 comments on commit 643d0ea

Please sign in to comment.