Git Product home page Git Product logo

spark-decorator's Introduction

spark-decorator

Build Status Coverage Status

Simple and pretty decorators for your routes.

Want some routes to have auth, but some don't? Or base it off a parameter?

Spark's before and after work great if the distinction your are making is by path. But what if it's not? Maybe some users can GET but cannot POST on the same route.

This libs helps you to decorate your routes in order to add custom behaviour to a selection of them, without duplication and hassles.

You can hook before and after filters and do anything you want!

Install

Of course you must have Spark, we include it as provided (version 2.7.2).

Then, add to your pom.xml:

    <dependency>
        <groupId>xyz.luan.spark.decorator</groupId>
        <artifactId>spark-decorator</artifactId>
        <version>0.2.0</version>
    </dependency>

Or, if you are using gradle:

    compile 'xyz.luan.spark.decorator:spark-decorator:0.2.0'

Usage

You can do a plethora of things using this, basically anything you would do in a before or after filter.

For example, let's add a secret header to some of our routes; create a RouteDecorator instance with a singleton instance (this is to follow Spark's convention and is optional).

    public class AfterDecorator extends RouteDecorator {
        public static final AfterDecorator secretPanda = new AfterDecorator();

        private AfterDecorator() {
        }

        @Override
        protected Route after() {
            return (req, resp) -> {
                resp.header("secret", "panda");
                return null;
            };
        }
    }

Now, in your controller/application, instead of

    get("foo", (req, resp) -> "bar");

You do:

    secretPanda.get("/foo", (req, resp) -> "bar");

And your request has been enriched. You'd of course statically import get from Spark, and now also statically import secretPanda from your class.

Pretty neat, hum? Let's see a more complex example now!

Auth Example

Assume each route might or might not require auth, and the ones that require auth require a specific role to access. You could create a decorator like so:

public class AuthDecorator extends RouteDecorator {

    public static AuthDecorator requiresRole(String role) {
        return new AuthDecorator(role);
    }

    private String requiredRole;

    private AuthDecorator(String requiredRole) {
        this.requiredRole = requiredRole;
    }

    @Override
    protected Route before() {
        return (req, resp) -> {
            String auth = req.header("Auth");
            if (auth == null) {
                if (requiredRole == null) {
                    return null; // ok
                }
                throw halt(401, "You must be authenticated to access.");
            }
            String role = getProfile(auth).getRole();
            if (!role.equals(requiredRole)) {
                throw halt(403, "You must be role " + requiredRole + " to access.");
            }
            return null;
        };
    }
}

Then, you can use it like so:

    get("/foo", (req, resp) -> "bar");
    requiresRole("user").post("/foo", (req, resp) -> "ok");
    requiresRole("admin").delete("/foo", (req, resp) -> "deleted");

Contributing

Want to contribute? Please do so! Leave a star, open PR and issues! To submit a PR, it's really simple:

  • Fork it!
  • Create a feature branch.
  • Make your modifications
  • Open a Pull Request :)

spark-decorator's People

Contributors

luanpotter avatar

Stargazers

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