Git Product home page Git Product logo

frameworkey-erlang's Introduction

Frameworkey-Erlang

A web framework that allows you Unix-style piping of controller actions. What you feel in your pants is a okay. That is what we call a nerd boner.

Why?

Why not?

No really, why?

Because when doing web-development with a lot of endpoints, there tends to be a ton of nesting-of-functions, and consequently, a ton of "copy-paste, and change one line" for different endpoints. What I want is to allow small, reusable, composable functions that can be arbitrarily glued together.

How does it work?

Routing

That's easy. In the routes.json:

{
  "METHOD /endpoint": "mycontroller.action mycontroller.action2"
}

mycontroller in this case is just a vanilla erlang module with the functions action/1 and action/2 exported. Here's what it can look like:

-module(mycontroller).
-export([action/1,action2/1]).

action(Params) ->
    Params.
action2(Blah) ->
    #{ json => Blah, header => #{code => 201, data =>[{<<"content-type">>,<<"application/json">>}]}}.


These functions can be chained ad-nauseum. If you wanted to have fifty actions handle an action, be my guest. Just make sure that the last action returns a map with the key json.

Permissions/ACL

The specifics of the API are liable to change, but the documentation is as follows.

Permissions are specified in the priv/policies.json file. Currently this is unchangable. The file should look like this:

{
  "mycontroller":{
    "action": ["auth", "admin"],
    "*": "auth"
  }
}

You can specify as many policies for each action as you'd like, and are run in the order in which they appear in the array (you can omit the array if you only want to run one policy). If any policies return false, none of the actions for the endpoint will be run, and a 403 will be sent to the client.

The "*" is the "fallback action". If a policy is not defined for the particular action that's about to be run, it "falls back" to the * as a sort of default permission.

Defining Policies

A policy may look something like this:

-module(auth).
-export([check/1]).

check(Params) ->
  Username = maps:get(<<"username">>, Params),
  Password= maps:get(<<"password">>, Params),
  Result = case db:lookupUser(Username, Password) of
    {error, nouser} -> false;
    {ok, User} -> true
  end,
  Result. 

You can probably make this look more elegant with cooler pattern matching and whatnot, but here's the criteria you need to know:

  • A policy needs to have a check/1 function exported.
  • The argument that will be fed into check/1 will be a map with all the params from the request.
  • You return true or false depending on whether or not you want to grant access.

What tech is powering this?

Besides my amazingly clever ingenuity, Frameworkey is powered by the amazingly wonderful Cowboy server. I use Sync so that I don't have to constantly reload stuff. It's not hard.

What version of Erlang does this support?

I'm not sure, but I make pretty liberal use of maps, so at least Erlang 17. I develop using Erlang 18.

frameworkey-erlang's People

Contributors

tombert avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.