Command
interface makes this a cinche, allowing you to create custom commands to run your application with. It’s built on the powerful Swift Argument Parser making it easy to add arguments, options, flags and help functionality to your custom commands. All commands have access to services registered in Application.boot
so it’s easy to interact with whatever database, queues, & other functionality that your app already has.
serve
which boots the app and serves it on the machine.
There are also migrate
and queue
commands which help run migrations and queue workers/schedulers respectively.
You can run these like so.
Run
-> Arguments
.
If you’re looking to extend your Alchemy app with your own custom commands, check out Commands.
swift run
orswift run Server serve
Option | Default | Description |
---|---|---|
—host | 127.0.0.1 | The host to listen on |
—port | 3000 | The port to listen on |
—unixSocket | nil | The unix socket to listen on. Mutually exclusive with host & port |
—workers | 0 | The number of workers to run |
—schedule | false | Whether scheduled tasks should be scheduled |
—migrate | false | Whether any outstanding migrations should be run before serving |
—env | env | The environment to load |
swift run Server migrate
Option | Default | Description |
---|---|---|
—rollback | false | Should migrations be rolled back instead of applied |
—env | env | The environment to load |
swift run Server queue
Option | Default | Description |
---|---|---|
—name | nil | The queue to monitor. Leave empty to monitor Queue.default |
—channels | default | The channels to monitor, separated by comma |
—workers | 1 | The number of workers to run |
—schedule | false | Whether scheduled tasks should be scheduled |
—env | env | The environment to load |
make
Commandsmake:
, and you can see all available ones with swift run MyApp help
.
For example, the make:model
command makes it easy to generate a model with the given fields. You can event generate a full populated Migration and Controller with CRUD routes by passing the --migration
and --controller
flags.
swift run MyApp help <command>
.
Command
protocol, implement func start()
, and register it with app.registerCommand(...)
. Now, when you run your Alchemy app you may pass your custom command name as an argument to execute it.
For example, let’s say you wanted a command that prints all user emails in your default database.
Application.boot
print
argument to run your command.
Command
inherits from Swift Argument Parser’s ParsableCommand
you can easily add flags, options, and configurations to your commands. There’s also support for adding help & discussion strings that will show if your app is run with the help
argument.
swift run MyApp sync --id 2 --dry
and it run with the given arguments.
help
argument to show all commands available to it, including any custom ones your may have registered.
configuration
, options, flags, etc.
-e, --env <env-file>
to any command to have it load your environment from a custom env file before running.