+++ title = "Deployment" date = 2021-05-01T08:00:00+00:00 updated = 2021-05-01T08:00:00+00:00 draft = false weight = 3 sort_by = "weight" template = "docs/page.html" [extra] toc = true top = false flair =[] +++ 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: ```sh 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: ```yaml # 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: ```yaml server: # Port on which the server will listen. the server binding is 0.0.0.0:{PORT} port: {{ get_env(name="NODE_PORT", default=5150) }} # The UI hostname or IP address that mailers will point to. host: http://localhost ``` - Database: ```yaml database: # Database connection URI uri: {{get_env(name="DATABASE_URL", default="postgres://loco:loco@localhost:5432/loco_app")}} # 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: ```yaml mailer: # SMTP mailer configuration. smtp: # Enable/Disable smtp mailer. enable: true # SMTP server host. e.x localhost, smtp.gmail.com host: {{ get_env(name="MAILER_HOST", default="localhost") }} # SMTP server port port: 1025 # Use secure connection (SSL/TLS). secure: false # auth: # user: # password: ``` - Queue: ```yaml queue: kind: Redis # Redis connection URI uri: {{ get_env(name="REDIS_URL", default="redis://127.0.0.1") }} # 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: ```yaml auth: # JWT authentication jwt: # Secret key for token generation and verification secret: PqRwLF2rhHe8J22oBeHy # Token expiration time in seconds expiration: 604800 # 7 days ``` ## Running `loco doctor` You can run `loco doctor` in your server to check the connection health of your environment. ```sh $ myapp doctor --production ``` ## Generate Loco offers a deployment template enabling the creation of a deployment infrastructure. ```sh 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. 2. Shuttle: - Generates a shuttle main function. - Adds `shuttle-runtime` and `shuttle-axum` as dependencies. - Adds a bin entrypoint for the deployment. 3. 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!