Skip to content

Commit be04363

Browse files
Merge pull request #45 from epochtalk/view-to-json
View to JSON
2 parents fff9476 + c01175c commit be04363

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+197
-215
lines changed

config/config.exs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ config :epochtalk_server, :redix,
6363
config :epochtalk_server, EpochtalkServerWeb.Endpoint,
6464
url: [host: "localhost"],
6565
render_errors: [
66-
view: EpochtalkServerWeb.ErrorView,
67-
format: "json",
68-
accepts: ~w(json),
66+
formats: [json: EpochtalkServerWeb.ErrorJSON],
6967
layout: false
7068
],
7169
pubsub_server: EpochtalkServer.PubSub,

config/prod.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ config :epochtalk_server, EpochtalkServerWeb.Endpoint,
1515
# Do not print debug messages in production
1616
config :logger, level: :info
1717

18+
config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: EpochtalkServer.Finch
19+
1820
# ## SSL Support
1921
#
2022
# To get SSL working, you will need to add the `https` key

config/runtime.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if config_env() == :prod do
2828
For example: ecto://USER:PASS@HOST/DATABASE
2929
"""
3030

31-
maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: []
31+
maybe_ipv6 = if System.get_env("ECTO_IPV6") in ~w(true 1), do: [:inet6], else: []
3232

3333
config :epochtalk_server, EpochtalkServer.Repo,
3434
# ssl: true,

config/test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ config :epochtalk_server, EpochtalkServerWeb.Endpoint,
2424
config :epochtalk_server, EpochtalkServer.Mailer, adapter: Swoosh.Adapters.Test
2525

2626
# Print only warnings and errors during test
27-
config :logger, level: :warn
27+
config :logger, level: :warning
2828

2929
# Initialize plugs at runtime for faster test compilation
3030
config :phoenix, :plug_init_mode, :runtime

lib/epochtalk_server/application.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ defmodule EpochtalkServer.Application do
2828
EpochtalkServerWeb.Telemetry,
2929
# Start the PubSub system
3030
{Phoenix.PubSub, name: EpochtalkServer.PubSub},
31+
# Start Finch
32+
{Finch, name: EpochtalkServer.Finch},
3133
# Start Presence for Phoenix channel tracking
3234
EpochtalkServerWeb.Presence,
3335
# Start the Endpoint (http/https)

lib/epochtalk_server_web.ex

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,26 @@ defmodule EpochtalkServerWeb do
1717
and import those modules here.
1818
"""
1919

20+
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
21+
2022
def controller do
2123
quote do
22-
use Phoenix.Controller, namespace: EpochtalkServerWeb
24+
use Phoenix.Controller,
25+
formats: [:html, :json, :js],
26+
layouts: [html: EpochtalkServerWeb.Layouts]
2327

2428
import Plug.Conn
25-
alias EpochtalkServerWeb.Router.Helpers, as: Routes
29+
30+
unquote(verified_routes())
2631
end
2732
end
2833

29-
def view do
34+
def js do
3035
quote do
31-
use Phoenix.View,
32-
root: "lib/epochtalk_server_web/templates",
33-
namespace: EpochtalkServerWeb
34-
35-
# Import convenience functions from controllers
36-
import Phoenix.Controller,
37-
only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]
36+
import EpochtalkServerWeb, only: [embed_templates: 2]
3837

39-
# Include shared imports and aliases for views
40-
unquote(view_helpers())
38+
# Routes generation with the ~p sigil
39+
unquote(verified_routes())
4140
end
4241
end
4342

@@ -56,13 +55,12 @@ defmodule EpochtalkServerWeb do
5655
end
5756
end
5857

59-
defp view_helpers do
58+
def verified_routes do
6059
quote do
61-
# Import basic rendering functionality (render, render_layout, etc)
62-
import Phoenix.View
63-
64-
import EpochtalkServerWeb.ErrorHelpers
65-
alias EpochtalkServerWeb.Router.Helpers, as: Routes
60+
use Phoenix.VerifiedRoutes,
61+
endpoint: EpochtalkServerWeb.Endpoint,
62+
router: EpochtalkServerWeb.Router,
63+
statics: EpochtalkServerWeb.static_paths()
6664
end
6765
end
6866

@@ -72,4 +70,16 @@ defmodule EpochtalkServerWeb do
7270
defmacro __using__(which) when is_atom(which) do
7371
apply(__MODULE__, which, [])
7472
end
73+
74+
defmacro embed_templates(pattern, opts) do
75+
quote do
76+
require Phoenix.Template
77+
78+
Phoenix.Template.compile_all(
79+
&(&1 |> Path.basename() |> Path.rootname() |> Path.rootname()),
80+
Path.expand(unquote(opts)[:root] || __DIR__, __DIR__),
81+
unquote(pattern) <> unquote(opts)[:ext]
82+
)
83+
end
84+
end
7585
end

lib/epochtalk_server_web/controllers/board_controller.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule EpochtalkServerWeb.BoardController do
2121
board_mapping <- BoardMapping.all(stripped: stripped),
2222
board_moderators <- BoardModerator.all(),
2323
categories <- Category.all() do
24-
render(conn, "by_category.json", %{
24+
render(conn, :by_category, %{
2525
categories: categories,
2626
board_moderators: board_moderators,
2727
board_mapping: board_mapping,
@@ -42,7 +42,7 @@ defmodule EpochtalkServerWeb.BoardController do
4242
board_moderators <- BoardModerator.all(),
4343
{:board, [_board]} <-
4444
{:board, Enum.filter(board_mapping, fn bm -> bm.board_id == id end)} do
45-
render(conn, "find.json", %{
45+
render(conn, :find, %{
4646
board_moderators: board_moderators,
4747
board_mapping: board_mapping,
4848
board_id: id,
@@ -60,7 +60,7 @@ defmodule EpochtalkServerWeb.BoardController do
6060
def slug_to_id(conn, attrs) do
6161
with slug <- Validate.cast(attrs, "slug", :string, required: true),
6262
{:ok, id} <- Board.slug_to_id(slug) do
63-
render(conn, "slug_to_id.json", data: %{id: id})
63+
render(conn, :slug_to_id, id: id)
6464
else
6565
{:error, :board_does_not_exist} ->
6666
ErrorHelpers.render_json_error(conn, 400, "Error, cannot board does not exist")

lib/epochtalk_server_web/controllers/configuration_controller.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ defmodule EpochtalkServerWeb.ConfigurationController do
1010
"""
1111
def config(conn, _attrs) do
1212
conn
13-
|> put_resp_header("content-type", "text/javascript")
14-
|> render("config.js", config: Application.get_env(:epochtalk_server, :frontend_config))
13+
|> Phoenix.Controller.put_format(:js)
14+
|> put_resp_content_type("content-type", "text/javascript")
15+
|> render(:config, config: Application.get_env(:epochtalk_server, :frontend_config))
1516
end
1617
end

lib/epochtalk_server_web/controllers/mention_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule EpochtalkServerWeb.MentionController do
2222
{:ok, mentions, data} <-
2323
Mention.page_by_user_id(user.id, page, per_page: limit, extended: extended),
2424
do:
25-
render(conn, "page.json", %{
25+
render(conn, :page, %{
2626
mentions: mentions,
2727
pagination_data: data,
2828
extended: extended

lib/epochtalk_server_web/controllers/moderation_log_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule EpochtalkServerWeb.ModerationLogController do
1717
page <- Validate.cast(attrs, "page", :integer, min: 1),
1818
limit <- Validate.cast(attrs, "limit", :integer, min: 1),
1919
{:ok, moderation_logs, data} <- ModerationLog.page(attrs, page, per_page: limit) do
20-
render(conn, "page.json", moderation_logs: moderation_logs, pagination_data: data)
20+
render(conn, :page, %{moderation_logs: moderation_logs, pagination_data: data})
2121
else
2222
{:auth, false} ->
2323
ErrorHelpers.render_json_error(conn, 400, "Not logged in, cannot page moderation log")

0 commit comments

Comments
 (0)