Elixir Phoenix 的演示,如何启动和运行它,以及如何通过 Gigalixir 进行部署。
介绍性说明:
有多种安装方式:
验证版本至少是这些:
erl --version
Erlang/OTP 27 …
elixir --version
Elixir 1.17.3 …
psql --version
psql (PostgreSQL) 16.4
node --version
v22.9.0
为了使本地 PostgreSQL 设置更强大、更适合此演示,请创建一个新角色。
角色名称为“demo_elixir_phoenix”,因为它与该项目名称匹配。
角色密码可以是强密码,因为这是一种良好的安全实践。
生成一个强密码,例如 32 个十六进制数字:
printf " %sn " $( LC_ALL=C < /dev/urandom tr -dc ' 0-9a-f ' | head -c32 )
输出示例:
a9ed78cd8e4aa2bd2a37ad7319899106
为了使本地 PostgreSQL 设置更强大、更适合此演示,请创建一个新角色。
角色名称为“demo_elixir_phoenix”,因为它与该项目名称匹配。
角色密码可以是强密码,因为这是一种良好的安全实践。
生成一个强密码,例如 32 个十六进制数字:
printf " %sn " $( LC_ALL=C < /dev/urandom tr -dc ' 0-9a-f ' | head -c32 )
输出示例:
a9ed78cd8e4aa2bd2a37ad7319899106
通过典型的 PostgreSQL 选项连接:
psql --username postgres postgres
通过 macOS brew PostgreSQL 选项连接:
psql --username " $USER " postgres
使用 psql 创建角色:
postgres=# CREATE ROLE demo_elixir_phoenix WITH CREATEDB LOGIN ENCRYPTED PASSWORD 'a9ed78cd8e4aa2bd2a37ad7319899106';
核实:
postgres=# du demo_elixir_phoenix
List of roles
Role name | Attributes
---------------------+------------
demo_elixir_phoenix | Create DB
更新mix
即 Elixir 包管理器:
mix local.hex --force
输出:
* creating …/.mix/archives/hex-2.1.1
安装 Phoenix 新项目生成器:
mix archive.install hex phx_new
输出:
* creating ~/.mix/archives/phx_new-1.7.14
核实:
mix phx.new -v
输出:
Phoenix installer v1.7.14
新的:
mix phx.new demo-elixir-phoenix --app demo_elixir_phoenix
cd demo-elixir-phoenix
更改文件config/dev.exs
中的数据库配置。
从默认的用户名和密码:
username : "postgres" ,
password: "postgres" ,
进入自定义用户名和密码:
username : "demo_elixir_phoenix" ,
password: "a9ed78cd8e4aa2bd2a37ad7319899106" ,
跑步:
mix ecto.create
The database for DemoElixirPhoenix.Repo has been created
对文件config/test.exs
执行相同的用户名和密码更改。
如果您收到此错误:
** (Mix) The task " ecto.create " could not be found
那么您可能在错误的目录中运行命令;验证您正在应用程序的基目录中运行该命令。
如果您收到此错误:
** (Mix) The database for DemoElixirPhoenix.Repo couldn ' t be created: an exception was raised:
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
然后在这里进行故障排除:
跑步:
mix test
输出:
…
Finished in 0.03 seconds (0.01s async, 0.02s sync)
5 tests, 0 failures
跑步:
mix phx.server
[info] Running DemoElixirPhoenixWeb.Endpoint with Bandit 1.5.5 at 127.0.0.1:4000 (http)
[info] Access DemoElixirPhoenixWeb.Endpoint at http://localhost:4000
[watch] build finished, watching for changes...
Rebuilding...
Done in 166ms.
浏览:
您应该会看到一个欢迎页面。
如果您收到此错误:
Error: Brunch 2+ requires node.js v4 or higher …
Upgrade node or use older brunch for old node.js: npm i -g brunch@1
然后更新 Node、NPM 和 brunch:
brew update
brew uninstall node
sudo rm -rf /usr/local/lib/node_modules
brew install node --with-full-icu
npm install -g npm
npm install -g brunch
您可能想要列出服务器路由:
mix phx.routes
输出:
GET / DemoElixirPhoenixWeb.PageController :home
GET /dev/dashboard/css-:md5 Phoenix.LiveDashboard.Assets :css
GET /dev/dashboard/js-:md5 Phoenix.LiveDashboard.Assets :js
GET /dev/dashboard Phoenix.LiveDashboard.PageLive :home
GET /dev/dashboard/:page Phoenix.LiveDashboard.PageLive :page
GET /dev/dashboard/:node/:page Phoenix.LiveDashboard.PageLive :page
* /dev/mailbox Plug.Swoosh.MailboxPreview []
WS /live/websocket Phoenix.LiveView.Socket
GET /live/longpoll Phoenix.LiveView.Socket
POST /live/longpoll Phoenix.LiveView.Socket
生成完整 HTML 资源的所有代码:ecto 迁移、ecto 模型、控制器、视图和模板。
mix phx.gen.html Account User users
name:string
email:string
输出:
* creating lib/demo_elixir_phoenix_web/controllers/user_controller.ex
* creating lib/demo_elixir_phoenix_web/controllers/user_html/edit.html.heex
* creating lib/demo_elixir_phoenix_web/controllers/user_html/index.html.heex
* creating lib/demo_elixir_phoenix_web/controllers/user_html/new.html.heex
* creating lib/demo_elixir_phoenix_web/controllers/user_html/show.html.heex
* creating lib/demo_elixir_phoenix_web/controllers/user_html/user_form.html.heex
* creating lib/demo_elixir_phoenix_web/controllers/user_html.ex
* creating test/demo_elixir_phoenix_web/controllers/user_controller_test.exs
* creating lib/demo_elixir_phoenix/account/user.ex
* creating priv/repo/migrations/20240625211751_create_users.exs
* creating lib/demo_elixir_phoenix/account.ex
* injecting lib/demo_elixir_phoenix/account.ex
* creating test/demo_elixir_phoenix/account_test.exs
* injecting test/demo_elixir_phoenix/account_test.exs
* creating test/support/fixtures/account_fixtures.ex
* injecting test/support/fixtures/account_fixtures.ex
我们希望迁移时间戳出现在其余字段之前,因此我们编辑迁移:
edit priv/repo/migrations/ * _create_users.exs
我们的结果如下所示:
defmodule DemoElixirPhoenix.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table ( :users ) do
timestamps ( type: :utc_datetime )
add :name , :string
add :email , :string
end
end
end
请参阅路由器文件lib/demo_elixir_phoenix_web/router.ex
。
默认为:
scope "/" , DemoElixirPhoenixWeb do
pipe_through :browser
get "/" , PageController , :home
end
编辑路由器:
edit lib/demo_elixir_phoenix_web/router.ex `
将用户资源添加到浏览器范围:
scope "/" , DemoElixirPhoenixWeb do
pipe_through :browser
resources "/users" , UserController
get "/" , PageController , :home
end
迁移:
mix ecto.migrate
输出:
Generated demo_elixir_phoenix app
[info] == Running … DemoElixirPhoenix.Repo.Migrations.CreateUsers.change/0 forward
[info] create table users
[info] == Migrated … in 0.0s
验证用户路由是否存在:
mix phx.routes | grep users
输出:
GET /users DemoElixirPhoenixWeb.UserController :index
GET /users/:id/edit DemoElixirPhoenixWeb.UserController :edit
GET /users/new DemoElixirPhoenixWeb.UserController :new
GET /users/:id DemoElixirPhoenixWeb.UserController :show
POST /users DemoElixirPhoenixWeb.UserController :create
PATCH /users/:id DemoElixirPhoenixWeb.UserController :update
PUT /users/:id DemoElixirPhoenixWeb.UserController :update
DELETE /users/:id DemoElixirPhoenixWeb.UserController :delete
运行服务器:
mix phx.server
…
[info] Running DemoElixirPhoenixWeb.Endpoint with Bandit 1.5.5 at 127.0.0.1:4000 (http)
[info] Access DemoElixirPhoenixWeb.Endpoint at http://localhost:4000
[watch] build finished, watching for changes...
Rebuilding...
Done in 152ms.
浏览:
现在,您会看到一个用户登录页面,其中包含“列出用户”、“姓名”、“电子邮件”等文本。
服务器控制台的输出:
[info] GET /users
[debug] Processing with DemoElixirPhoenixWeb.UserController.index/2
Parameters: %{}
Pipelines: [:browser]
[debug] QUERY OK source="users" db=0.4ms queue=0.5ms idle=974.7ms
SELECT u0."id", u0."name", u0."email", u0."inserted_at", u0."updated_at" FROM "users" AS u0 []
↳ DemoElixirPhoenixWeb.UserController.index/2, at: lib/demo_elixir_phoenix_web/controllers/user_controller.ex:8
[info] Sent 200 in 48ms