Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating repo to newest version of Phoenix #3

Open
wants to merge 8 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
9 changes: 5 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
use Mix.Config

# Configures the endpoint
config :bird_watch, BirdWatch.Endpoint,
config :bird_watch, BirdWatchWeb.Endpoint,
url: [host: "localhost"],
root: Path.dirname(__DIR__),
secret_key_base: "uATboE7xe64yJpO+0VgSI56MDhQBILIWcRwr+CL+01K9PNyLPyolPDKoAlbjLHnQ",
pubsub_server: BirdWatch.PubSub,
render_errors: [accepts: ~w(html json)],
pubsub: [name: BirdWatch.PubSub,
adapter: Phoenix.PubSub.PG2]
live_view: [signing_salt: "HNKcYMPI"]

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

config :phoenix, :json_library, Poison

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
12 changes: 7 additions & 5 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 :bird_watch, BirdWatch.Endpoint,
config :bird_watch, BirdWatchWeb.Endpoint,
http: [port: 4000],
debug_errors: true,
code_reloader: true,
Expand All @@ -15,12 +15,12 @@ config :bird_watch, BirdWatch.Endpoint,
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin"]]

# Watch static and templates for browser reloading.
config :bird_watch, BirdWatch.Endpoint,
config :bird_watch, BirdWatchWeb.Endpoint,
live_reload: [
patterns: [
~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
~r{web/views/.*(ex)$},
~r{web/templates/.*(eex)$}
~r{lib/birdwatch_web/(live|views)/.*(ex)$},
~r{lib/birdwatch_web/templates/.*(eex)$}
]
]

Expand All @@ -37,4 +37,6 @@ config :couchdb_connector,
database: "birds_dev",
hostname: "localhost",
protocol: "http",
port: 5984
port: 5984,
user: "admin",
password: "admin"
6 changes: 4 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use Mix.Config

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :bird_watch, BirdWatch.Endpoint,
config :bird_watch, BirdWatchWeb.Endpoint,
http: [port: 4001],
server: false

Expand All @@ -14,4 +14,6 @@ config :couchdb_connector,
database: "birds_test",
hostname: "localhost",
protocol: "http",
port: 5984
port: 5984,
user: "admin",
password: "admin"
11 changes: 6 additions & 5 deletions lib/bird_watch.ex → lib/bird_watch/application.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
defmodule BirdWatch do
defmodule BirdWatch.Application do
use Application

# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false

children = [
BirdWatchWeb.Telemetry,
# Start the endpoint when the application starts
supervisor(BirdWatch.Endpoint, []),
BirdWatchWeb.Endpoint,
# Start the PubSub system
{Phoenix.PubSub, name: BirdWatch.PubSub}
# Here you could define other workers and supervisors as children
# worker(BirdWatch.Worker, [arg1, arg2, arg3]),
]
Expand All @@ -22,7 +23,7 @@ defmodule BirdWatch do
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
BirdWatch.Endpoint.config_change(changed, removed)
BirdWatchWeb.Endpoint.config_change(changed, removed)
:ok
end
end
88 changes: 88 additions & 0 deletions lib/bird_watch/couchdb.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
defmodule BirdWatch.Couchdb do
@database_properties %{
protocol: Application.compile_env(:couchdb_connector, :protocol),
hostname: Application.compile_env(:couchdb_connector, :hostname),
database: Application.compile_env(:couchdb_connector, :database),
user: Application.compile_env(:couchdb_connector, :user),
password: Application.compile_env(:couchdb_connector, :password),
port: Application.compile_env(:couchdb_connector, :port)
}

@server_uri "#{@database_properties[:protocol]}://#{@database_properties[:hostname]}:#{
@database_properties[:port]
}/"
@database_uri "#{@server_uri}/#{@database_properties[:database]}"

alias Couchdb.Connector.Reader
alias Couchdb.Connector.Writer
alias Couchdb.Connector.View
alias Couchdb.Connector.Storage

def test_database_server_connection do
case HTTPoison.get(@server_uri) do
{:error, %HTTPoison.Error{reason: :econnrefused}} ->
raise RuntimeError, message: "Connection refused. Is the database running?"

{:ok, %HTTPoison.Response{status_code: 200}} ->
{:ok, :connection_accepted}
end
end

def create do
case HTTPoison.get("#{@database_uri}") do
{:ok, %HTTPoison.Response{status_code: 200}} ->
{:ok, :database_exists}

{:ok, %HTTPoison.Response{status_code: 404}} ->
case Storage.storage_up(@database_properties) do
{:ok, body} -> {:ok, body}
{:error, body} -> {:error, body}
end
end
end

def destroy! do
Couchdb.Connector.Storage.storage_down(@database_properties)
end

def insert(document) do
Writer.create_generate(@database_properties, document)
end

def read(id) do
{:ok, bird_json} = Reader.get(@database_properties, id)
bird_json
end

def all_birds do
{:ok, birds_json} = View.fetch_all(@database_properties, "bird", "by_link")
birds_json
end

def bird_by_link(link) do
{:ok, birds_json} =
View.document_by_key(
@database_properties,
%{design: "bird", view: "by_link", key: link}
)

result_set = Poison.Parser.parse!(birds_json)

case result_set["rows"] do
[] -> not_found()
_ -> first_result(result_set)
end
end

defp not_found do
{:error, :not_found}
end

defp first_result(result_set) do
{:ok, hd(result_set["rows"])["value"]}
end

def create_view(design_name, code) do
View.create_view(@database_properties, design_name, code)
end
end
37 changes: 0 additions & 37 deletions lib/bird_watch/endpoint.ex

This file was deleted.

2 changes: 2 additions & 0 deletions lib/birdwatch.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
defmodule BirdWatch do
end
8 changes: 4 additions & 4 deletions web/web.ex → lib/birdwatch_web.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule BirdWatch.Web do
defmodule BirdWatchWeb do
@moduledoc """
A module that keeps using definitions for controllers,
views and so on.
Expand Down Expand Up @@ -26,21 +26,21 @@ defmodule BirdWatch.Web do
quote do
use Phoenix.Controller

import BirdWatch.Router.Helpers
import BirdWatchWeb.Router.Helpers
end
end

def view do
quote do
use Phoenix.View, root: "web/templates"
use Phoenix.View, root: "lib/birdwatch_web/templates"

# Import convenience functions from controllers
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]

# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML

import BirdWatch.Router.Helpers
import BirdWatchWeb.Router.Helpers
end
end

Expand Down
11 changes: 11 additions & 0 deletions lib/birdwatch_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule BirdWatchWeb.UserSocket do
use Phoenix.Socket

@impl true
def connect(_params, socket, _connect_info) do
{:ok, socket}
end

@impl true
def id(_socket), do: nil
end
13 changes: 13 additions & 0 deletions lib/birdwatch_web/controllers/bird_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule BirdWatchWeb.BirdController do
use BirdWatchWeb, :controller

alias BirdWatch.Couchdb

def index(conn, _params) do
birds =
Couchdb.all_birds()
|> Poison.Parser.parse!()

render(conn, "index.html", birds: birds)
end
end
57 changes: 57 additions & 0 deletions lib/birdwatch_web/endpoint.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule BirdWatchWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :bird_watch

# 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.
@session_options [
store: :cookie,
key: "_bird_watch_key",
signing_salt: "kU6D9s8w"
]

socket("/socket", BirdWatchWeb.UserSocket,
websocket: true,
longpoll: false
)

socket("/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]])

# 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,
at: "/",
from: :bird_watch,
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
socket("/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket)
plug(Phoenix.LiveReloader)
plug(Phoenix.CodeReloader)
end

plug(Phoenix.LiveDashboard.RequestLogger,
param_key: "request_logger",
cookie_key: "request_logger"
)

plug(Plug.RequestId)
plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])

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

plug(Plug.MethodOverride)
plug(Plug.Head)
plug(Plug.Session, @session_options)
plug(BirdWatchWeb.Router)
end
1 change: 0 additions & 1 deletion web/models/bird.ex → lib/birdwatch_web/models/bird.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule BirdWatch.Bird do

@derive [Poison.Encoder]
defstruct [:_id, :name, :link, :location, :image, :date, :attribution, :attribution_link]
end
30 changes: 30 additions & 0 deletions lib/birdwatch_web/router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule BirdWatchWeb.Router do
import Phoenix.LiveDashboard.Router

use BirdWatchWeb, :router

pipeline :browser do
plug(:accepts, ["html"])
plug(:fetch_session)
plug(:fetch_flash)
plug(:protect_from_forgery)
plug(:put_secure_browser_headers)
end

pipeline :api do
plug(:accepts, ["json"])
end

scope "/", BirdWatchWeb do
# Use the default browser stack
pipe_through(:browser)

get("/", BirdController, :index)
live_dashboard("/dashboard", metrics: BirdWatchWeb.Telemetry)
end

# Other scopes may use custom stacks.
# scope "/api", BirdWatchWeb do
# pipe_through :api
# end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading