Skip to content

pyloque/miniso

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

#Miniso Cheap But Works!

Maybe the Simplest Web Framework Of Java Universe !

Hello World

import miniso.JettyConfig;
import miniso.JettyServer;

public class Application {

	public static void main(String[] args) {
		JettyConfig config = new JettyConfig();
		config.setPort(8080);
		JettyServer server = new JettyServer(config);
		server.route(router -> {
			router.get("/hello", (req, res) -> {
				res.html("hello, world!");
			});
		});
		server.start();
	}

}

Dependencies

  1. jetty
  2. freemarker
  3. fastjson

Config

JettyConfig config = new JettyConfig();
config.setHost("localhost").setPort(8080)
	  .setAcceptors(1)  // accept connection threads
	  .setSelectors(2)  // read write connection threads
	  .setWorkers(10)  // handle http request threads
	  .setStaticPrefix("/static")  // static resource url prefix
	  .setClasspathStaticDir("/static") // static resource directory in classpath
	  .setExternalStaticDir("/whatever") // static resource directory in file system
	  .setTemplateDir("/pages");  // freemarker template directory in classpath

Route

JettyServer server = new JettyServer();
server.route(router -> {
	router.get("/hello", (req, res) -> {});
	router.post("/hello", (req, res) -> {});
	router.child("/math", math -> {  // sub modules
		math.get("/add", (req, res) -> {});
		math.get("/sub", (req, res) -> {});
		math.get("/mul", (req, res) -> {});
		math.get("/div", (req, res) -> {});
	});
	Company company = new Company();
	router.child("/company", company::route) // sub modules using class
});

class Company {
	public void hire(Request req, Response res) {}
	public void dismiss(Request req, Response res) {}

	public void route(JettyRouter router) {
		router.get("/hire", this::hire);
		router.get("/dismiss", this::dismiss);
	}
}

Error Handling

JettyServer server = new JettyServer();
server.route(router-> {
		  router.get("/google", (req, res) -> {
		      throw new Redirect("http://www.google.com");	
		  });
		  router.get("/facebook", (req, res) -> {
		      throw new NotFound("well, it's gone!");
          })
          router.get("/twitter", (req, res) -> {
		      throw new Forbidden("well, it's forbidden!");
		  });
      })
      .error(NotFound.class, (req, res, exc) -> {
	      res.template("404.html");
      })
      .error(Forbidden.class, (req, res, exc) -> {
          res.template("403.html");
	  })
	  .error(ParamError.class, (req, res, exc) -> {
		  res.template("400.html");
	  });

Output Style

JettyServer server = new JettyServer();
server.route(router -> {
    router.get("/hello/json", (req, res) -> {
        Map<String, Object> context = new HashMap<>();
        context.put("hello", "world");
        res.json(context);
    });
    router.get("/hello/tpl", (req, res) -> {
        Map<String, Object> context = new HashMap<>();
        context.put("hello", "world");
        res.template("hello.ftl", context);
    });
    router.get("/hello/text", (req, res) -> {
        res.html("hello, world!");
    });
});

Request Filter Ordering

JettyServer server = new JettyServer();
server.route(router -> {})
	  .before((req, res) -> {}) // global before filter
	  .after((req, res) -> {})  // global after filter
	  .before(path, (req, res) -> {})  // before filter of specified path
	  .after(path, (req, res) -> {}) // after filter of specified path

// if any filter return false, subsequently filters and route handle will not be executed
  1. global before filters
  2. before filters of specified path
  3. route handle of specified path
  4. global after filters
  5. after filters of specified path

Request Parameter Checking

JettyServer server = new JettyServer();
server.route(router -> {
	router.get("/hello", (req, res) -> {
		int a = req.int32("a");
		long b = req.int64("b", 1L);
		boolean c = req.bool("c");
		String d = req.string("d");
		List<Integer> e = req.int32s("e");
		int f = req.crack("f").checkInt32(value -> {
			return value >= 0;
		});
		List<String> g = req.crack("g").checkStrings(value -> {
			return value.length > 10;
		});
		// illegals will throw ParamError
	});
});

Notice

  1. https not supported yet!
  2. restful style url matching not supported!
  3. too simple, too naive!
  4. don't use me! just for inspiration!

About

mini java web framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages