Creating Frontend Website
Overview
This guide provides a comprehensive walkthrough on using Loco
to build a Todo list application with a REST API and a React frontend. The steps outlined cover everything from project creation to deployment.
Explore the example repository here
The key steps include:
- Creating a Loco project with the SaaS starter
- Setting up a Vite frontend with React
- Configuring Loco to serve frontend static assets
- Implementing the Notes model/controller in the REST API
- Reloading the server and frontend during development
- Deploying the website to production
Selecting SaaS Starter
To begin, run the following command to create a new Loco app using the SaaS starter:
&
)
Follow the prompts to specify the app name (e.g., todolist) and choose the SaaS app option.
After generating the app, ensure you have the necessary resources by running:
$ cd todolist
$ cargo loco doctor
✅ SeaORM CLI is installed
✅ DB connection: success
✅ Redis connection: success
Verify that SeaORM CLI is installed, and the database and Redis connections are successful. If any resources fail, refer to the quick tour guide for troubleshooting.
Once cargo loco doctor
shows all checks passed, start the server:
$ cargo loco start
Updating crates.io index
.
.
.
▄ ▀
▀ ▄
▄ ▀ ▄ ▄ ▄▀
▄ ▀▄▄
▄ ▀ ▀ ▀▄▀█▄
▀█▄
▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█
██████ █████ ███ █████ ███ █████ ███ ▀█
██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄
██████ █████ ███ █████ █████ ███ ████▄
██████ █████ ███ █████ ▄▄▄ █████ ███ █████
██████ █████ ███ ████ ███ █████ ███ ████▀
▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
https://loco.rs
environment: development
database: automigrate
logger: debug
modes: server
listening on port 5150
Creating the Frontend
For the frontend, we'll use Vite with React. In the todolist
folder, run:
)
Follow the prompts to set up the frontend
as a project name.
Navigate to the frontend folder and install dependencies:
$ cd todolist/frontend
$ pnpm install
Start the development server:
Serving Static Assets in Loco
First, move all our rest api endpoint under /api
prefix. for doing it go to src/app.rs
. in routes
hooks function add .prefix("/api")
to the default routes.
Build the frontend for production:
In the frontend
folder, a dist
directory is created. Update the config/development.yaml
file in the main folder to include a static middleware:
server:
middlewares:
static:
enable: true
must_exist: true
folder:
uri: "/"
path: "frontend/dist"
fallback: "frontend/dist/index.html"
Now, run the Loco server again and you should see frontend app serving via Loco
Developing the UI
Install react-router-dom
, react-query
and axios
- Copy main.jsx to frontend/src/main.jsx.
- Copy App.jsx to frontend/src/App.jsx.
- Copy App.css to frontend/src/App.css.
Now, run the server cargo loco start
and the UI pnpm dev in the frontend folder, and start adding your todo list!
Improve Development
use cargo-watch for hot reloading the server:
Now, any changes in your Rust code will automatically reload the server, and any changes in your frontend Vite will reload the frontend app.
Deploy To Production
In the frontend
folder, run pnpm build
. After a successful build, go to the Loco server and run cargo loco start
. Loco will serve the frontend static files directly from the server.
Prepare Docker Image
Run cargo loco generate deployment
and select Docker as the deployment type:
Loco will add a Dockerfile
and a .dockerignore
file. Note that Loco detect the static assent and included them as part of the image
Build the container:
Now run the container: