# yaml-language-server: $schema=https://schema.zeabur.app/template.json
apiVersion: zeabur.com/v1
kind: Template
metadata:
    name: NATS
spec:
    description: A simple, secure and performant communications system and data layer for services
    icon: https://avatars.githubusercontent.com/u/10203055?s=200&v=4
    tags:
        - Tools
        - API
    readme: |
        # NATS

        NATS is a simple, secure and high performance open source data layer for cloud native applications, IoT messaging, and microservices architectures.

        We feel that it should be the backbone of your communication between services. It doesn't matter what language, protocol, or platform you are using; NATS is the best way to connect your services.

        ## Usage

        After deploying your NATS, you can connect to your NATS server using the address `nats.zeabur.internal:4222` *internally*. The default `$SYS` account and password are `admin` and `nats@zeabur`, and you can change it in the configuration file.

        This NATS server has Jetstream enabled, which means your messages will be persisted. You can disable Jetstream by modifying the `/etc/nats/nats-server.conf` configuration file.

        The port `7422` can used for [mounting a NATS leaf node](https://docs.nats.io/running-a-nats-service/configuration/leafnodes). Connect it with the account you set.

        To monitor the instance, check the HTTP port `8222`.
    services:
        - name: nats
          icon: https://avatars.githubusercontent.com/u/10203055?s=200&v=4
          template: PREBUILT
          spec:
            source:
                image: nats:2-linux
                args:
                    - -c
                    - /etc/nats/nats-server.conf
            ports:
                - id: client
                  port: 4222
                  type: TCP
                - id: monitoring
                  port: 8222
                  type: HTTP
                - id: leafnode
                  port: 7422
                  type: TCP
            volumes:
                - id: jetstream
                  dir: /var/db/jetstream
            instructions:
                - title: NATS Connection String
                  content: nats://${NATS_USERNAME}:${NATS_PASSWORD}@${PORT_FORWARDED_HOSTNAME}:${CLIENT_PORT_FORWARDED_PORT}
                - title: NATS username
                  content: ${NATS_USERNAME}
                - title: NATS password
                  content: ${NATS_PASSWORD}
                - title: NATS server name
                  content: ${NATS_SERVER_NAME}
            env:
                NATS_ACCOUNT_SCOPE:
                    default: $SYS
                NATS_CONNECTION_STRING:
                    default: nats://${NATS_USERNAME}:${NATS_PASSWORD}@${NATS_HOST}:${NATS_PORT}
                    expose: true
                NATS_HOST:
                    default: ${CONTAINER_HOSTNAME}
                    expose: true
                NATS_PASSWORD:
                    default: ${PASSWORD}
                    expose: true
                NATS_PORT:
                    default: ${CLIENT_PORT}
                    expose: true
                NATS_SERVER_NAME:
                    default: zeabur-primary
                    expose: true
                NATS_URI:
                    default: nats://${NATS_USERNAME}:${NATS_PASSWORD}@${NATS_HOST}:${NATS_PORT}
                    expose: true
                NATS_USERNAME:
                    default: zeabur
                    expose: true
            configs:
                - path: /etc/nats/nats-server.conf
                  template: |
                    server_name: ${NATS_SERVER_NAME}

                    # Client port of 4222 on all interfaces
                    port: 4222

                    # HTTP monitoring port
                    monitor_port: 8222

                    # Jetstream configuration
                    jetstream {
                        store_dir: /var/db/jetstream
                        max_mem: 1G
                        max_file: 8G
                    }

                    accounts {
                        ${NATS_ACCOUNT_SCOPE} {
                            users = [
                                {
                                    user: "${NATS_USERNAME}",
                                    pass: "${NATS_PASSWORD}"
                                }
                            ]
                        }
                    }

                    leafnodes {
                        port: 7422
                    }
                  permission: null
                  envsubst: true
