Phoenix Web Development
上QQ阅读APP看书,第一时间看更新

Creating our Social Voting project

What we haven't done yet is actually create our Phoenix project, so let's do that! I'm going to call this application "Vocial" ("Social Voting" because I am incredibly creative)! We'll start off by running our mix task to create a Phoenix application:

$ mix phx.new vocial
* creating vocial/config/config.exs
* creating vocial/config/dev.exs
* creating vocial/config/prod.exs
* creating vocial/config/prod.secret.exs
* creating vocial/config/test.exs
* creating vocial/lib/vocial/application.ex
* creating vocial/lib/vocial.ex
* creating vocial/lib/vocial_web/channels/user_socket.ex
* creating vocial/lib/vocial_web/views/error_helpers.ex
* creating vocial/lib/vocial_web/views/error_view.ex
* creating vocial/lib/vocial_web/endpoint.ex
* creating vocial/lib/vocial_web/router.ex
* creating vocial/lib/vocial_web.ex
* creating vocial/mix.exs
* creating vocial/README.md
* creating vocial/test/support/channel_case.ex
* creating vocial/test/support/conn_case.ex
* creating vocial/test/test_helper.exs
* creating vocial/test/vocial_web/views/error_view_test.exs
* creating vocial/lib/vocial_web/gettext.ex
* creating vocial/priv/gettext/en/LC_MESSAGES/errors.po
* creating vocial/priv/gettext/errors.pot
* creating vocial/lib/vocial/repo.ex
* creating vocial/priv/repo/seeds.exs
* creating vocial/test/support/data_case.ex
* creating vocial/lib/vocial_web/controllers/page_controller.ex
* creating vocial/lib/vocial_web/templates/layout/app.html.eex
* creating vocial/lib/vocial_web/templates/page/index.html.eex
* creating vocial/lib/vocial_web/views/layout_view.ex
* creating vocial/lib/vocial_web/views/page_view.ex
* creating vocial/test/vocial_web/controllers/page_controller_test.exs
* creating vocial/test/vocial_web/views/layout_view_test.exs
* creating vocial/test/vocial_web/views/page_view_test.exs
* creating vocial/.gitignore
* creating vocial/assets/brunch-config.js
* creating vocial/assets/css/app.css
* creating vocial/assets/css/phoenix.css
* creating vocial/assets/js/app.js
* creating vocial/assets/js/socket.js
* creating vocial/assets/package.json
* creating vocial/assets/static/robots.txt
* creating vocial/assets/static/images/phoenix.png
* creating vocial/assets/static/favicon.ico

Fetch and install dependencies? [Yn] y
* running mix deps.get
* running mix deps.compile
* running cd assets && npm install && node node_modules/brunch/bin/brunch build

We are all set! Go into your application by running:

$ cd vocial

Then configure your database in config/dev.exs and run:

$ mix ecto.create

Start your Phoenix app with:

$ mix phx.server

You can also run your app inside IEx (Interactive Elixir) as:

$ iex -S mix phx.server

We want to answer Y to the question about fetching and installing dependencies since we will be building the HTML/JS/CSS components required for our application. This gives us a good baseline for our application. We'll want to follow the instructions to create our database and then start up our server just to make sure everything is good and has been created as expected:

$ cd vocial
$ mix ecto.create
Compiling 13 files (.ex)
Generated vocial app
The database for Vocial.Repo has been created

$ iex -S mix phx.server
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

[info] Running VocialWeb.Endpoint with Cowboy using http://0.0.0.0:4000
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 13:27:13 - info: compiled 6 files into 2 files, copied 3 in 3.4 sec

Assuming all went well, we should see the default Phoenix start page (the same one that we saw in Chapter 1, A brief Introduction to Elixir and Phoenix). Now, one option we have for putting a lot of this together is to run Phoenix's generators to create our controllers, views, templates, and contexts/schemas all automatically for us. Generally speaking, you should avoid using these for anything other than learning purposes, since they tend to come with a lot of boilerplate code and functionality that you're probably not going to need in the long run! Sometimes they can be nice for I need a website 30 minutes ago situations or base prototyping, but again, you tend to end up with a lot of extra cruft in your project that you may not need or use, but will have open and available regardless of your intentions! This is also the sort of thing that can lead to unintended security vulnerabilities down the road!