https://github.com/devinus/fresh
The freshest Erlang web framework
https://github.com/devinus/fresh
Last synced: about 1 year ago
JSON representation
The freshest Erlang web framework
- Host: GitHub
- URL: https://github.com/devinus/fresh
- Owner: devinus
- License: unlicense
- Created: 2010-11-05T22:45:34.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2011-02-17T18:45:02.000Z (over 15 years ago)
- Last Synced: 2025-03-18T03:45:55.059Z (about 1 year ago)
- Language: Erlang
- Homepage: https://github.com/devinus/fresh
- Size: 710 KB
- Stars: 33
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-sinatra - Fresh
README
Fresh - The freshest Erlang web framework
=========================================
## What's so fresh about it?
Fresh is a Sinatra inspired Erlang web framework
based on Mochiweb that supports registering multiple
"web handlers" that are dispatched to when you access
a Fresh instance. This allows you to theoretically
serve sections of your website from multiple physically
separate computers. For instance, your main pages could
serve from a server in Hong Kong while your user pages
are served from Marfa, TX.
## What's it look like?
### example_web_handler.erl
-module(example_web_handler).
-behaviour(fresh_handler).
-export([dispatch_rules/0, 'GET'/2]).
dispatch_rules() ->
fun('GET', ["fresh"], _Req) -> true;
('POST', ["fresh"], _Req) -> true;
(_, _, _) -> false end.
'GET'(["fresh"], _Req) ->
"So fresh!".
'POST'(["fresh"], Req) ->
Body = Req:recv_body(),
{json, [{ok, Body}]}.
### example.erl
-module(example).
-behaviour(application).
-behaviour(supervisor).
-export([start/0, stop/0]).
-export([init/1, start/2, stop/1]).
start() -> application:start(?MODULE).
stop() -> application:stop(?MODULE).
start(_Type, _Args) ->
supervisor:start_link({local, example_sup}, ?MODULE, []).
stop(_State) -> ok.
init([]) ->
WebHandler = {example_web_handler,
{fresh_handler, start_link, [example_web_handler]},
permanent, 5000, worker, [fresh_handler]},
{ok, {{one_for_one, 10, 10}, [WebHandler]}}.
### example.app
{application, example, [
{description, "A Fresh example app"},
{vsn, "0.1"},
{applications, [kernel, stdlib, sasl]},
{modules, [example, example_web_handler]},
{registered, [example]},
{mod, {example, []}}
]}.
## Options
- `port`: port number, default: 7235
## TODO
- Allow custom error handlers (easy)
- Register new dispatch rules during hot OTP upgrade (harder)
- Statically analyze handler AST during registration to
create preliminary dispatch rules (hardest)