wslでelixir その7

# 概要
wsl(wsl2じゃない)で、elixirやってみた。
練習問題やってみた。

# 練習問題
生httpリクエストを取得せよ。

# サンプルコード

```
defmodule NamaServer do
  @moduledoc """
  Documentation for `NamaServer`.
  """

  @doc """
  Hello world.

  ## Examples

      iex> NamaServer.hello()
      :world

  """
  def hello do
    :world
  end
	require Logger
	use GenServer
	@port 8080
	def start do
		GenServer.start(__MODULE__, %{socket: nil})
	end
	def init(state) do
		{:ok, socket} = :gen_tcp.listen(@port, [:binary, active: true])
		send(self(), :accept)
		Logger.info ">Listenn port: #{@port}"
		{:ok, %{state | socket: socket}}
	end
	def handle_info(:accept, %{socket: socket} = state) do
		{:ok, _} = :gen_tcp.accept(socket)
		Logger.info ">Connected"
		{:noreply, state}
	end
	def handle_info({:tcp, socket, data}, state) do
		Logger.info ">Received: #{String.length(data)}"
		Logger.info data
		Logger.info ">Send msg"
		res = "HTTP/1.1 200 OK" <> "\r\n" <>
			"Content-Length: 65" <> "\r\n\r\n" <>
			"<!doctype html><html><body><h1>Hello world</h1></body></html>" <> "\r\n\r\n"
		Logger.info res
		:ok = :gen_tcp.send(socket, res)
		#:gen_tcp.close(socket)
		{:noreply, state}
	end
	def handle_info({:tcp_closed, _}, state) do 
		Logger.info ">Close"
		{:stop, :normal, state}
	end
	def handle_info({:tcp_error, _}, state) do
		Logger.info ">Error"
		{:stop, :normal, state}
	end
end





```


# 実行結果

```
$ iex -S mix
Erlang/OTP 25 [erts-13.0.4] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit:ns]

Compiling 1 file (.ex)
Interactive Elixir (1.13.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> NamaServer.start

07:13:18.243 [info]  >Listenn port: 8080
{:ok, #PID<0.162.0>}
iex(2)>
07:13:31.573 [info]  >Connected

07:13:31.574 [info]  >Received: 700

07:13:31.575 [info]  GET / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
sec-ch-ua: "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Sec-Purpose: prefetch;prerender
Purpose: prefetch
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: ja,en-US;q=0.9,en;q=0.8



07:13:31.576 [info]  >Send msg

07:13:31.580 [info]  HTTP/1.1 200 OK
Content-Length: 65

<!doctype html><html><body><h1>Hello world</h1></body></html>



07:13:39.366 [info]  >Received: 577

07:13:39.367 [info]  GET /favicon.ico HTTP/1.1
Host: localhost:8080
Connection: keep-alive
sec-ch-ua: "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
sec-ch-ua-platform: "Windows"
Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: ja,en-US;q=0.9,en;q=0.8



07:13:39.369 [info]  >Send msg

07:13:39.369 [info]  HTTP/1.1 200 OK
Content-Length: 65

<!doctype html><html><body><h1>Hello world</h1></body></html>









```

以上。