# server.exs -rw-r--r-- 1023 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env elixir

defmodule Start do
  defp mime_types() do
    [
      {'html', 'text/html'},
      {'htm', 'text/html'},
      {'js', 'text/javascript'},
      {'css', 'text/css'},
      {'gif', 'image/gif'},
      {'jpg', 'image/jpeg'},
      {'jpeg', 'image/jpeg'},
      {'png', 'image/png'}
    ]
  end

  def start() do
    abs_path = :string.trim(:filename.absname('output_folder'), :trailing, '/.')
    :inets.start()
    address = {127, 0, 0, 1}
    ip_family_opts = []

    {:ok, pid} =
      :httpd.start_service(
        bind_address: address,
        document_root: abs_path,
        server_root: abs_path,
        directory_index: ['home'],
        server_name: 'localhost',
        port: 8008,
        default_type: 'text/html',
        mime_types: mime_types(),
        modules:
          [:mod_alias, :mod_dir, :mod_get] ++
            ip_family_opts
      )

    receive do
      {from, :shutdown} ->
        :ok = :erlang.stop_service(pid)
        send(from, :done)
    end
  end
end

Start.start()