Update to Phoenix 1.3.0

Signed-off-by: Rémy Coutable's avatarRémy Coutable <remy@rymai.me>
parent 327c98f3
Pipeline #14016361 failed with stage
in 3 minutes and 7 seconds
......@@ -7,18 +7,21 @@
# Generated on crash by the VM
erl_crash.dump
# Generated on crash by NPM
npm-debug.log
# Static artifacts
/node_modules
/assets/node_modules
# Since we are building assets from web/static,
# Since we are building assets from assets/,
# we ignore priv/static. You may want to comment
# this depending on your deployment strategy.
/priv/static/
# The config/prod.secret.exs file by default contains sensitive
# data and you should not commit it into version control.
# Files matching config/*.secret.exs pattern contain sensitive
# data and you should not commit them into version control.
#
# Alternatively, you may comment the line below and commit the
# secrets file as long as you replace its contents by environment
# secrets files as long as you replace their contents by environment
# variables.
/config/prod.secret.exs
/config/*.secret.exs
# Maldon
To start your Phoenix app:
To start your Phoenix server:
* Install dependencies with `mix deps.get`
* Create and migrate your database with `mix ecto.create && mix ecto.migrate`
* Install Node.js dependencies with `npm install`
* Start Phoenix endpoint with `mix phoenix.server`
* Install Node.js dependencies with `cd assets && yarn install`
* Start Phoenix endpoint with `mix phx.server`
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
......
......@@ -7,23 +7,20 @@ exports.config = {
// To use a separate vendor.js bundle, specify two files path
// http://brunch.io/docs/config#-files-
// joinTo: {
// "js/app.js": /^(web\/static\/js)/,
// "js/vendor.js": /^(web\/static\/vendor)|(deps)/
// "js/app.js": /^js/,
// "js/vendor.js": /^(?!js)/
// }
//
// To change the order of concatenation of files, explicitly mention here
// order: {
// before: [
// "web/static/vendor/js/jquery-2.1.1.js",
// "web/static/vendor/js/bootstrap.min.js"
// "vendor/js/jquery-2.1.1.js",
// "vendor/js/bootstrap.min.js"
// ]
// }
},
stylesheets: {
joinTo: "css/app.css",
order: {
after: ["web/static/css/app.css"] // concat app.css last
}
joinTo: "css/app.css"
},
templates: {
joinTo: "js/app.js"
......@@ -32,34 +29,30 @@ exports.config = {
conventions: {
// This option sets where we should place non-css and non-js assets in.
// By default, we set this to "/web/static/assets". Files in this directory
// By default, we set this to "/assets/static". Files in this directory
// will be copied to `paths.public`, which is "priv/static" by default.
assets: /^(web\/static\/assets)/
assets: /^(static)/
},
// Phoenix paths configuration
paths: {
// Dependencies and current project directories to watch
watched: [
"web/static",
"test/static"
],
watched: ["static", "css", "js", "vendor"],
// Where to compile files to
public: "priv/static"
public: "../priv/static"
},
// Configure your plugins
plugins: {
babel: {
// Do not use ES6 compiler in vendor code
ignore: [/web\/static\/vendor/]
ignore: [/vendor/]
}
},
modules: {
autoRequire: {
"js/app.js": ["web/static/js/app"]
"js/app.js": ["js/app"]
}
},
......
......@@ -17,11 +17,6 @@ body, form, ul, table {
/* Phoenix flash messages */
.alert:empty { display: none; }
/* Phoenix inline forms in links and buttons */
form.link, form.button {
display: inline;
}
/* Custom page header */
.header {
border-bottom: 1px solid #e5e5e5;
......@@ -79,4 +74,4 @@ form.link, form.button {
.jumbotron {
border-bottom: 0;
}
}
\ No newline at end of file
}
// NOTE: The contents of this file will only be executed if
// you uncomment its entry in "web/static/js/app.js".
// you uncomment its entry in "assets/js/app.js".
// To use Phoenix channels, the first step is to import Socket
// and connect at the socket path in "lib/my_app/endpoint.ex":
// and connect at the socket path in "lib/web/endpoint.ex":
import {Socket} from "phoenix"
let socket = new Socket("/socket", {params: {token: window.userToken}})
......@@ -13,7 +13,7 @@ let socket = new Socket("/socket", {params: {token: window.userToken}})
// If the current user exists you can assign the user's token in
// the connection for use in the layout.
//
// In your "web/router.ex":
// In your "lib/web/router.ex":
//
// pipeline :browser do
// ...
......@@ -31,12 +31,12 @@ let socket = new Socket("/socket", {params: {token: window.userToken}})
// end
//
// Now you need to pass this token to JavaScript. You can do so
// inside a script tag in "web/templates/layout/app.html.eex":
// inside a script tag in "lib/web/templates/layout/app.html.eex":
//
// <script>window.userToken = "<%= assigns[:user_token] %>";</script>
//
// You will need to verify the user token in the "connect/2" function
// in "web/channels/user_socket.ex":
// in "lib/web/channels/user_socket.ex":
//
// def connect(%{"token" => token}, socket) do
// # max_age: 1209600 is equivalent to two weeks in seconds
......
{
{
"repository": {},
"license": "MIT",
"scripts": {
......@@ -6,15 +6,13 @@
"watch": "brunch watch --stdin"
},
"dependencies": {
"phoenix": "file:deps/phoenix",
"phoenix_html": "file:deps/phoenix_html"
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html"
},
"devDependencies": {
"babel-brunch": "~6.0.0",
"brunch": "2.7.4",
"clean-css-brunch": "~2.0.0",
"css-brunch": "~2.0.0",
"javascript-brunch": "~2.0.0",
"uglify-js-brunch": "~2.0.1"
"babel-brunch": "6.1.1",
"brunch": "2.10.9",
"clean-css-brunch": "2.10.0",
"uglify-js-brunch": "2.10.0"
}
}
This diff is collapsed.
......@@ -10,10 +10,10 @@ config :maldon,
ecto_repos: [Maldon.Repo]
# Configures the endpoint
config :maldon, Maldon.Endpoint,
config :maldon, MaldonWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "eZa8aF+sGEZsj2vZU6QGu0fpucMUfG9cD7NbGU+s78E86HpMhHflrtTsM+L6I6Pk",
render_errors: [view: Maldon.ErrorView, accepts: ~w(html json)],
render_errors: [view: MaldonWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: Maldon.PubSub,
adapter: Phoenix.PubSub.PG2]
......
......@@ -6,23 +6,38 @@ use Mix.Config
# The watchers configuration can be used to run external
# watchers to your application. For example, we use it
# with brunch.io to recompile .js and .css sources.
config :maldon, Maldon.Endpoint,
config :maldon, MaldonWeb.Endpoint,
http: [port: 4000],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
cd: Path.expand("../", __DIR__)]]
cd: Path.expand("../assets", __DIR__)]]
# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# command from your terminal:
#
# openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout priv/server.key -out priv/server.pem
#
# The `http:` config above can be replaced with:
#
# https: [port: 4000, keyfile: "priv/server.key", certfile: "priv/server.pem"],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.
# Watch static and templates for browser reloading.
config :maldon, Maldon.Endpoint,
config :maldon, MaldonWeb.Endpoint,
live_reload: [
patterns: [
~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
~r{priv/gettext/.*(po)$},
~r{web/views/.*(ex)$},
~r{web/templates/.*(eex)$}
~r{lib/maldon_web/views/.*(ex)$},
~r{lib/maldon_web/templates/.*(eex)$}
]
]
......
use Mix.Config
# For production, we configure the host to read the PORT
# from the system environment. Therefore, you will need
# to set PORT=80 before running your server.
# For production, we often load configuration from external
# sources, such as your system environment. For this reason,
# you won't find the :http configuration below, but set inside
# MaldonWeb.Endpoint.init/2 when load_from_system_env is
# true. Any dynamic configuration should be done there.
#
# You should also configure the url host to something
# meaningful, we use this information when generating URLs.
# Don't forget to configure the url host to something meaningful,
# Phoenix uses this information when generating URLs.
#
# Finally, we also include the path to a manifest
# Finally, we also include the path to a cache manifest
# containing the digested version of static files. This
# manifest is generated by the mix phoenix.digest task
# manifest is generated by the mix phx.digest task
# which you typically run after static files are built.
config :maldon, Maldon.Endpoint,
http: [port: {:system, "PORT"}],
config :maldon, MaldonWeb.Endpoint,
load_from_system_env: true,
url: [host: "example.com", port: 80],
cache_static_manifest: "priv/static/manifest.json"
cache_static_manifest: "priv/static/cache_manifest.json"
# Do not print debug messages in production
config :logger, level: :info
......@@ -27,7 +29,8 @@ config :logger, level: :info
# config :maldon, Maldon.Endpoint,
# ...
# url: [host: "example.com", port: 443],
# https: [port: 443,
# https: [:inet6,
# port: 443,
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH")]
#
......@@ -38,7 +41,7 @@ config :logger, level: :info
# We also recommend setting `force_ssl`, ensuring no data is
# ever sent via http, always redirecting to https:
#
# config :maldon, Maldon.Endpoint,
# config :maldon, MaldonWeb.Endpoint,
# force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.
......@@ -53,7 +56,7 @@ config :logger, level: :info
# Alternatively, you can configure exactly which server to
# start per endpoint:
#
# config :maldon, Maldon.Endpoint, server: true
# config :maldon, MaldonWeb.Endpoint, server: true
#
# Finally import the config/prod.secret.exs
......
......@@ -10,7 +10,7 @@ config :maldon, Maldon.Endpoint,
config :logger, level: :warn
# Configure your database
config :maldon, Maldon.Repo,
config :maldon, MaldonWeb.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
......
defmodule Maldon do
use Application
@moduledoc """
SampleApp keeps the contexts that define your domain
and business logic.
# 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
children = [
# Start the Ecto repository
supervisor(Maldon.Repo, []),
# Start the endpoint when the application starts
supervisor(Maldon.Endpoint, []),
# Start your own worker by calling: Maldon.Worker.start_link(arg1, arg2, arg3)
# worker(Maldon.Worker, [arg1, arg2, arg3]),
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Maldon.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
Maldon.Endpoint.config_change(changed, removed)
:ok
end
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
end
defmodule Maldon.Application do
use Application
# See https://hexdocs.pm/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
children = [
# Start the Ecto repository
supervisor(Maldon.Repo, []),
# Start the endpoint when the application starts
supervisor(MaldonWeb.Endpoint, []),
# Start your own worker by calling: Maldon.Worker.start_link(arg1, arg2, arg3)
# worker(Maldon.Worker, [arg1, arg2, arg3]),
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Maldon.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
MaldonWeb.Endpoint.config_change(changed, removed)
:ok
end
end
defmodule Maldon.Repo do
use Ecto.Repo, otp_app: :maldon
@moduledoc """
In memory repository
"""
@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
#
# # def all(MaldonWeb.User) do
# # [%MaldonWeb.User{id: "1", name: "Rémy Coutable", username: "rymai", password: "secure"}]
# # end
# def all(_module), do: []
#
# def get(module, id) do
# Enum.find all(module), fn map -> map.id == id end
# end
#
# def get_by(module, params) do
# Enum.find all(module), fn map ->
# Enum.all?(params, fn {key, val} -> Map.get(map, key) == val end)
# end
# end
end
defmodule Maldon.Web do
defmodule MaldonWeb do
@moduledoc """
A module that keeps using definitions for controllers,
views and so on.
The entrypoint for defining your web interface, such
as controllers, views, channels and so on.
This can be used in your application as:
use Maldon.Web, :controller
use Maldon.Web, :view
use MaldonWeb, :controller
use MaldonWeb, :view
The definitions below will be executed for every view,
controller, etc, so keep them short and clean, focused
on imports, uses and aliases.
Do NOT define functions inside the quoted expressions
below.
below. Instead, define any helper function in modules
and import those modules here.
"""
def model do
quote do
use Ecto.Schema
import Ecto
import Ecto.Changeset
import Ecto.Query
end
end
# def model do
# quote do
# use Ecto.Schema
#
# import Ecto
# import Ecto.Changeset
# import Ecto.Query
# end
# end
def controller do
quote do
use Phoenix.Controller
use Phoenix.Controller, namespace: MaldonWeb
import Plug.Conn
alias Maldon.Repo
import Ecto
import Ecto.Query
import Maldon.Router.Helpers
import Maldon.Gettext
import MaldonWeb.Router.Helpers
import MaldonWeb.Gettext
end
end
def view do
quote do
use Phoenix.View, root: "web/templates"
use Phoenix.View, root: "lib/maldon_web/templates",
namespace: MaldonWeb
# Import convenience functions from controllers
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
import Phoenix.Controller, only: [get_flash: 2, view_module: 1]
# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML
import Maldon.Router.Helpers
import Maldon.ErrorHelpers
import Maldon.Gettext
import MaldonWeb.Router.Helpers
import MaldonWeb.ErrorHelpers
import MaldonWeb.Gettext
end
end
def router do
quote do
use Phoenix.Router
import Plug.Conn
import Phoenix.Controller
end
end
......@@ -68,7 +73,7 @@ defmodule Maldon.Web do
alias Maldon.Repo
import Ecto
import Ecto.Query
import Maldon.Gettext
import MaldonWeb.Gettext
end
end
......
defmodule Maldon.UserSocket do
defmodule MaldonWeb.UserSocket do
use Phoenix.Socket
## Channels
# channel "room:*", Maldon.RoomChannel
# channel "room:*", MaldonWeb.RoomChannel
## Transports
transport :websocket, Phoenix.Transports.WebSocket
......@@ -25,12 +25,12 @@ defmodule Maldon.UserSocket do
# Socket id's are topics that allow you to identify all sockets for a given user:
#
# def id(socket), do: "users_socket:#{socket.assigns.user_id}"
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
#
# Would allow you to broadcast a "disconnect" event and terminate
# all active sockets and channels for a given user:
#
# Maldon.Endpoint.broadcast("users_socket:#{user.id}", "disconnect", %{})
# MaldonWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
#
# Returning `nil` makes this socket anonymous.
def id(_socket), do: nil
......
defmodule Maldon.PageController do
use Maldon.Web, :controller
defmodule MaldonWeb.PageController do
use MaldonWeb, :controller
def index(conn, _params) do
render conn, "index.html"
......
defmodule Maldon.Endpoint do
defmodule MaldonWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :maldon
socket "/socket", Maldon.UserSocket
socket "/socket", MaldonWeb.UserSocket
# Serve at "/" the static files from "priv/static" directory.
#
......@@ -38,5 +38,20 @@ defmodule Maldon.Endpoint do
key: "_maldon_key",
signing_salt: "QsmSzY3G"
plug Maldon.Router
plug MaldonWeb.Router
@doc """
Callback invoked for dynamically configuring the endpoint.
It receives the endpoint configuration and checks if
configuration should be loaded from the system environment.
"""
def init(_key, config) do
if config[:load_from_system_env] do
port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
{:ok, Keyword.put(config, :http, [:inet6, port: port])}
else
{:ok, config}
end
end
end
defmodule Maldon.Gettext do
defmodule MaldonWeb.Gettext do
@moduledoc """
A module providing Internationalization with a gettext-based API.
By using [Gettext](https://hexdocs.pm/gettext),
your module gains a set of macros for translations, for example:
import Maldon.Gettext
import MaldonWeb.Gettext
# Simple translation
gettext "Here is the string to translate"
......
defmodule Maldon.Router do
use Maldon.Web, :router
defmodule MaldonWeb.Router do
use MaldonWeb, :router
pipeline :browser do
plug :accepts, ["html"]
......@@ -13,14 +13,16 @@ defmodule Maldon.Router do
plug :accepts, ["json"]
end
scope "/", Maldon do
scope "/", MaldonWeb do
pipe_through :browser # Use the default browser stack
get "/users", UserController, :index
get "/users/:id", UserController, :show
get "/", PageController, :index
end
# Other scopes may use custom stacks.
# scope "/api", Maldon do
# scope "/api", MaldonWeb do
# pipe_through :api
# end
end
<div class="jumbotron">
<h2><%= gettext "Welcome to %{name}", name: "Phoenix!" %></h2>
<h2><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h2>
<p class="lead">A productive web framework that<br />does not compromise speed and maintainability.</p>
</div>
......
defmodule Maldon.ErrorHelpers do
defmodule MaldonWeb.ErrorHelpers do
@moduledoc """
Conveniences for translating and building error messages.
"""
......@@ -9,9 +9,9 @@ defmodule Maldon.ErrorHelpers do
Generates tag for inlined form input errors.
"""
def error_tag(form, field) do
if error = form.errors[field] do
Enum.map(Keyword.get_values(form.errors, field), fn (error) ->
content_tag :span, translate_error(error), class: "help-block"
end
end)
end
@doc """
......@@ -32,9 +32,9 @@ defmodule Maldon.ErrorHelpers do
# dgettext "errors", "is invalid"
#
if count = opts[:count] do
Gettext.dngettext(Maldon.Gettext, "errors", msg, msg, count, opts)
Gettext.dngettext(MaldonWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(Maldon.Gettext, "errors", msg, opts)
Gettext.dgettext(MaldonWeb.Gettext, "errors", msg, opts)
end
end
end
defmodule Maldon.ErrorView do
use Maldon.Web, :view
defmodule MaldonWeb.ErrorView do
use MaldonWeb, :view
def render("404.html", _assigns) do
"Page not found"
......
defmodule MaldonWeb.LayoutView do
use MaldonWeb, :view
end
defmodule MaldonWeb.PageView do
use MaldonWeb, :view
end
......@@ -2,42 +2,47 @@ defmodule Maldon.Mixfile do
use Mix.Project
def project do
[app: :maldon,
version: "0.0.1",
elixir: "~> 1.2",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
aliases: aliases(),
deps: deps()]
[
app: :maldon,
version: "0.0.1",
elixir: "~> 1.4",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
aliases: aliases(),
deps: deps()
]
end
# Configuration for the OTP application.
#
# Type `mix help compile.app` for more information.
def application do
[mod: {Maldon, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex]]
[
mod: {Maldon.Application, []},
extra_applications: [:logger, :runtime_tools]
]
end
# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
defp elixirc_paths(_), do: ["lib", "web"]
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
# Specifies your project dependencies.
#
# Type `mix help deps` for examples and options.
defp