Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Upgrade to phoenix v1.5 #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
# set this value to 0 (zero).
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},

{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
Expand All @@ -88,7 +87,6 @@
{Credo.Check.Readability.VariableNames},
{Credo.Check.Readability.Semicolons},
{Credo.Check.Readability.SpaceAfterCommas},

{Credo.Check.Refactor.DoubleBooleanNegation},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.CyclomaticComplexity},
Expand All @@ -99,7 +97,6 @@
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.UnlessWithElse},

{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
Expand Down Expand Up @@ -128,7 +125,7 @@
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
{Credo.Check.Warning.NameRedeclarationByCase, false},
{Credo.Check.Warning.NameRedeclarationByDef, false},
{Credo.Check.Warning.NameRedeclarationByFn, false},
{Credo.Check.Warning.NameRedeclarationByFn, false}

# Custom checks can be created using `mix credo.gen.check`.
#
Expand Down
21 changes: 3 additions & 18 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
[
inputs: [
"mix.exs",
"{config,lib,test,priv}/**/*.{ex,exs}"
],
locals_without_parens: [
# Formatter tests
assert_format: 2,
assert_format: 3,
assert_same: 1,
assert_same: 2,

# Errors tests
assert_eval_raise: 3,

# Mix tests
in_fixture: 2,
in_tmp: 2
]
import_deps: [:ecto, :phoenix],
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
subdirectories: ["priv/*/migrations"]
]
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Updates to Elixir, Erlang and deps

- elixir 1.9.1
- erlang 22.1
- Circle CI config for elixir 1.9.1
- erlang 22.1
- Circle CI config for elixir 1.9.1
- Phoenix v1.3 -> v1.4
- Refactoring of several modules
- References:
- https://www.phoenixdiff.org/?source=1.3.0&target=1.4.3
- https://github.com/phoenixframework/phoenix/blob/v1.4/CHANGELOG.md
- Ecto v2 -> v3
- This upgrade includes the split into Ecto + Ecto_sql
- Comeonin v4 -> v5
- Comeonin changed how dependencies works
- basically we removed comeonein deps and function calls and replaced it with BCrypt ones
- Reference: https://github.com/riverrun/comeonin/blob/master/UPGRADE_v5.md

## [1.0.0] - 2019-04-10

Expand Down
5 changes: 4 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ config :real_world, RealWorldWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "9ueg5YcX8/LKzVUcDrXp5xpYuaBCUfZZAJ3/udC1LCoabotR3O1CJyf/u/6RLJ/N",
render_errors: [view: RealWorldWeb.ErrorView, accepts: ~w(json)],
pubsub: [name: RealWorld.PubSub, adapter: Phoenix.PubSub.PG2]
pubsub_server: [name: RealWorld.PubSub, adapter: Phoenix.PubSub.PG2]

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

config :real_world, RealWorldWeb.Guardian,
issuer: "RealWorld",
secret_key: "MDLMflIpKod5YCnkdiY7C4E3ki2rgcAAMwfBl0+vyC5uqJNgoibfQmAh7J3uZWVK",
Expand Down
5 changes: 2 additions & 3 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use Mix.Config

config :real_world, RealWorldWeb.Endpoint,
load_from_system_env: true,
http: [port: {:system, "PORT"}],
http: [:inet6, port: System.get_env("PORT") || 4000],
url: [scheme: "https", host: "https://real-world-example.herokuapp.com/", port: 443],
force_ssl: [rewrite_on: [:x_forwarded_proto]],
secret_key_base: Map.fetch!(System.get_env(), "SECRET_KEY_BASE")
secret_key_base: System.get_env("SECRET_KEY_BASE")

config :logger, level: :info

config :real_world, RealWorld.Repo,
adapter: Ecto.Adapters.Postgres,
url: System.get_env("DATABASE_URL"),
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
ssl: true
3 changes: 1 addition & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use Mix.Config
# We don't run a server during test. If one is required,
# you can enable the server option below.
config :real_world, RealWorldWeb.Endpoint,
http: [port: 4001],
http: [port: 4002],
server: false

# Print only warnings and errors during test
config :logger, level: :warn

# Configure your database
config :real_world, RealWorld.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "real_world_test",
Expand Down
5 changes: 2 additions & 3 deletions lib/real_world/accounts/encryption.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule RealWorld.Accounts.Encryption do
alias Comeonin.Bcrypt

def password_hashing(password), do: Bcrypt.hashpwsalt(password)
def validate_password(password, hash), do: Bcrypt.checkpw(password, hash)
def password_hashing(password), do: Bcrypt.hash_pwd_salt(password)
def validate_password(password, hash), do: Bcrypt.verify_pass(password, hash)
end
15 changes: 8 additions & 7 deletions lib/real_world/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ defmodule RealWorld.Application do
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec

# Define workers and child supervisors to be supervised
# List all child processes to be supervised
children = [

# Start the PubSub system
{Phoenix.PubSub, name: RealWorld.PubSub},
# Start the Ecto repository
supervisor(RealWorld.Repo, []),
RealWorld.Repo,
# Start the endpoint when the application starts
supervisor(RealWorldWeb.Endpoint, [])
# Start your own worker by calling: RealWorld.Worker.start_link(arg1, arg2, arg3)
# worker(RealWorld.Worker, [arg1, arg2, arg3]),
RealWorldWeb.Endpoint
# Starts a worker by calling: RealWorld.Worker.start_link(arg)
# {RealWorld.Worker, arg},
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
Expand Down
12 changes: 3 additions & 9 deletions lib/real_world/repo.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
defmodule RealWorld.Repo do
use Ecto.Repo, otp_app: :real_world

@doc """
Dynamically loads the repository url from the
DATABASE_URL environment variable.
"""
def init(_, opts) do
{:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
end
use Ecto.Repo,
otp_app: :real_world,
adapter: Ecto.Adapters.Postgres
end
4 changes: 2 additions & 2 deletions lib/real_world_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ defmodule RealWorldWeb do
quote do
use Phoenix.Controller, namespace: RealWorldWeb
import Plug.Conn
import RealWorldWeb.Router.Helpers
import RealWorldWeb.Gettext
alias RealWorldWeb.Router.Helpers, as: Routes
end
end

Expand All @@ -35,9 +35,9 @@ defmodule RealWorldWeb do
# Import convenience functions from controllers
import Phoenix.Controller, only: [get_flash: 2, view_module: 1]

import RealWorldWeb.Router.Helpers
import RealWorldWeb.ErrorHelpers
import RealWorldWeb.Gettext
alias RealWorldWeb.Router.Helpers, as: Routes
end
end

Expand Down
6 changes: 1 addition & 5 deletions lib/real_world_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ defmodule RealWorldWeb.UserSocket do
## Channels
# channel "room:*", RealWorldWeb.RoomChannel

## Transports
transport(:websocket, Phoenix.Transports.WebSocket)
# transport :longpoll, Phoenix.Transports.LongPoll

# Socket params are passed from the client and can
# be used to verify and authenticate a user. After
# verification, you can put default assigns into
Expand All @@ -19,7 +15,7 @@ defmodule RealWorldWeb.UserSocket do
#
# See `Phoenix.Token` documentation for examples in
# performing token verification on connect.
def connect(_params, socket) do
def connect(_params, socket, _connect_info) do
{:ok, socket}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/real_world_web/controllers/article_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule RealWorldWeb.ArticleController do

conn
|> put_status(:created)
|> put_resp_header("location", article_path(conn, :show, article))
|> put_resp_header("location", Routes.article_path(conn, :show, article))
|> render("show.json", article: article)
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/real_world_web/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ defmodule RealWorldWeb.FallbackController do
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
conn
|> put_status(:unprocessable_entity)
|> render(RealWorldWeb.ChangesetView, "error.json", changeset: changeset)
|> put_view(RealWorldWeb.ChangesetView)
|> render("error.json", changeset: changeset)
end

def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
|> render(RealWorldWeb.ErrorView, :"404")
|> put_view(RealWorldWeb.ErrorView)
|> render(:"404")
end
end
9 changes: 6 additions & 3 deletions lib/real_world_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ defmodule RealWorldWeb.SessionController do

conn
|> put_status(:created)
|> render(RealWorldWeb.UserView, "login.json", jwt: jwt, user: user)
|> put_view(RealWorldWeb.UserView)
|> render("login.json", jwt: jwt, user: user)

{:error, message} ->
conn
|> put_status(401)
|> render(RealWorldWeb.UserView, "error.json", message: message)
|> put_view(RealWorldWeb.UserView)
|> render("error.json", message: message)
end
end

def auth_error(conn, {_type, _reason}, _opts) do
conn
|> put_status(:forbidden)
|> render(RealWorldWeb.UserView, "error.json", message: "Not Authenticated")
|> put_view(RealWorldWeb.UserView)
|> render("error.json", message: "Not Authenticated")
end
end
38 changes: 10 additions & 28 deletions lib/real_world_web/endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
defmodule RealWorldWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :real_world

socket("/socket", RealWorldWeb.UserSocket)
socket "/socket", RealWorldWeb.UserSocket

# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phoenix.digest
# when deploying your static files in production.
plug(
Plug.Static,
plug Plug.Static,
at: "/",
from: :real_world,
gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)
)

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
plug(Phoenix.CodeReloader)
end

plug(Plug.RequestId)
plug(Plug.Logger)
plug Plug.RequestId
plug Plug.Logger

plug(
Plug.Parsers,
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison
)
json_decoder: Phoenix.json_library()

plug(Plug.MethodOverride)
plug(Plug.Head)
plug Plug.MethodOverride
plug Plug.Head

# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
plug(
Plug.Session,
plug Plug.Session,
store: :cookie,
key: "_real_world_key",
signing_salt: "Stn1Lrm3"
)

if Mix.env() == :dev do
plug(
Expand All @@ -52,17 +46,5 @@ defmodule RealWorldWeb.Endpoint do
)
end

plug(RealWorldWeb.Router)

@doc """
Dynamically loads configuration from the system environment
on startup.

It receives the endpoint configuration from the config files
and must return the updated configuration.
"""
def load_from_system_env(config) do
port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
{:ok, Keyword.put(config, :http, [:inet6, port: port])}
end
plug RealWorldWeb.Router
end
9 changes: 5 additions & 4 deletions lib/real_world_web/views/error_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ defmodule RealWorldWeb.ErrorView do
%{errors: %{detail: "Internal server error"}}
end

# In case no render clause matches or no
# template is found, let's render it as 500
def template_not_found(_template, assigns) do
render("500.json", assigns)
# By default, Phoenix returns the status message from
# the template name. For example, "404.html" becomes
# "Not Found".
def template_not_found(template, _assigns) do
Phoenix.Controller.status_message_from_template(template)
end
end
21 changes: 11 additions & 10 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule RealWorld.Mixfile do
defmodule RealWorld.MixProject do
use Mix.Project

def project do
Expand Down Expand Up @@ -38,18 +38,19 @@ defmodule RealWorld.Mixfile do
# Type `mix help deps` for examples and options.
defp deps do
[
{:phoenix, "~> 1.3.0"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.2"},
{:postgrex, "~> 0.13.3"},
{:phoenix, "~> 1.5.0"},
{:phoenix_pubsub, "~> 1.1"},
{:phoenix_ecto, "~> 4.0"},
{:ecto_sql, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
{:gettext, "~> 0.11"},
{:proper_case, "~> 1.0.0"},
{:cowboy, "~> 1.1"},
{:plug_cowboy, "~> 1.0.0"},
{:comeonin, "~> 3.2"},
{:proper_case, "~> 1.3"},
{:plug_cowboy, "~> 2.2"},
{:comeonin, "~> 5.0"},
{:bcrypt_elixir, "~> 2.0"},
{:guardian, "~> 1.0"},
{:excoveralls, "~> 0.7", only: [:dev, :test]},
{:credo, "~> 0.8.5", only: [:dev, :test]},
{:credo, "~> 1.0", only: [:dev, :test]},
{:ex_machina, "~> 2.0", only: :test},
{:ex_doc, "~> 0.16", only: :dev, runtime: false},
{:plug, "~> 1.0"},
Expand Down
Loading