DEV Community

gumi TECH for gumi TECH Blog

Posted on

3 2

Elixir入門: Plugを使うには

本稿は、「Elixir入門」シリーズの番外編として、Plugライブラリのインストールから立ち上げまでをご説明します。

Plugは、webライブラリやフレームワークを構築するための基盤となるプロジェクトです。Plugライブラリを使うと、開発者はwebサーバーで動く独自のPlugが定められます。ここでは、Plugをインストールして簡単なコードをローカルサーバーで開いてみます。

Plugにはつぎのふたつの役割があります。

  • webアプリケーション間で組み立てられるモジュールの仕様
  • Erlang VMにおける異なるwebサーバー用の接続アダプター

mixでElixirプロジェクトをつくる

まず、mixでElixirプロジェクトをつくります。コマンドラインツールでmix newコマンドに続けて、プロジェクト名のパスを打ち込んでください。



$ mix new my_plug


Enter fullscreen mode Exit fullscreen mode

プロジェクトのパス(my_plug)がつくられて、つぎのようなファイルをつくったことが示されるはずです(図001)。



* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/my_plug.ex
* creating test
* creating test/test_helper.exs
* creating test/my_plug_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:


Enter fullscreen mode Exit fullscreen mode

図001■mixでつくられたプロジェクトファイル

elixir_plug_001.png

Plugのインストール

アプリケーションのディレクトリ(my_plug)に切り替えたら、ファイルmix.exsapplicationdepsにつぎのように依存関係を加えます。:plug_cowboyは、webサーバーとしてCowboyをPlugとともに使う設定です。



defmodule MyPlug.MixProject do

  def application do
    [

      applications: [:plug_cowboy]
    ]
  end

  defp deps do
    [
      {:plug_cowboy, "~> 2.0"}
    ]
  end
end


Enter fullscreen mode Exit fullscreen mode
  • application: アプリケーションの依存関係を定めます。
  • deps: プロジェクトの依存関係を定めます。

そして、mix deps.getコマンドで依存関係を解決してください。



$ mix deps.get


Enter fullscreen mode Exit fullscreen mode


Resolving Hex dependencies...
Dependency resolution completed:
New:
  cowboy 2.6.1
  cowlib 2.7.0
  mime 1.3.1
  plug 1.7.1
  plug_cowboy 2.0.1
  plug_crypto 1.0.0
  ranch 1.7.1
* Getting plug_cowboy (Hex package)
* Getting cowboy (Hex package)
* Getting plug (Hex package)
* Getting mime (Hex package)
* Getting plug_crypto (Hex package)
* Getting cowlib (Hex package)
* Getting ranch (Hex package)


Enter fullscreen mode Exit fullscreen mode

サンプルコードを動かす

mixでつくられたプロジェクトファイルmy_plug.exのモジュールMyPlugに、つぎのテスト用の関数を定めます。



defmodule MyPlug do
  import Plug.Conn
  def init(options) do
    # optionsの初期化
    options
  end
  def call(conn, _opts) do
    conn
    |> put_resp_content_type("text/plain")
    |> send_resp(200, "Hello world")
  end
end


Enter fullscreen mode Exit fullscreen mode

そして、mixプロジェクトをつぎのようにiexのセッションで開きます。



$ iex -S mix


Enter fullscreen mode Exit fullscreen mode

Plugアプリケーションの中で前掲モジュールを動かすのがつぎのコードです。ターミナルからサーバーが起ち上がります。http://localhost:4000/のURLを開くと、ページに"Hello world"のテキストが示されるでしょう(図002)。これで、Plugアプリケーションが起動できました。



iex> {:ok, _} = Plug.Cowboy.http MyPlug, []
{:ok, #PID<0.208.0>}

Enter fullscreen mode Exit fullscreen mode




図002■サーバーから開いたページにテキストが示される

elixir_plug_002.png

Elixir入門もくじ

番外
  • Elixir入門: Plugを使うには

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay