avatar
分享一段代码, 38 行用 Java 实现 Koa 核心功能

admin 108 2019-03-10 15:20:38

                                           
                         package org.jianzhao.onion;

/**
 * Onion is just like an Onion
 *
 * @param <T> Context
 * @author cbdyzj
 * @since 2018.3.23
 */
public final class Onion<T> {

    private Middleware<T> middleware = (ctx, nxt) -> nxt.next();

    public Onion<T> use(Middleware<T> middleware) {
        this.middleware = this.middleware.compose(middleware);
        return this;
    }

    public void handle(T context) throws Exception {

        this.middleware.via(context,() -> { });
    }

    public interface Middleware<T> {

        void via(T context, Next next) throws Exception;

        default Middleware<T> compose(Middleware<T> middleware) {
            return (ctx, nxt) -> this.via(ctx, () -> middleware.via(ctx, nxt));
        }
    }

    public interface Next {

        void next() throws Exception;
    }

}
                      
                                       
To share this paste please copy this url and send to your friends
预览

评论

需要身份验证

您必须登录才能发表评论.

登录
    还没有评论.