# install hex
mix local.hex
#install phoenix
mix archive.install hex phx_new
#create new app
mix phx.new crypto_api --database mysql
#config Database at dev.exs
config :crypto_api, CryptoApi.Repo,
username: "root",
password: "sua senha",
database: "crypto_api_dev",
hostname: "localhost",
port: 9999,
show_sensitive_data_on_connection_error: true,
pool_size: 10
config :crypto_api, CryptoApi.Repo,
username: "root",
password: "sua senha",
database: "crypto_api_test#{System.get_env("MIX_TEST_PARTITION")}",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox,
port: 9999,
pool_size: 10
mix ecto.create
mix phx.server
mix phx.gen.json Catalog Coin coins ticker:string:unique name:string price:decimal
# priv/repo/migrations/20211111180810_create_coins.exs
add :price, :decimal, precision: 20, scale: 10
mix ecto.migrate
Change priv/repo/seeds.exs
:
alias CryptoApi.Repo
alias CryptoApi.Catalog.Coin
Repo.delete_all("coins")
Repo.insert!(%Coin{ticker: "BTC", name: "Bitcoin", price: 65432.12})
Repo.insert!(%Coin{ticker: "ETH", name: "Ethereum", price: 4765.30})
Repo.insert!(%Coin{ticker: "DOT", name: "Polkadot", price: 48.90})
Repo.insert!(%Coin{ticker: "ADA", name: "Cardano", price: 2.20})
mix run priv/repo/seeds.exs
Change the router.ex
file:
scope "/", CryptoApiWeb do
pipe_through(:api)
resources("/coins", CoinController)
end
mix test
git push origin main
# run the database locally to run migrations
cloud_sql_proxy -instances=tests-322618:us-central1:crypto-api=tcp:3306
# run database create
SECRET_KEY_BASE=1234 MIX_ENV=prod DATABASE_URL=ecto://root@127.0.0.1:3306/crypto-api mix ecto.create
# run database migrate
SECRET_KEY_BASE=1234 MIX_ENV=prod DATABASE_URL=ecto://root@127.0.0.1:3306/crypto-api mix ecto.migrate
SECRET_KEY_BASE=1234 MIX_ENV=prod DATABASE_URL=ecto://root@127.0.0.1:3306/crypto-api mix run priv/repo/seeds.exs
Dockerfile
FROM bitwalker/alpine-elixir-phoenix:1.12.2
# Set mix env and ports
ENV MIX_ENV=prod
ENV PORT=4000
# Cache elixir deps
WORKDIR /app
COPY . .
RUN rm -rf _build && rm -rf .elixir_ls
RUN mix clean
RUN mix deps.get
RUN mix deps.compile
# Run frontend build, compile, and digest assets
# RUN cd assets/ && npm run deploy
# RUN mix do compile, phx.digest
CMD ["mix", "phx.server"]
cloudbuild.yaml
steps:
# build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/github.com/danicuki/crypto_api', '.']
# push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/github.com/danicuki/crypto_api']
# Deploy container image to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
args: ['beta', 'run', 'deploy', 'crypto-api',
'--image', 'gcr.io/$PROJECT_ID/github.com/danicuki/crypto_api',
'--region', 'us-central1', '--allow-unauthenticated', '--port=4000',
'--add-cloudsql-instances=tests-322618:us-central1:crypto-api',
'--update-secrets=DATABASE_URL=crypto_api:latest,SECRET_KEY_BASE=crypto_api:latest']
images:
- gcr.io/$PROJECT_ID/github.com/danicuki/crypto_api
add to prod.exs
config :crypto_api, CryptoApi.Repo,
database: "crypto-api",
socket: "/cloudsql/tests-322618:us-central1:crypto-api"
echo ecto://root@34.133.36.129/crypto-api? > database_secret.txt
gcloud secrets create crypto_api --data-file database_secret.txt
Set up a Cloud Build trigger Now you set up Cloud Build to build on every code change in your GitHub repository.
Go to the Cloud Build Triggers page in the Cloud Console.
Create a new trigger for your Phoenix app, following the on-screen prompts. On the Trigger settings page, set the Build configuration to Cloud Build configuration file; everything else can stay at the default values.
git push origin main
defmodule CryptoApiWeb.CoinView do
use CryptoApiWeb, :view
alias CryptoApiWeb.CoinView
def render("index.json", %{coins: coins}) do
render_many(coins, CoinView, "coin.json")
end
def render("show.json", %{coin: coin}) do
render_one(coin, CoinView, "coin.json")
end
def render("coin.json", %{coin: coin}) do
%{
id: coin.id,
ticker: coin.ticker,
name: coin.name,
price: coin.price
}
end
end
# catalog.ex
def get_coin!(ticker), do: Repo.get_by!(Coin, ticker: ticker)