1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

概要

wsl(wsl2じゃない)でnervesやってみる。
qemu(x86_64エミュレータ、ラズパイじゃない)でやってみた。
生成したnerves_livebook.imgを、quemで起動してテストしてみた。
練習問題、やってみた。

練習問題

cowboyを使え。

写真

image.png

セットアップ

Mix.install([
  {:cowboy, "~> 2.0"},
 	{:req, "~> 0.4.5"}
])

サンプルコード

defmodule Hello do
	defmodule Handler do
		def init(req, _opts) do
			resp = :cowboy_req.reply(_status = 200, _headers = %{"content-type" => "text/html; charset=utf-8"}, _body = "<!doctype html><h1>Hello, Cowboy!</h1>", _request = req)
			{:ok, resp, []}
		end
	end
	def start do
		routes = :cowboy_router.compile([{:_,  [{:_, Handler, []} ]}])
		require Logger
		Logger.info("Staring server at http://localhost:4001/")
		:cowboy.start_clear(:hello_http, [port: 4001], %{env: %{dispatch: routes}})
	end
end
Hello.start()

Req.get!("http://localhost:4001/")

実行結果

%Req.Response{
  status: 200,
  headers: %{
    "content-type" => ["text/html; charset=utf-8"],
    "date" => ["Tue, 21 Nov 2023 23:31:42 GMT"],
    "server" => ["Cowboy"]
  },
  body: "<!doctype html><h1>Hello, Cowboy!</h1>",
  trailers: %{},
  private: %{}
}

以上。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?