## Elixir project file structure > project > deps > lib - project.ex > test - project_test.exs - test_helper.exs - mix.exs - mix.lock - README.md # after running 'iex -S mix', run this to start server {:ok, _} = Plug.Cowboy.http(Helloplug, [])
## mix.exs defmodule Helloplug.MixProject do use Mix.Project def project do [ app: :helloplug, version: "0.1.0", elixir: "~> 1.16", start_permanent: Mix.env() == :prod, deps: deps() ] end # Run "mix help compile.app" to learn about applications. def application do [applications: [ :logger, # :sqlite_ecto, # :ecto, # :cowboy, :plug, :plug_cowboy ] ] end # Run "mix help deps" to learn about dependencies. defp deps do [ # {:cowboy, "~> 1.0.0"}, {:plug, "~> 1.0"}, {:plug_cowboy, "~> 1.0"} # {:sqlite_ecto, ">= 1.0"}, # {:ecto_sqlite3, "~> 0.10"}, # {:ecto, "~> 1.0"}] ] end end
## test/project.exs defmodule HelloplugTest do use ExUnit.Case doctest Helloplug test "greets the world" do assert Helloplug.hello() == :world end end
## main.exs IO.puts("hello there!!!") IO.puts(Helloplug.hello()) # for i <- [1,2,3] do # IO.puts("hello #{i}") # end foo = 123 bar = 456 IO.puts(foo + bar)
## lib/project.ex defmodule Helloplug do @moduledoc """ Documentation for `Helloplug`. """ @doc """ Hello world. ## Examples iex> Helloplug.hello() :world """ def hello do :world end def init(default_ops) do IO.puts("starting up Helloplug...") end def call(conn, _opts) do IO.puts("saying hello!") route(conn.method, conn.path_info, conn) end def template_show_user(user_id) do EEx.eval_string("""""", [user_id: user_id]) end ########################## # routes def route("GET", ["hello"], conn) do conn |> Plug.Conn.put_resp_header("Server", "Plug") |> Plug.Conn.send_resp(200, "Hello, world!") end def route("GET", ["users", user_id], conn) do # route for /users/ User Information Page
Looks like you've requested information for the user with id <%= user_id %>.
Also, 1 + 1 = <%= 1 + 1 %>
<%= if user_id == "0" do %> <%# we should tell everybody that user 0 is really cool %> User zero is really cool! <% end %># conn |> Plug.Conn.send_resp(200, "You requested user #{user_id}") page_contents = template_show_user(user_id) conn |> Plug.Conn.put_resp_content_type("text/html") |> Plug.Conn.send_resp(200, page_contents) end def route(_method, _path, conn) do conn |> Plug.Conn.send_resp(404, "Couldn't find that page, sorry!") end end