|
@@ -1,72 +1,117 @@
|
|
package hello;
|
|
package hello;
|
|
|
|
|
|
import com.blade.Blade;
|
|
import com.blade.Blade;
|
|
-import com.blade.server.netty.HttpConst;
|
|
|
|
|
|
+import com.blade.mvc.Const;
|
|
|
|
+import com.blade.mvc.RouteContext;
|
|
|
|
+import com.blade.mvc.http.StringBody;
|
|
|
|
+import hello.model.Fortune;
|
|
import hello.model.Message;
|
|
import hello.model.Message;
|
|
import hello.model.World;
|
|
import hello.model.World;
|
|
-import io.netty.buffer.ByteBuf;
|
|
|
|
-import io.netty.buffer.Unpooled;
|
|
|
|
-import io.netty.util.AsciiString;
|
|
|
|
-import io.netty.util.CharsetUtil;
|
|
|
|
|
|
|
|
-import java.util.Optional;
|
|
|
|
-import java.util.Random;
|
|
|
|
|
|
+import java.util.List;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
+
|
|
|
|
+import static io.github.biezhi.anima.Anima.select;
|
|
|
|
+import static io.github.biezhi.anima.Anima.update;
|
|
|
|
+import static java.util.Comparator.comparing;
|
|
|
|
+import static java.util.stream.Collectors.toList;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Blade Application
|
|
* Blade Application
|
|
*
|
|
*
|
|
* @author biezhi
|
|
* @author biezhi
|
|
- * @date 2017/9/22
|
|
|
|
|
|
+ * @date 2018/10/17
|
|
*/
|
|
*/
|
|
public class Application {
|
|
public class Application {
|
|
|
|
|
|
- private static final int DB_ROWS = 308;
|
|
|
|
- private static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(CharsetUtil.UTF_8);
|
|
|
|
- private static final ByteBuf PLAINTEXT_CONTENT_BUFFER = Unpooled.unreleasableBuffer(Unpooled.directBuffer().writeBytes(STATIC_PLAINTEXT));
|
|
|
|
- private static final CharSequence PLAINTEXT_CLHEADER_VALUE = AsciiString.cached(String.valueOf(STATIC_PLAINTEXT.length));
|
|
|
|
|
|
+ private static final StringBody PLAINTEXT = StringBody.of("Hello, World!");
|
|
|
|
+ private static final String JSON_CONTENT_TYPE = "application/json";
|
|
|
|
+ private static final String SERVER_HEADER = "Server";
|
|
|
|
+ private static final String SERVER_VALUE = "Blade-" + Const.VERSION;
|
|
|
|
+ private static final String ADDITIONAL_FORTUNE = "Additional fortune added at request time.";
|
|
|
|
|
|
- private static int getQueries(Optional<String> queryCount) {
|
|
|
|
- if (!queryCount.isPresent()) {
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
- int count;
|
|
|
|
|
|
+ private static final int DB_ROWS = 10000;
|
|
|
|
+
|
|
|
|
+ private static int getQueries(String queries) {
|
|
try {
|
|
try {
|
|
- count = Integer.parseInt(queryCount.get());
|
|
|
|
- } catch (NumberFormatException ignored) {
|
|
|
|
|
|
+ int count = Integer.valueOf(queries);
|
|
|
|
+ return Math.min(500, Math.max(1, count));
|
|
|
|
+ } catch (Exception e) {
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
- count = count < 1 ? 1 : count;
|
|
|
|
- count = count > 500 ? 500 : count;
|
|
|
|
- return count;
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static Integer generateId() {
|
|
|
|
+ return 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static List<Integer> generateIdList(int size) {
|
|
|
|
+ return Stream.iterate(0, num -> num + 1).limit(size)
|
|
|
|
+ .map(i -> generateId())
|
|
|
|
+ .collect(toList());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void db(RouteContext ctx) {
|
|
|
|
+ World world = select().from(World.class).byId(generateId());
|
|
|
|
+ ctx.json(world).contentType(JSON_CONTENT_TYPE).header(SERVER_HEADER, SERVER_VALUE);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void queries(RouteContext ctx) {
|
|
|
|
+ int queries = getQueries(ctx.fromString("queries", "1"));
|
|
|
|
+
|
|
|
|
+ List<Integer> idList = generateIdList(queries);
|
|
|
|
+
|
|
|
|
+ List<World> worlds = idList.stream()
|
|
|
|
+ .map(id -> select().from(World.class).byId(id))
|
|
|
|
+ .collect(toList());
|
|
|
|
+ ctx.json(worlds).contentType(JSON_CONTENT_TYPE).header(SERVER_HEADER, SERVER_VALUE);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void updates(RouteContext ctx) {
|
|
|
|
+ int queries = getQueries(ctx.fromString("queries", "1"));
|
|
|
|
+
|
|
|
|
+ List<Integer> idList = generateIdList(queries);
|
|
|
|
+
|
|
|
|
+ List<World> worlds = idList.stream()
|
|
|
|
+ .map(id -> select().from(World.class).byId(id))
|
|
|
|
+ .peek(Application::updateWorld).collect(toList());
|
|
|
|
+
|
|
|
|
+ ctx.json(worlds).contentType(JSON_CONTENT_TYPE).header(SERVER_HEADER, SERVER_VALUE);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void updateWorld(World world) {
|
|
|
|
+ int number = generateId();
|
|
|
|
+
|
|
|
|
+ update().from(World.class)
|
|
|
|
+ .set("randomNumber", number)
|
|
|
|
+ .where("id", world.getId())
|
|
|
|
+ .execute();
|
|
|
|
+
|
|
|
|
+ world.setRandomNumber(number);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void fortunes(RouteContext ctx) {
|
|
|
|
+ List<Fortune> fortunes = select().from(Fortune.class).all();
|
|
|
|
+
|
|
|
|
+ fortunes.add(new Fortune(0, ADDITIONAL_FORTUNE));
|
|
|
|
+ fortunes.sort(comparing(Fortune::getMessage));
|
|
|
|
+
|
|
|
|
+ ctx.attribute("fortunes", fortunes);
|
|
|
|
+ ctx.header(SERVER_HEADER, SERVER_VALUE);
|
|
|
|
+ ctx.render("fortunes.html");
|
|
}
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
- Blade.me()
|
|
|
|
- .get("/json", (request, response) -> {
|
|
|
|
- response.contentType(HttpConst.getContentType("application/json"));
|
|
|
|
- response.json(new Message());
|
|
|
|
- })
|
|
|
|
- .get("/db", (request, response) -> {
|
|
|
|
- final Random random = ThreadLocalRandom.current();
|
|
|
|
- response.contentType(HttpConst.getContentType("application/json"));
|
|
|
|
- response.json(new World().find(random.nextInt(DB_ROWS) + 1));
|
|
|
|
- })
|
|
|
|
- .get("/queries", (request, response) -> {
|
|
|
|
- int queries = getQueries(request.query("queries"));
|
|
|
|
- final World[] worlds = new World[queries];
|
|
|
|
- final Random random = ThreadLocalRandom.current();
|
|
|
|
- for (int i = 0; i < queries; i++) {
|
|
|
|
- worlds[i] = new World().find(random.nextInt(DB_ROWS) + 1);
|
|
|
|
- }
|
|
|
|
- response.contentType(HttpConst.getContentType("application/json"));
|
|
|
|
- response.json(worlds);
|
|
|
|
- })
|
|
|
|
- .get("/plaintext", (request, response) -> {
|
|
|
|
- response.contentType(HttpConst.getContentType("text/plain"));
|
|
|
|
- response.header(HttpConst.CONTENT_LENGTH, PLAINTEXT_CLHEADER_VALUE);
|
|
|
|
- response.body(PLAINTEXT_CONTENT_BUFFER.duplicate());
|
|
|
|
- })
|
|
|
|
|
|
+ Blade.of()
|
|
|
|
+ .get("/json", ctx -> ctx.json(new Message()).contentType(JSON_CONTENT_TYPE)
|
|
|
|
+ .header(SERVER_HEADER, SERVER_VALUE))
|
|
|
|
+ .get("/plaintext", ctx -> ctx.body(PLAINTEXT).contentType("text/plain")
|
|
|
|
+ .header(SERVER_HEADER, SERVER_VALUE))
|
|
|
|
+ .get("/db", Application::db)
|
|
|
|
+ .get("/queries", Application::queries)
|
|
|
|
+ .get("/updates", Application::updates)
|
|
|
|
+ .get("/fortunes", Application::fortunes)
|
|
.disableSession()
|
|
.disableSession()
|
|
.start(Application.class, args);
|
|
.start(Application.class, args);
|
|
}
|
|
}
|