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자리 16진수와 같은 강력한 비밀번호를 생성하세요.
printf " %sn " $( LC_ALL=C < /dev/urandom tr -dc ' 0-9a-f ' | head -c32 )
예제 출력:
a9ed78cd8e4aa2bd2a37ad7319899106
이 데모에 대해 로컬 PostgreSQL 설정을 더욱 유용하고 구체적으로 만들려면 새 역할을 생성하십시오.
역할 이름은 이 프로젝트 이름과 일치하므로 "demo_elixir_phoenix"입니다.
역할 비밀번호는 좋은 보안 관행이므로 강력한 비밀번호가 될 수 있습니다.
32자리 16진수와 같은 강력한 비밀번호를 생성하세요.
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
Elixir 패키지 관리자인 업데이트 mix
:
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 및 브런치를 업데이트합니다.
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 리소스에 대한 모든 코드를 생성합니다.
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