# pheonix 學習筆記 ## my systeminfo Ubuntu 20.04 ## 如何安裝 ``` sudo apt-get install elixir mix local.hex mix archive.install hex phx_new ``` 遇到以下錯誤: ** (Mix) You're trying to run :phx_new on Elixir v1.9.1 but it has declared in its mix.exs file it supports only Elixir ~> 1.12 移除原本的 elixir: ``` sudo apt-get remove elixir sudo apt-get autoremove ``` 使用新版的 elixir 安裝 pheonix: 1. 先安裝 erlang https://www.erlang-solutions.com/downloads/ 2. Compiling from source (Unix and MinGW) https://elixir-lang.org/install.html#gnulinux click `.tar.gz` download `elixir-1.14.1.tar.gz` ``` tar zxvf elixir-1.14.1.tar.gz cd elixir-1.14.1 make sudo make install ``` 3. `mix local.hex` 遇到以下錯誤: ```! 15:25:56.258 [error] Unable to load crypto library. Failed with error: ":load_failed, Failed to load NIF library /usr/lib/erlang/lib/crypto-5.1.1/priv/lib/crypto: 'libcrypto.so.1.0.0: cannot open shared object file: No such file or directory'" OpenSSL might not be installed on this system. ``` https://unix.stackexchange.com/questions/283607/libraries-libcrypto-so-1-0-0-cannot-open-shared-object-file-no-such-file-or-d 根據上述網址,需要執行以下指令: ``` sudo apt-get install libssl1.0.0 ``` 但卻出現以下錯誤: ``` 無法取得套件 libssl1.0-dev,但它卻被其它的套件引用了。 這意味著這個套件可能已經消失了、被廢棄了,或是只能由其他的來源取得 ``` https://unix.stackexchange.com/questions/283607/libraries-libcrypto-so-1-0-0-cannot-open-shared-object-file-no-such-file-or-d 根據以上網址,須執行以下步驟: ```! 1. sudo nano /etc/apt/sources.list 2. add deb http://security.ubuntu.com/ubuntu bionic-security main 3. sudo apt update && apt-cache policy libssl1.0-dev 4. sudo apt-get install libssl1.0-dev ``` 然後就能成功執行 `mix local.hex` 了。 4. `mix archive.install hex phx_new` 可以直接成功執行。 ## 另一種安裝方法 (by sam) ubuntu: ``` sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev 安裝 https://asdf-vm.com/ asdf plugin add erlang asdf plugin add elixir asdf install erlang 24.3.4.5 asdf install elixir 1.13.4-otp-24 asdf global erlang 24.3.4.5 asdf global elixir 1.13.4-otp-24 ``` macOS: ``` brew install asdf asdf plugin add erlang asdf plugin add elixir asdf install erlang 24.3.4.5 asdf install elixir 1.13.4-otp-24 asdf global erlang 24.3.4.5 asdf global elixir 1.13.4-otp-24 ``` 然後要把 `. /opt/homebrew/opt/asdf/libexec/asdf.sh` 放在 `.zshrc` ## 啟動 phoenix 專案 https://hexdocs.pm/phoenix/up_and_running.html https://hexdocs.pm/phoenix/1.3.0-rc.1/using_mysql.html 啟動 hello 專案(創建一個 hello 資料夾,初始化專案資料,並使用 mysql 作為資料庫)。 ``` mix phx.new hello --database mysql ``` ### 什麼是 mix https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html > Mix is a build tool that ships with Elixir that provides tasks for creating, compiling, testing your application, managing its dependencies and much more; --- https://ui-code.com/archives/293 https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04 ``` sudo apt update && sudo apt install mysql-server sudo systemctl start mysql.service ``` 接著要安裝 mysql 並且啟動服務,調整 `config/dev.exs` 的帳號密碼設定之後,執行以下指令: ``` mix ecto.create ``` 完成後可以看到 mysql 裡面有 database `hello_dev`。 接著啟動 server: ``` mix phx.server ``` 然後可以從 http://localhost:4000 看到 quickStart 頁面。 ## phoenix 專案資料結構 ``` ├── _build ├── assets ├── config ├── deps ├── lib │ ├── hello │ ├── hello.ex │ ├── hello_web │ └── hello_web.ex ├── priv └── test ``` * _build 處理所有專案的 compilation 部份,不能放在版本控制。 * assets 處理所有前端組件部份,包括 css, js 部份。 * config 放置專案的 config 設定。 * deps 放置所有依賴的 library code,不能放在版本控制,另外可在 `mix.exs` 看到所有依賴的 library 名稱。 * lib 放置後端邏輯的主要 code,主要分成兩個子目錄,一個是 `lib/hello`,另外一個是 `lib/hello_web`。 ... * test 儲存所有程式邏輯的測試程式碼。 * priv 儲存所有 production 需要的但不包含在 source code 的文件。 ## lib/hello 和 lib/hello_web 差別 以自己的理解來說,在 tl-websocket 的專案裡,`lib/hello` 就像是 apiv3 的 `app/core/services`,需要跟 db 和其他部件去做對接,`lib/hello_web` 則是單純的 api_function 定義,或是前端的部件,就像是 apiv3 的 `app/v3`。