can erlang cowboy be used as a directory index fileserver?
- User
can erlang cowboy be used as a directory index fileserver?
Assistant
A very specific and interesting question!
Yes, Cowboy, the Erlang HTTP server, can indeed be used as a directory index fileserver. While Cowboy is primarily designed as a web server for building RESTful APIs and web applications, its flexibility and modularity allow it to be configured for serving static files, including acting as a directory index server.
Here's how you can do it:
1. Basic Setup
First, ensure you have Cowboy installed in your Erlang project. If you're using Rebar3, add
cowboyto yourrebar.configdeps:{deps, [ {cowboy, {git, "https://github.com/ninenines/cowboy", {tag, "2.9.0"}}} ]}.Then, run
rebar3 compileto fetch and compile dependencies.2. Configure Cowboy to Serve Static Files
Create a simple Cowboy handler that serves files from a directory. You'll need to define a module that will handle HTTP requests. Let's name it
static_file_handler.erl.Here's an example implementation:
-module(static_file_handler). -behaviour(cowboy_handler). -export([init/2]). init(Req0, Opts) -> %% Get the requested path from the URL {Method, Req1} = cowboy_req:method(Req0), case Method of <<"GET">> -> {Path, Req2} = cowboy_req:path(Req1), serve_file(Path, Req2, Opts); _ -> %% Only GET is supported Req3 = cowboy_req:reply(405, Req1), {ok, Req3, Opts} end. serve_file(<<"/", _/binary>> = Path, Req, Opts) -> %% Directory where your static files are located DocRoot = proplists:get_value(doc_root, Opts), FullPath = filename:join(DocRoot, Path), case filelib:is_dir(FullPath) of true -> serve_directory_index(FullPath, Req, Opts); false -> case filelib:is_regular(FullPath) of true -> send_file(FullPath, Req, Opts); false -> Req2 = cowboy_req:reply(404, Req), {ok, Req2, Opts} end end. serve_directory_index(DirPath, Req, Opts) -> case file:list_dir(DirPath) of {ok, Files} -> Body = generate_directory_index(Files), Headers = #{<<"content-type">> => <<"text/html">>}, Req2 = cowboy_req:reply(200, Headers, Body, Req), {ok, Req2, Opts}; {error, _} -> Req2 = cowboy_req:reply(403, Req), {ok, Req2, Opts} end. generate_directory_index(Files) -> L = lists:map(fun(File) -> io_lib:format("<li><a href='~s'>~s</a></li>", [File, File]) end, Files), "<html><body><h1>Index</h1><ul>" ++ lists:flatten(L) ++ "</ul></body></html>". send_file(FilePath, Req, Opts) -> case cowboy_req:reply(200, #{ <<"content-type">> => guess_mime_type(FilePath) }, FilePath, Req) of {ok, Req2, _} -> {ok, Req2, Opts}; Error -> Error end. guess_mime_type(FilePath) -> case filename:extension(FilePath) of <<".html">> -> <<"text/html">>; <<".css">> -> <<"text/css">>; <<".js">> -> <<"application/javascript">>; <<".png">> -> <<"image/png">>; _ -> <<"application/octet-stream">> end.This handler (
static_file_handler) does the following:- Checks if the request method is
GET. - Determines if the requested path is a directory or a file.
- If it's a directory, it lists the files in the directory (akin to Apache's directory index feature) by generating a simple HTML page with links to the files/subdirectories.
- If it's a file, it serves the file with a guessed MIME type.
3. Start Cowboy with the Handler
Now, let's start Cowboy and tell it to use
static_file_handlerfor serving files from a specific directory. Create amain.erlfile:-module(main). -export([start/0]). start() -> %% Where your static files are located DocRoot = "/path/to/your/static/files", Dispatch = cowboy_router:compile([ {'_', [ {"/[...]", static_file_handler, [{doc_root, DocRoot}]} ]} ]), {ok, _} = cowboy:start_clear(my_http_listener, [{port, 8080}], #{env => #{dispatch => Dispatch}} ), io:format("Server running on http://localhost:8080~n"), receive _ -> ok end.Replace
"/path/to/your/static/files"with the actual path to the directory you want to serve.4. Run Your Server
- Compile everything:
rebar3 compile - Start your Erlang shell:
rebar3 shell - In the shell, start the server:
main:start().
Now, visit
http://localhost:8080/in your browser. You should see the directory index of theDocRootfolder you specified, and navigating through directories and accessing files should work as expected.Notes:
- This is a basic example. You might want to enhance security (e.g., prevent directory traversal attacks), add more MIME types, handle more HTTP methods, implement caching, etc.
- Cowboy's documentation is excellent; refer to it for deeper customization: ninenines.eu/docs/en/cowboy/2.9/guide/.
Enjoy serving your files with Erlang and Cowboy!
- Checks if the request method is
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.