W
Wasp-lang•2mo ago
KC

redirect incoming request to a path with a new header

Is there a way to redirect a request coming to a path like /blog*? I am trying to add an Astro blog to our app which will be on a different machine. So all requests to /blog need to be routed to the Astro machine. I am using Fly so there is an easy way to do this using the Fly-Replay header. Here's the implementation in Express app.all('/blog*', function replayToBlog(req, res) { res.set('Fly-Replay', 'app=fly-replay-blog') res.status(204).send('') })
11 Replies
kapa.ai
kapa.ai•2mo ago
Hi @KC (GitHub @Case-E). I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. The knowledge sources provided do not contain information on how to redirect requests to a different machine using Wasp or how to set headers in Wasp. However, Wasp is built on top of Express.js for the server-side, so you might be able to use Express.js middleware for this purpose. I recommend checking the Express.js documentation (https://expressjs.com/) or the Wasp documentation (https://wasp-lang.dev/docs) for more information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @KC (GitHub @Case-E), I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
KC
KC•2mo ago
I might use Docusaurus or Astro for the blog but what I am looking to do is to let Fly Proxy replay the request on the correct machine. I am wondering if there is something I can setup in the main.wasp file like this (I know Wasp uses Express under the hood, so the handler would have some express code, may be?):
route RedirectBlogRoute {
path: "/blog*"
middlewareConfigFn: import { redirectBlogHandler } from "@src/redirectBlog.js"
route RedirectBlogRoute {
path: "/blog*"
middlewareConfigFn: import { redirectBlogHandler } from "@src/redirectBlog.js"
But that's only a middleware.
matijash
matijash•2mo ago
got it. @miho will probably be the best person to help out with this. By the way, is there a particular reason why Astro blog has to be on a different machine? Also, Open SaaS already comes with a predefined Astro blog
KC
KC•2mo ago
Thanks @matija! I am specifically looking to scale to 0 when there isn't any traffic on the blog and let the app machines not get worked up with the blog/docs traffic. I think Open SaaS also asks you to host the blog on Netlify or similar if I remember it right. They are not hosted together on the same machine. @Vinny (@Wasp) A good addition to the Open SaaS docs would be on how to point example.com/blog to the blog site from the Open SaaS app. Back to this problem it looks like route doesn't accept any middleware or functions. So I am trying to see if I can get this done by using an api instead but somehow the API isn't rendering and I am getting a 404 or "This site needs JS" when hitting the URL from the terminal.
KC
KC•2mo ago
Fly.io
How to route certain paths to another Fly app without a reverse pro...
We have an amazing feature called Fly-Replay that let anyone hosting apps on Fly teach our proxy how to reroute requests. Want to make a request go to another region? No problem. Should this request go to one specific machine? We can do that too. Should this request go to another app entirely? Eeeeasy. In this demo yall can see how to make an ...
KC
KC•2mo ago
@miho Wondering if you had a chance to take a look at this. Do you have any suggestions here? Thank you! 😄
miho
miho•2mo ago
If a Wasp client lives at example.com, you can't embed an Astro app at /blog. Unless you do some sort of an iframe solution which I wouldn't really recommend. You'd have to use the platform features e.g. Fly somehow supports serving a different app at a subfolder. But this then becomes a problem not related to Wasp but rather to the hosting platform. The Fly-Replay feature won't help you here unfortunately, because it requires access to setting different headers for different sub routes and if you use the default client setup for Wasp, we are just using a basic static serving server written in Go for which you can't really send custom headers for different subfolders. Let us know if you figure this out.
KC
KC•2mo ago
Thanks for the super detailed response here Miho! I tried the API route but then realised that only works for the server, not the client. Hypothetically, would it be possible to generate an Astro site and run it alongside Wasp's client on the same machine? I am now thinking the best way is to use a reverse-proxy and send the traffic to the machines accordingly. (may be I could try CNAME with a sub-domain as well)
miho
miho•2mo ago
Hypothetically, would it be possible to generate an Astro site and run it alongside Wasp's client on the same machine?
One thing you could do is building the blog into the ./dist folder where Wasp builds the client. Before deploying the client, you could sneak in the blog to be at ./.wasp/build/dist/blog/... so that way it gets served first instead of Netlify defaulting to the index.html and serving the Wasp client SPA.
KC
KC•2mo ago
Wonderful! Thanks @miho, I'll give that a shot to see how it works. If I go that route, I'll share details on how I set it up with the CI etc., here.
MEE6
MEE6•2mo ago
Wohooo @KC, you just became a Waspeteer level 4!
Want results from more Discord servers?
Add your server