Deployment

Deployment is super simple in Loco, and this is why this guide is super short. Although most of the time in development you are using cargo when deploying, you use the binary that was compiled, there is no need for cargo or Rust on the target server.

To deploy, build your production binary for your relevant server architecture:

cargo build --release

And copy your binary along with your config/ folder to the server. You can then run myapp start on your server.

That's it!

We took special care that all of your work is embbedded in a single binary, so you need nothing on the server other than that.

Review your production config

There are a few configuration sections that are important to review and set accordingly when deploying to production:

  • Logger:
# Application logging configuration
logger:
  # Enable or disable logging.
  enable: true
  # Enable pretty backtrace (sets RUST_BACKTRACE=1)
  pretty_backtrace: true
  # Log level, options: trace, debug, info, warn or error.
  level: debug
  # Define the logging format. options: compact, pretty or json
  format: compact
  # By default the logger has filtering only logs that came from your code or logs that came from `loco` framework. to see all third party libraries
  # Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters.
  # override_filter: trace
  • Server:
server:
  # Port on which the server will listen. the server binding is 0.0.0.0:{PORT}
  port: 
  # The UI hostname or IP address that mailers will point to.
  host: http://localhost
  # Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block
  • Database:
database:
  # Database connection URI
  uri: 
  # When enabled, the sql query will be logged.
  enable_logging: false
  # Set the timeout duration when acquiring a connection.
  connect_timeout: 500
  # Set the idle duration before closing a connection.
  idle_timeout: 500
  # Minimum number of connections for a pool.
  min_connections: 1
  # Maximum number of connections for a pool.
  max_connections: 1
  # Run migration up when application loaded
  auto_migrate: true
  # Truncate database when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode
  dangerously_truncate: false
  # Recreating schema when application loaded.  This is a dangerous operation, make sure that you using this flag only on dev environments or test mode
  dangerously_recreate: false
  • Mailer:
mailer:
  # SMTP mailer configuration.
  smtp:
    # Enable/Disable smtp mailer.
    enable: true    
    # SMTP server host. e.x localhost, smtp.gmail.com
    host: 
    # SMTP server port
    port: 1025
    # Use secure connection (SSL/TLS).
    secure: false
    # auth:
    #   user:
    #   password:
  • Queue:
queue:
  # Redis connection URI
  uri: 
  # Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode
  dangerously_flush: false
  • JWT secret:
auth:
  # JWT authentication
  jwt:
    # Secret key for token generation and verification
    secret: PqRwLF2rhHe8J22oBeHy
    # Token expiration time in seconds
    expiration: 604800 # 7 days

Generate

Loco offers a deployment template enabling the creation of a deployment infrastructure.

cargo loco generate deployment
? ❯ Choose your deployment ›
 Docker
 Shuttle
 Nginx

..
 ❯ Choose your deployment · Docker
skipped (exists): "dockerfile"
added: ".dockerignore"

Deployment Options:

  1. Docker:
  • Generates a Dockerfile ready for building and deploying.
  • Creates a .dockerignore file.
  1. Shuttle:
  • Generates a shuttle main function.
  • Adds shuttle-runtime and shuttle-axum as dependencies.
  • Adds a bin entrypoint for the deployment.
  1. Nginx:
  • Generates a nginx configuration file for reverse proxying.

Choose the option that best fits your deployment needs. Happy deploying!

If you have a preference for deploying on a different cloud, feel free to open a pull request. Your contributions are more than welcome!