Code Monkey home page Code Monkey logo

elixir-phoenix-realworld-example-app's Introduction

RealWorld Example App

Elixir (Phoenix) codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.

CircleCI codecov

This codebase was created to demonstrate a fully fledged backend application built with Elixir and Phoenix including CRUD operations, authentication, routing, pagination, and more.

We've gone to great lengths to adhere to the credo community styleguides & best practices.

For more information on how to this works with other frontends/backends, head over to the RealWorld repo.

Installing / Getting started

To run this project, you will need to install the following dependencies on your system:

To get started, run the following commands in your project folder:

cp config/dev.exs.example config/dev.exs  # creates the project's configuration file
mix deps.get  # installs the dependencies
mix ecto.create  # creates the database.
mix ecto.migrate  # run the database migrations.
mix phx.server  # run the application.

This is a backend project, you won't be able to go to localhost:4000 and see an aplication.

In order to see the Conduit frontend you will need to download and setup one of the frontend projects and set it up to consume this apps api. Typically this can be done by finding the API_URL and setting it to localhost:4000.

Tests

To run the tests for this project, simply run in your terminal:

mix test

Documentation

To generate the documentation, your can run in your terminal:

mix docs

This will generate a doc/ directory with a documentation in HTML. To view the documentation, open the index.html file in the generated directory.

Style guide

This project uses mix format. You can find the configuration file for the formatter in the .formatter.exs file.

Licensing

MIT © Ezinwa Okpoechi

elixir-phoenix-realworld-example-app's People

Contributors

515hikaru avatar brainmaestro avatar fcevado avatar florian95 avatar lbighetti avatar letitcrash avatar mattdharmon avatar rohanpujaris avatar shdblowers avatar sotojuan avatar viniciusilveira avatar zesky665 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elixir-phoenix-realworld-example-app's Issues

Errors updating articles

Using the vue.js front end I had errors updating articles:

https://github.com/gothinkster/elixir-phoenix-realworld-example-app/blob/master/lib/real_world_web/controllers/article_controller.ex#L66

  def update(conn, %{"id" => slug, "article" => article_params}, user) do
    article =
      slug
      |> Blog.get_article_by_slug!()
      |> Repo.preload([:author, :favorites])
      |> Blog.load_favorite(user)

    with {:ok, %Article{} = article} <- Blog.update_article(article, article_params) do
      render(conn, "show.json", article: article)
    end
  end

Modified to use slug to get the article.

Can't delete an article with a favorite count on it

Current Behaviour

As pointed out on #3, we currently can't delete an article with a favorite count on it

[error] #PID<0.499.0> running RealWorldWeb.Endpoint terminated
Server: localhost:4000 (http)
Request: DELETE //articles/hello-x
** (exit) an exception was raised:
    ** (Ecto.ConstraintError) constraint error when attempting to delete struct:

    * foreign_key: favorites_article_id_fkey

If you would like to convert this constraint into an error, please
call foreign_key_constraint/3 in your changeset and define the proper
constraint name. The changeset has not defined any constraint.

Expected Behaviour

Delete favourites while deleting the article.

Wrap to docker

Hi!
Should we wrap your example of app to docker?
If you find it is useful, please do not hesitate to contact me and I create .dockerfile and other configuration management actions for it!
:-)

Postgres error `role "postgres" does not exist` on running `mix ecto.create`

I'm a postgres noob and when following the README.md to run this locally on my macbook.
But on running the mix ecto.create command I get below error about a role "postgres" does not exist.

I have installed postgres using brew (and started it using pg_ctl -D /usr/local/var/postgres start as brew suggests after successful install).

14:37:22.569 [error] GenServer #PID<0.516.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification): role "postgres" does not exist
    (db_connection 1.1.3) lib/db_connection/connection.ex:163: DBConnection.Connection.connect/2
    (connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.13.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for RealWorld.Repo couldn't be created: FATAL 28000 (invalid_authorization_specification): role "postgres" does not exist

Provide some information about usage of application

Liked the code. Easy install, goes everything smooth and application starts. Now don't know where to go, what to open in browser which shows me the the working example.

I know it's a lot to ask but i am sure people new to elixir/erlang/phoenix would love to see it in action.

Error running command mix phx.server

Hi, Error When running mix phx.server - I am getting the following error though I have all the deps.get, compile steps complete.
Can someone help ?
** (Mix) Could not start application real_world: RealWorld.Application.start(:normal, []) returned an error: shutdown: failed to start child: RealWorldWeb.Endpoint
** (EXIT) shutdown: failed to start child: Phoenix.PubSub.PG2
** (EXIT) shutdown: failed to start child: Phoenix.PubSub.PG2Server
** (EXIT) an exception was raised:
** (UndefinedFunctionError) function :pg2.create/1 is undefined or private
(kernel 8.1.3) :pg2.create({:phx, RealWorld.PubSub})
(phoenix_pubsub 1.1.2) lib/phoenix/pubsub/pg2_server.ex:43: Phoenix.PubSub.PG2Server.init/1
(stdlib 3.16.1) gen_server.erl:423: :gen_server.init_it/2
(stdlib 3.16.1) gen_server.erl:390: :gen_server.init_it/6
(stdlib 3.16.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

I have done mix hex.local --force and still getting the error

Some Bugs

Migrated from https://github.com/lbighetti/elixir-phoenix-realworld/issues/43 opened by @mydearxym


1. can't display articles base on tag

tag is passed in (Parameters: %{"limit" => "10", "offset" => "0", "tag" => "elixir"}) , but you simply ignored all the params?

2. pagination is not work

reason is the same above

3. can't delete a article with a favorite count on it

[error] #PID<0.499.0> running RealWorldWeb.Endpoint terminated
Server: localhost:4000 (http)
Request: DELETE //articles/hello-x
** (exit) an exception was raised:
    ** (Ecto.ConstraintError) constraint error when attempting to delete struct:

    * foreign_key: favorites_article_id_fkey

If you would like to convert this constraint into an error, please
call foreign_key_constraint/3 in your changeset and define the proper
constraint name. The changeset has not defined any constraint.

plug_cowboy dependency missing

Hello, I cloned the repository then executed:

cp config/dev.exs.example config/dev.exs
mix deps.get
mix ecto.create
mix ecto.migrate
mix phx.server

I have the following errors when executing mix phx.server:

warning: please add the following dependency to your mix.exs:

{:plug_cowboy, "~> 1.0"}

This dependency is required by Plug.Adapters.Cowboy
which you may be using directly or indirectly.
Note you no longer need to depend on :cowboy directly.

[info] Application real_world exited: RealWorld.Application.start(:normal, []) returned an error: shutdown: failed to start child: RealWorldWeb.Endpoint
** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Handler
** (EXIT) "plug_cowboy dependency missing"
** (Mix) Could not start application real_world: RealWorld.Application.start(:normal, []) returned an error: shutdown: failed to start child: RealWorldWeb.Endpoint
** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Handler
** (EXIT) "plug_cowboy dependency missing"

Cockroachdb issues tracker

Trying cockroachdb on 2.1 beta (v2.1.0-beta.20181015):

  • Cannot create a database automatically, but can migrate with ecto.migrate (expected)
  • Getting list of tags fails
    Replaced query with:
  def list_tags do
    Ecto.Adapters.SQL.query!(Repo, "select count(*) as tag_count, ut.tag
    from articles, (select unnest(tag_list) from articles) as ut(tag)
    group by ut.tag
    order by tag_count desc limit 5;").rows
    |> Enum.map(fn v -> Enum.at(v, 1) end)
  end

Implement pagination on index and feed

Current Behaviour

As pointed out by #3 we are ignoring params passed to index and feed on ArticleController
Passing offset and limit will not produce pagination, neither does any default values or

  def index(conn, _params, user, _full_claims) do
    articles = Blog.list_articles()
               |> Repo.preload([:author, :favorites])
               |> Blog.load_favorites(user)
    render(conn, "index.json", articles: articles)
  end

Expected Behaviour

Return of index and feed should be produced with pagination, and params to use it such as limit and offset should work.
For example params = %{"limit" => "10", "offset" => "0"} should return the first page of the results with 10 items.

Implement Filters on index and feed

Current Behaviour

As pointed out by #3 we are ignoring params passed to index and feed on ArticleController
Passing tag for example will not filter the articles per tag

  def index(conn, _params, user, _full_claims) do
    articles = Blog.list_articles()
               |> Repo.preload([:author, :favorites])
               |> Blog.load_favorites(user)
    render(conn, "index.json", articles: articles)
  end

Expected Behaviour

Passing parameters to these functions should produce a filtered result.
For example params = %{"tag" => "elixir"} should return only articles with tag elixir

mix ecto.create on Fedora 30 and cannot find crti.o

Hi all,

I'm trying to run this example and am getting:

~/src/elixir-phoenix-realworld-example-app [master*]$ mix ecto.create
==> comeonin
mkdir -p priv
cc -g -O3 -Wall -I/usr/lib64/erlang/erts-10.3.5.5/include -Ic_src -fPIC -shared  c_src/bcrypt_nif.c c_src/blowfish.c -o priv/bcrypt_nif.so
/usr/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [Makefile:30: priv/bcrypt_nif.so] Error 1
could not compile dependency :comeonin, "mix compile" failed. You can recompile this dependency with "mix deps.compile comeonin", update it with "mix deps.update comeonin" or clean it with "mix deps.clean comeonin"
==> real_world
** (Mix) Could not compile with "make" (exit status: 2).
You need to have gcc and make installed. If you are using
Ubuntu or any other Debian-based system, install the packages
"build-essential". Also install "erlang-dev" package if not
included in your Erlang/OTP version. If you're on Fedora, run
"dnf group install 'Development Tools'".

I do have Development Tools installed and can see this:

ls -lh /usr/lib64/crti*
-rw-r--r-- 1 root root 1.8K Sep  4 20:48 /usr/lib64/crti.o

Any pointers?

Thanks.

The docs could be more helpful

Dependencies are Elixir, Phoenix, PostgreSQL, but Phoenix depends on Postgresql so the order isn't right
The Phoenix docs say to install mix local.hex but that doesn't work
I skip that and continue on your instructions, and mix deps.get offers to install hex. Goodie.
But then: mix ecto.create installs a lot but fails at

...
14:04:16.964 [error] GenServer #PID<0.7212.0> terminating
** (Postgrex.Error) FATAL 28P01 (invalid_password): password authentication failed for user "postgres"
    (db_connection 1.1.3) lib/db_connection/connection.ex:163: DBConnection.Connection.connect/2
    (connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.13) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for RealWorld.Repo couldn't be created: FATAL 28P01 (invalid_password): password authentication failed for user "postgres"

Presumably I should have created the postgres user, but this isn't mentioned anywhere.
But I rarely use postgres, so I wouldn't know where to begin

Phoenix.Router.NoRouteError) no route found for GET /phoenix/live_reload/socket/websocket (RealWorldWeb.Router)

elixir -v
Erlang/OTP 21 [erts-10.0.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Elixir 1.6.6 (compiled with OTP 20)

after follow-ing readme instructions & ensuring that there is no other elixir process running:

mix phx.server

[info] GET /phoenix/live_reload/socket/websocket
[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /phoenix/live_reload/socket/websocket (RealWorldWeb.Router)
(real_world) lib/real_world_web/router.ex:1: RealWorldWeb.Router.match_route/4
(real_world) lib/phoenix/router.ex:303: RealWorldWeb.Router.call/2
(real_world) lib/real_world_web/endpoint.ex:1: RealWorldWeb.Endpoint.plug_builder_call/2
(real_world) lib/plug/debugger.ex:102: RealWorldWeb.Endpoint."call (overridable 3)"/2
(real_world) lib/real_world_web/endpoint.ex:1: RealWorldWeb.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/sylvan/elixir-phoenix-realworld-example-app/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

Please note that mix test run with no issue.

Could not compile dependency :comeonin, "mix compile" failed.

Heeey,

I'm running the following command:
mix ecto.create

And the following occurs:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun could not compile dependency :comeonin, "mix compile" failed. You can recompile this dependency with "mix deps.compile comeonin", update it with "mix deps.update comeonin" or clean it with "mix deps.clean comeonin"

I already install make and nothing changes.

I'm using macOS 10.15.1

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.