動作環境
$ cat /etc/os-release | grep VERSION=
VERSION="20.04.4 LTS (Focal Fossa)"
$ elixir -v
Erlang/OTP 24 [erts-12.3.1] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit]
Elixir 1.13.0 (compiled with Erlang/OTP 24)
$ mix phx.new --version
Phoenix installer v1.6.7
プロジェクトの作成
今回はDBを使わないため--no--ecto
を指定します
$ mix phx.new neoex --no-ecto
* creating neoex/config/config.exs
省略
* creating neoex/priv/static/favicon.ico
Fetch and install dependencies? [Yn] Y
* running mix deps.get
* running mix deps.compile
We are almost there! The following steps are missing:
$ cd neoex
Start your Phoenix app with:
$ mix phx.server
You can also run your app inside IEx (Interactive Elixir) as:
$ iex -S mix phx.server
Phoenixの起動
$ cd neoex
$ phx.server
Compiling 13 files (.ex)
Generated neoex app
[info] Running NeoexWeb.Endpoint with cowboy 2.9.0 at 127.0.0.1:4000 (http)
[debug] Downloading esbuild from https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-0.14.29.tgz
[info] Access NeoexWeb.Endpoint at http://localhost:4000
[watch] build finished, watching for changes...
ブラウザで起動確認
APIを作ろう
lib/neoex_web/router.ex
を編集
/exec/
値のパス追加します
defmodule NeoexWeb.Router do
use NeoexWeb, :router
#省略
scope "/", NeoexWeb do
pipe_through :browser
+ get "/exec/:val", PageController, :exec
get "/", PageController, :index
end
#省略
end
lib/neoex_web/views/page_view.ex
を編集
プレーンテキストを表示できるようにrender
関数を追加します
defmodule NeoexWeb.PageView do
use NeoexWeb, :view
+ def render("exec.txt", %{val: val}) do
+ val
+ end
end
lib/neoex_web/controllers/page_controller.ex
を編集
exec関数を追加します
router.exで指定した:exec
と同じ名前にします
defmodule NeoexWeb.PageController do
use NeoexWeb, :controller
def index(conn, _params) do
render(conn, "index.html")
end
+ def exec(conn, params) do
+ val = """
+ あなたの名前は#{params["val"]}です
+ """
+ render(conn, "exec.txt", val: val)
+ end
end
ブラウザで動作確認
http://localhost:4000/exec/hoge
NeosVR側
NeosVRで下記のLogiXを作ってPulseを押します
試しにhogeをabcに書き換えます
http://localhost:4000/exec/abc
表示結果が変化することを確認できました