Giter Club home page Giter Club logo

fresh's Introduction

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)

fresh's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.