Limmen / marley

very basic webserver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

marley

Build Status

Description

Very basic http-server in erlang.

Not recommended for any serious use, this was just developed as a exercise. I recommend to have a look at cowboy and elli

How to

Add marley to your project:

{deps, [
       {marley, {git, "https://github.com/Limmen/marley"}}
]}.

Start the server as follows:

    Routes = #{static => "priv", router => marley_example_router},
    Port = 3000,
    marley:start_http(Port, Routes).

Where static is the directory from where to server static files and router is the name of the module of your router, port is the portnumber that the server will listen on.

Create your router or place static files in your static folder

Your router should export functions on the following form:

    httpmethod(URI, Body, Headers) ->
                    {Code, ResponseBody, ResponseHeaders}.

URI, Body, headers, ResponseBody, ResponseHeaders should be in binary format and Code should be a integer (http status code).

Headers should be separated by "\r\n".

For instance:

get(<<"/">>,_,_)->
    {200, <<"Home page">>, <<"content-type: text/plain\r\n">>};
    
get(<<"/index">>,_,_)->
    {200, <<"Response to http get request for /index">>, <<"content-type: text/plain\r\n">>}.

post(<<"/resource">>,_,_)->
    Resource = <<"resource">>,
    {201, Resource, <<"content-type:text/plain\r\n">>}.

There is also a special function that your router can export to handle the case when the requested resource was not found:

not_found(Route,_,_)->
    {404, <<Route/bits," not found">>, <<"content-type:text/plain\r\n">>}.

Next open your browser at:

http://localhost:3000/

See examples in /examples

Usage

# build
$ ./rebar3 compile

# remove temporary files
$ ./rebar3 clean

# run tests
$ ./rebar3 alias testall

# validate codebase, runs: tests, linters, static code analysis
$ ./rebar3 alias validate

# Generate documentation with edoc
$ ./rebar3 edoc

# Start shell with application loaded
$ ./rebar3 shell

# Start shell with configuration
$ rebar3 shell --config config_marley.config

# Start shell with application loaded and listen for code changes
$ ./rebar3 auto

# Run release
$ ./rebar3 run

Copyright and license

see: LICENSE

Copyright (c) 2016 Kim Hammar

About

very basic webserver

License:Other


Languages

Language:Erlang 100.0%