Git Product home page Git Product logo

ignite_rockelivery's Introduction

Rocket Seat Ignite - Rockelivery

Elixir CI codecov

Table of Contents

About

A food delivery backend made with Elixir and Phoenix.

Endpoints

Built-in:

Welcome

Users

Items

Setup

  1. Install asdf, an extensible version manager for node, clojure, elixir and ruby:
# Install apt-get packages
$ sudo apt install curl git

# Clone asdf
$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.8.0

# Add it to your path
$ echo '# asdf version manager' >> ~/.bashrc
$ echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
$ echo '. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc
  1. Install Erlang, Elixir and Phoenix:
# Install apt-get packages
$ sudo apt-get -y install build-essential autoconf m4 libncurses5-dev \
  libwxgtk3.0-gtk3-dev libgl1-mesa-dev libglu1-mesa-dev libpng-dev libssh-dev \
  unixodbc-dev xsltproc fop libxml2-utils libncurses-dev openjdk-11-jdk

# Add plugins
$ asdf plugin-add erlang
$ asdf plugin-add elixir

# Install and set Elixir
$ asdf install elixir 1.11.3
$ asdf global elixir 1.11.3

# Install and set Erlang
$ asdf install erlang 23.2
$ asdf global erlang 23.2

# Verify installation
$ erl --version
$ elixir --version

# Install Phoenix
$ mix archive.install hex phx_new 1.5.8

Postgres

Create a postgress docker container with:

$ docker run --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres

Bash Commands

# Create phoenix app without webpacker or html views
$ mix phx.new app_name --no-webpack --no-html

# Intall dependencies
$ mix deps.get

# Compile project
$ mix compile

# Generate linter config file
$ mix credo.gen.config

# Run linter
$ mix credo --strict

# Start Phoenix dev server on http://localhost:4000
$ mix phx.server

# Start your project as an Interactive Elixir session
$ iex -S mix

# List all configured routes
$ mix phx.routes

# Generate JWT secret
$ mix guardian.gen.secret

Ecto

# Create and migrate database
$ mix ecto.setup

# Create a migration
$ mix ecto.gen.migration migration_name

# Run pending migrations
$ mix ecto.migrate

# Drop and migrate databases
$ mix ecto.reset

# Drop databases
$ mix ecto.drop

# Create databases
$ mix ecto.create

Tests

# Run tests
$ mix test

# Run tests w/ coverage report
$ mix test --cover

# Generate coverage report w/ excoveralls
$ mix coveralls

# Generate and save coverage report as html
$ mix coveralls.html

Local release

export SECRET_KEY_BASE="$(mix phx.gen.secret)"
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/rockelivery_dev"
MIX_ENV=prod mix release
MIX_ENV=prod APP_NAME=rockelivery PORT=4000 _build/prod/rel/rockelivery/bin/rockelivery start

Gigalixir deploy

APP_NAME=$(gigalixir create --name rockelivery)
gigalixir apps
git remote -v
echo "elixir_version=1.11.2" > elixir_buildpack.config
echo "erlang_version=23.2" >> elixir_buildpack.config
gigalixir pg:create --free
gigalixir pg
gigalixir config

# Deploy
git push gigalixir
gigalixir ps
gigalixir open
gigalixir logs -a rockelivery

# Database
gigalixir account:ssh_keys:add "$(cat ~/.ssh/id_rsa.pub)"
gigalixir ps:migrate

Remote console

gigalixir ps:remote_console

System.cmd("whoami", [])
System.cmd("echo", ["hello"], into: IO.stream(:stdio, :line))
seed_script = Path.join(["#{:code.priv_dir(:rockelivery)}", "repo", "seeds.exs"])
Code.eval_file(seed_script)

Elixir Commands

Users

Create a user:

> user_params = %{
  name: "Luisito",
  age: 40,
  email: "[email protected]",
  cpf: "12345678910",
  address: "paper street 123",
  cep: "01001000",
  password: "password"
}
> Rockelivery.Users.Create.call(user_params)

Fetch users:

> Rockelivery.Repo.all(Rockelivery.User)
> Rockelivery.Repo.get(Rockelivery.User, "8623cdd8-7cad-43cc-953b-c30260a349f4")

> Rockelivery.Users.Get.by_id("8623cdd8-7cad-43cc-953b-c30260a349f4")
> Rockelivery.Users.Get.by_id("8623cdd8-7cad-43cc-953b-c30260a349f8")
> Rockelivery.Users.Get.by_id("8623cdd8")

> Rockelivery.Users.GetV2.by_id("8623cdd8-7cad-43cc-953b-c30260a349f4")
> Rockelivery.Users.GetV2.by_id("8623cdd8-7cad-43cc-953b-c30260a349f8")
> Rockelivery.Users.GetV2.by_id("8623cdd8")

> Rockelivery.Users.GetV3.by_id("8623cdd8-7cad-43cc-953b-c30260a349f4")
> Rockelivery.Users.GetV3.by_id("8623cdd8-7cad-43cc-953b-c30260a349f8")
> Rockelivery.Users.GetV3.by_id("8623cdd8")

Update a user:

> updated_user_params = %{
  "id" => "9a519870-ff34-40ff-b24d-a0a387c8e73a",
  "name" => "UPDATEDDDDDD",
  "age" => 40,
  "email" => "[email protected]",
  "cpf" => "UPDATEDDDDD",
  "address" => "UPDATEDDDDDD",
  "cep" => "UPDATEDD"
}
> Rockelivery.Users.Update.call(updated_user_params)

Delete a user:

> Rockelivery.Users.Delete.call("8623cdd8-7cad-43cc-953b-c30260a349f4")
> Rockelivery.Users.GetV3.by_id("8623cdd8-7cad-43cc-953b-c30260a349f8")
> Rockelivery.Users.GetV3.by_id("8623cdd8")

Verify password:

> {:ok, %{password_hash: hash}} = Rockelivery.Users.Get.by_id("a2a60a12-1c03-4357-b370-c78ed28a9e38")
> Pbkdf2.verify_pass("password", hash)
true
> Pbkdf2.verify_pass("badddddd", hash)
false

> params = %{
	"id" => "a2a60a12-1c03-4357-b370-c78ed28a9e38",
	"password" => "password"
}
> RockeliveryWeb.Auth.Guardian.authenticate(params)
> bad_params = %{
	"id" => "a2a60a12-1c03-4357-b370-c78ed28a9e38",
	"password" => "badddddd"
}
> RockeliveryWeb.Auth.Guardian.authenticate(bad_params)

Orders

Orders report:

> Rockelivery.Orders.Report.create()

Misc

Tesla HTTP client:

> Tesla.get("https://api.github.com/users/danilo-vieira/repos")
> Tesla.get("https://viacep.com.br/ws/01001000/json/")
> Tesla.get("https://viacep.com.br/ws/01001-000/json/")
> Tesla.get("https://viacep.com.br/ws/00000000/json/")
> Tesla.get("https://viacep.com.br/ws/123/json/")

ViaCep client:

> ViaCEP.Client.get_cep_info("01001000")
> ViaCEP.Client.get_cep_info("01001-000")
> ViaCEP.Client.get_cep_info("00000000")
> ViaCEP.Client.get_cep_info("123")

Stack server:

> {:ok, pid} = Rockelivery.Stack.start_link()
> Rockelivery.Stack.push(pid, :world)
> Rockelivery.Stack.push_async(pid, :world)
> Rockelivery.Stack.pop(pid)
> Rockelivery.Stack.pop_async(pid)

Enum:

> Enum.chunk_every([1, 2, 3, 4, 5, 6, 7, 8], 3) |> Enum.flat_map(fn [_head | tails] -> tails end)

Libs

Docs

CI

Deploy

Resources

ignite_rockelivery's People

Contributors

librity avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

fpolica91

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.