rpuls / medusajs-2.0-for-railway-boilerplate

Monorepo including medusajs 2.0 backend and storefront
25 stars 32 forks source link

QOL improvements #8

Closed aleciavogel closed 2 weeks ago

aleciavogel commented 2 weeks ago

I used your starter and got stuck earlier this week, so these are a few of the changes I made to get properly up and running.

Summary

Railway Changes

Original railway changes discussed In order to get this new version deployed on Railway, you'll need to make the following changes in the `Backend` service: 1. On Railway, update the Backend's custom Build script setting to: ```bash pnpm run build && cd .medusa/server && pnpm install ``` 2. Update the Backend's custom Start script setting to: ```bash cd .medusa/server && pnpm start ``` 3. Add `STRIPE_API_KEY`, `STRIPE_WEBHOOK_SECRET`, `RESEND_API_KEY`, and `RESEND_FROM` variables to the Backend. 4. Add the `/backend/**` watcher so it doesn't run a new deployment in the backend for changes made in the frontend. *** If anyone is worried about image size due to running `pnpm install` twice but doesn't want to go down the Dockerfile rabbit hole with Railway, one workaround could be: 1. Build locally via `cd backend && pnpm build` 2. `cd ./.medusa/server && pnpm install` 3. Use the [Railway cli](https://docs.railway.app/guides/cli) to deploy the `.medusa/server` folder directly (ie, `railway up` after running `railway link`) 4. Replacing the custom Build script on Railway with `pnpm exec init-backend` (or alternatively, running `railway run pnpm exec init-backend` if I'm wrong about how `railway up` works) and deleting the custom Start script setting However, this approach will prevent you from being able to use a CI/CD workflow and the automated PR branch environments in Railway without further work (like creating a github workflow, etc) Another idea I randomly had is not ignoring the `.medusa/server` folder and simply changing the Root Directory on Railway to use that as its source. You'll either need to build locally or have a github workflow that adds and commits the files for you upon merging to your `main` or `master` branch. Then just follow Step 4 in the list above to update your Railway settings for the Backend service accordingly. This way, you still get to keep a CI/CD workflow and minimize the size of the image.

Add STRIPE_API_KEY, STRIPE_WEBHOOK_SECRET, RESEND_API_KEY, and RESEND_FROM variables to the Backend.

rpuls commented 2 weeks ago

Thank you for the comprehensive PR and thorough description of your improvements. 🙂 There are a few challenges with the current build approach:

I'll see if I can come up with a solution

aleciavogel commented 2 weeks ago

While I'm full stack and can usually feel my way around with Docker, I'm definitely not an expert when it comes to dev-ops or hosting infrastructure related tasks. 😅

Perhaps keeping the post-build function is best, as I was going off of what the official docs were saying when I made the decision to switch to using .medusa/server.

Also, I've written a custom resend provider that uses react-email which I'm happy to include. I found the one from typed-dev to be missing a bunch of resend options like replyTo and then I went a bit overboard, haha.

rpuls commented 2 weeks ago

Definitely no dev-ops engineer here either 😂 But I have been dealing with Railway for a while now.

I think I will reintroduce the postBuild script, but align it more closely with the recommended approach. This means installing dependencies again inside the actual build directory, rather than just moving the build output to the root, which was dirty temp. solution. After that, copy the lockfile (and .env for local ) to /server before finally installing dependencies again using lock file. Since this is the production build, the node_modules will be significantly smaller (300mb) as it won't include all the devDependencies. So, while there will be some redundant install time and disk space usage, it might not be the end of the world.

Regarding Resend integration, it would be great to have a new module. The @typed-dev one is outdated and hasn't merged my PR, which is why I'm currently installing it from a Git branch. If you have an up-to-date npm package, and plan on maintaining it, that would be ideal. 🙂

rpuls commented 2 weeks ago

The staging version of the template, which uses the staging branch (including this PR and my edits), now deploys successfully on Railway: Railway Template.

I will be doing extensive feature testing tomorrow and will merge the branch to master if everything checks out.

Thank you very much for contributing, and good catch on the invite issue! I had noticed the problem but didn’t realize it was related to the excessive auth modules :)

aleciavogel commented 2 weeks ago

I've added my custom resend provider with the react-email integration.

Once you pull these changes to your dev environment, run the following:

cd backend && pnpm install && pnpm email:dev

You can open http://localhost:3002 once it's up and running. You can then click on invite-created to view a preview of the email template.

I've also written a README on how to use the custom templates here

rpuls commented 2 weeks ago

Thank you for the great improvements! 🙂

A few key changes I made:

Unfortunately, when I tried to invite a new admin user, I still encountered an error about the token being invalid or expired. Were you able to successfully use the generated invite link?

Feel free to discuss or debate any of my adjustments.

aleciavogel commented 2 weeks ago

Have you tried wiping the database and reseeding? And did you remember to log out (or open in incognito) before trying to accept the invite?

On Fri, Nov 1, 2024, 7:57 a.m. Rasmus Puls @.***> wrote:

Thank you for the great improvements! 🙂

A few key changes I made:

  • Stripe, Resend, and Redis are now optional. Redis allows for super rapid local development, while the other two ensure that the deploy template can still be launched with just one click, without additional configuration needed. I also added optional environment variables to the Railway template to make it easier to launch with Resend and Stripe from the initial deploy.
  • The build procedure follows our previous discussion, using a postBuild script to move the lock file before installing dependencies for the second time in the build output directory.
  • I extended your excellent react-email implementation to templatize the order-placed email as well.
  • I removed the medusa db:migrate command from the start script, as it is run within init-backend (if the database is not yet seeded). Please let me know if migrations are supposed to run on every deploy.

Unfortunately, when I tried to invite a new admin user, I still encountered an error about the token being invalid or expired. Were you able to successfully use the generated invite link?

Feel free to discuss or debate any of my adjustments.

— Reply to this email directly, view it on GitHub https://github.com/rpuls/medusajs-2.0-for-railway-boilerplate/pull/8#issuecomment-2451916092, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACZFREG5DDPFV7HHQKSDTGDZ6OCELAVCNFSM6AAAAABQ5XZ6YSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINJRHEYTMMBZGI . You are receiving this because you authored the thread.Message ID: @.***>

rpuls commented 2 weeks ago

You are right! :D Just to ensure no cross session, I opened the email from my phone and were able to sigup just fine. Awesome!

aleciavogel commented 2 weeks ago

Yay!

And I think me adding the db:migrate was me being superstitious because of how unpredictable Medusa was being while I was trying to get up and running. Did more research on MikroORM to make sure we don't need to rerun the link sync step (which happens automatically during migrations) on startup either. So good call on that.

On Fri, Nov 1, 2024, 8:04 a.m. Rasmus Puls @.***> wrote:

You are right! :D Just to ensure no cross session, I opened the email from my phone and were able to sigup just fine. Awesome!

— Reply to this email directly, view it on GitHub https://github.com/rpuls/medusajs-2.0-for-railway-boilerplate/pull/8#issuecomment-2451927686, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACZFREDQ2JY34BMUKTC3ESDZ6OC7BAVCNFSM6AAAAABQ5XZ6YSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINJRHEZDONRYGY . You are receiving this because you authored the thread.Message ID: @.***>