Running a python api and react frontend from one service (or alternative?)

frazrasHOBBY

8 months ago

I'm building an app that builds a react app then python uses the build files as the root / but when I run npm in the build step, the build folder is still missing.

my railway.json is below:

{
  "$schema": "https://railway.app/railway.schema.json",
  "build": {
    "builder": "NIXPACKS",
    "buildCommand": "pip install -r requirements.txt && if [ -d \"ui\" ]; then cd ui && npm install && npm run build && cd ..; fi"
  },
  "deploy": {
    "startCommand": "uvicorn app.main:app --host 0.0.0.0 --port $PORT",
    "restartPolicyType": "ON_FAILURE",
    "restartPolicyMaxRetries": 10
  }
}

Then python should find and mount the build folder:

if os.path.exists(current_dir + "/ui/build"):
    app.mount("/", StaticFiles(directory=current_dir + "/ui/build", html=True), name="static")
    print("Mounted / at " + current_dir + "/ui/build")
else:
    #warning with full directory path
    print("Warning: '" + current_dir + "/ui/build' directory does not exist. Static files will not be served.")
    # Show the contents of the current folder

Works fine locally but not seeing the build folder when deployed.

The project is open source here: https://github.com/frazras/contentscribe

1 Replies

8 months ago

I'm building an app that builds a react app then python uses the build files as the root / but when I run npm in the build step, the build folder is still missing.

In my experience this is not a good idea and results in a horrible DX both locally and in cloud environments.

What you want is a monorepo!

Please get your project into the following structure before we continue -

.
├── /frontend/
│   ├── /src
│   ├── package.json
│   └── package-lock.json
└── /backend/
    ├── /app
    ├── Dockerfile
    └── manage.py

Running a python api and react frontend from one service (or alternative?) - Railway Help Station