A Quick Tour with Loco
Let's create a blog backend on `loco` in 4 commands. First install `loco-cli` and `sea-orm-cli`:
Now you can create your new app (choose "SaaS app").
)
)
)
loco:loco
and a db named is the [insert app]_development.
You can use Docker to run a Postgres instance:
When generating a starter, the database name incorporates your application name and the environment. For instance, if you include myapp
, the database name in the test.yaml
configuration will be myapp_test
, and in the development.yaml
configuration, it will be myapp_development
.
$ docker run -d -p 5432:5432 -e POSTGRES_USER=loco -e POSTGRES_DB=myapp_development -e POSTGRES_PASSWORD="loco" postgres:15.3-alpine
A more advanced set of docker-compose.yml
and Dockerfiles
that include Redis and the mailtutan
mailer are available for each starter on GitHub.
Now cd
into your myapp
and start your app:
$ cd myapp
$ cargo loco start
Finished dev [unoptimized + debuginfo] target(s) in 21.63s
Running `target/debug/myapp start`
:
:
:
controller/app_routes.rs:203: [Middleware] Adding log trace id
▄ ▀
▀ ▄
▄ ▀ ▄ ▄ ▄▀
▄ ▀▄▄
▄ ▀ ▀ ▀▄▀█▄
▀█▄
▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█
██████ █████ ███ █████ ███ █████ ███ ▀█
██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄
██████ █████ ███ █████ █████ ███ ████▄
██████ █████ ███ █████ ▄▄▄ █████ ███ █████
██████ █████ ███ ████ ███ █████ ███ ████▀
▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
started on port 3000
Adding a CRUD API
We have a base SaaS app with user authentication generated for us. Let's make it a blog backend by adding a post
and a full CRUD API using scaffold
:
Your database have been migrated and model, entities, and a full CRUD controller have been generated automatically.
Start your app:
Next, try adding a post
with curl
:
You can list your posts:
For those counting -- the commands for creating a blog backend were:
cargo install loco-cli
cargo install sea-orm-cli
loco new
cargo loco generate scaffold post title:string content:text
Done! enjoy your ride with loco
🚂
Checking Out SaaS Authentication
Your generated app contains a fully working authentication suite, based on JWTs.
To authenticate, you will need a running redis server.
This docker command starts up a redis server:
docker run -p 6379:6379 -d redis redis-server
Use doctor command to check the needed resources:
$ cargo loco doctor
Finished dev [unoptimized + debuginfo] target(s) in 0.32s
Running `target/debug/myapp-cli doctor`
✅ SeaORM CLI is installed
✅ DB connection: success
✅ Redis connection: success
Registering a New User
The /api/auth/register
endpoint creates a new user in the database with an email_verification_token
for account verification. A welcome email is sent to the user with a verification link.
For security reasons, if the user is already registered, no new user is created, and a 200 status is returned without exposing user email details.
Login
After registering a new user, use the following request to log in:
The response includes a JWT token for authentication, user ID, name, and verification status.
{
}
Get current user
This endpoint is protected by auth middleware.
Check out the source code for controllers/auth.rs
to see how to use the authentication middleware in your own controllers.