Lightweight framework for creating small standalone Java applications in a micro service way.
轻量级高性能Java微型服务框架,本项目是 Netty+Java8 的 Sinatra 实现。
沙丁鱼,世界重要海洋经济鱼类。硬骨鱼纲鲱形目鲱科,形态延长,侧扁,腹部具棱鳞。小者二寸,大者尺许,密集群息,沿岸洄游。
- Java8: functional, Lambda, Stream
- Netty: 事件驱动, 异步高性能, 高可靠性
- Tiny: 全部源码只有100k+,仅依赖Netty
one line code, one http server。
public static void main(String[] args) {
get("/", () -> "hello sardine");
}
DONE! Now, Run and View:
maven
<dependency>
<groupId>info.ibruce</groupId>
<artifactId>sardine</artifactId>
<version>1.0.0</version>
</dependency>
gradle
compile group: 'info.ibruce', name: 'sardine', version: '1.0.0'
versions
- sardine-1.0.0:Java8 + Netty5(已发版:2015.06)
- sardine-2.0.0:Java8 + Netty4(开发中:2016.08)
import static sardine.Sardine.*;
public class SardineServerTest {
public static void main(String[] args) {
port(9527);
get("/", () -> "hello sardine.");
}
}
Route 由三部分组成:
- 动作:http 动作的一种,如:
GET
,POST
,HEAD
,OPTIONS
,PUT
,DELETE
,TRACE
,CONNECT
,PATCH
- 路径:http 请求路径,如:
/home
,/books/:id
,/books/:author
- 函数:消费http request 产生 http response,如:
() -> {}
,request, response) -> {}
根据restful规范,建议开发者将
GET
,PUT
,DELETE
,HEAD
设计为幂等接口。
此外,还可以使用:
- 条件函数:条件函数,如:
(request) -> {}
,request -> "127.0.0.1".equals(request.ip())
- 接收类型:即http 头中的 accept,如:
Accept: text/html,*/*
,Accept: application/json
示例:
get("/books/:author", (request, response) -> {
return "query";
});
post("/book", (request, response) -> {
return "created";
});
put("/books/:id", (request, response) -> {
return "updated";
});
delete("/books/:id", (request, response) -> {
return "deleted";
});
// TODO restful verb 介绍
条件函数
get("/hello", request -> "127.0.0.1".equals(request.ip()), (request, response) -> {
return "hello sardine.";
});
参数分为三种类型:
- Named parameters:
- Wildcards parameters:
- Query parameters:
request.host();
request.port();
request.method();
request.path();
request.scheme();
request.contentType();
request.accept();
request.ajax();
request.headers();
request.headers("foo");
request.cookies();
request.cookies("name");
request.params();
request.params("foo");
request.paramsOptional("foo")
request.paramsOrElse("foo")
request.splats()
request.splatsAsList()
request.splatsFirst()
request.splatsLast()
request.queryParams();
request.queryParams("foo");
request.multiQueryParams("foo");
request.body();
request.bodyAsByte();
request.bodyLength();
response.status(404);
response.header("foo", "bar");
response.cookie("foo", "bar");
response.contentType("application/json");
response.body();
response.body("are you ok ?");
response.redirect("/login");
response.file("/hello.html");
中断
halt();
halt(403);
halt("a u ok?");
halt(503, "i got u!");
staticFileLocation("/resources");
externalStaticFileLocation("/var/www/resources");
json
最简版:
import static sardine.Sardine.*;
public class HelloSardine {
public static void main(String[] args) {
get("/", () -> "hello sardine.");
}
}
Java8 语法糖版:
import static sardine.Sardine.*;
public class HelloSardine {
public static void main(String[] args) {
get("/", new HelloSardine()::hello);
}
public String hello() { return "hello sardine"; }
}
import static sardine.Sardine.*;
public class HelloSardine {
public static void main(String[] args) {
get("/", HelloSardine::hello);
}
public static String hello() { return "hello sardine"; }
}
不蒜子:http://busuanzi.ibruce.info
ab