Rewriting custom Elixir (Phoenix) json api / backend for a work project built with .NET 6 (C#) / Vue3 (TypeScript & TailwindCSS).
Set DB env variables in config/dev.secret.exs
, see config.dev.exs
for example.
Make sure postgres is running.
- Install dependencies with
mix deps.get
- Create and migrate your database with
mix ecto.setup
To start your Phoenix server:
- Start Phoenix endpoint with
mix phx.server
or inside IEx with:iex -S mix phx.server
(bash)iex.bat -S mix phx.server
(powershell)
Now you can visit localhost:4000
from your browser.
Run mix phx.routes
to get a list of all available routes.
Send a request, e.g.:
- http://localhost:4000/api/health
curl http://localhost:4000/api/health
curl http://localhost:4000/api/channels
POST / PATCH / PUT requests must send data in a payload with a data property:
// POST: http://localhost:4000/api/channels
// Content-Type: application/json
{
"data": {
"name": "created",
"channelNumber": 2,
"mode": "RTP",
"streamAddress": "312.123.322.122",
"streamPort": 5000
}
}
Create a free production DB on railway.app.
Windows (PS)
# set env variables
$env:SECRET_KEY_BASE = "$(mix phx.gen.secret)"
$env:DATABASE_URL="postgresql://${{user}}:${{password}}@${{host}}.railway.app:${{port}}/${{database}}"
$env:MIX_ENV = "prod"
# build release
mix release
# set remaining variables:
$env:APP_NAME = "phoenix_api_v2"
$env:PORT = 4000
# start server
_build/prod/rel/phoenix_api_v2/bin/phoenix_api_v2 start
Bash
# set env variables
export SECRET_KEY_BASE="$(mix phx.gen.secret)"
export DATABASE_URL="postgresql://${{user}}:${{password}}@${{host}}.railway.app:${{port}}/${{database}}"
# build release
MIX_ENV=prod mix release
# start server
MIX_ENV=prod APP_NAME=phoenix_api_v2 PORT=4000 _build/prod/rel/phoenix_api_v2/bin/phoenix_api_v2 start
Manage release
# remote in from new terminal (opens iex)
\_build/prod/rel/phoenix_api_v2/bin/phoenix_api_v2 remote
# stop from other terminal
\_build/prod/rel/phoenix_api_v2/bin/phoenix_api_v2 stop
Run migrations
Once you've tested the local version you can deploy easily with gigalixir.
-
install the gigalixir cli and create a user: Docs: Getting started
-
login:
gigalixir login
-
check app is created:
gigalixir apps
The app needs configuring to run on gigalixir:
Docs: Modifying an existing app
There are three different deployment modes: mix, distillery or elixir releases. Releases seemed like the best option.
Once you've tested the local version, push the branch to gigalixir:
git push gigalixir
Finally, run migrations on server
ssh_keygen
- create ssh key in ~/.ssh/id_rsa.pubgigalixir account:ssh_keys:add "$(cat ~/.ssh/id_rsa.pub)"
gigalixir ps:migrate
- run db migrations
gigalixir logs
- monitor logging on the servergigalixir ps:remote-console
- open iex on the servergigalixir ps:ssh
- ssh into the production container (where the release is deployed)
Ready to run in production? Please check our deployment guides.
-
Official website: https://www.phoenixframework.org/
-
deploy to update
-
07/12/2022 - deploy
-
03/01/2023 - deploy