|
@@ -4,17 +4,13 @@ import com.wizzardo.epoll.ByteBufferProvider;
|
|
|
import com.wizzardo.epoll.ByteBufferWrapper;
|
|
|
import com.wizzardo.http.HttpConnection;
|
|
|
import com.wizzardo.http.framework.Controller;
|
|
|
-import com.wizzardo.http.framework.template.Renderer;
|
|
|
import com.wizzardo.http.request.Header;
|
|
|
import com.wizzardo.http.response.Status;
|
|
|
import com.wizzardo.tools.json.JsonTools;
|
|
|
-import io.reactiverse.pgclient.PgIterator;
|
|
|
-import io.reactiverse.pgclient.PgPool;
|
|
|
-import io.reactiverse.pgclient.Tuple;
|
|
|
+import com.wizzardo.tools.misc.Unchecked;
|
|
|
+import io.reactiverse.pgclient.*;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
@@ -22,6 +18,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
public class DBController extends Controller {
|
|
|
|
|
|
DBService dbService;
|
|
|
+ CachedWorldService cachedWorldService;
|
|
|
|
|
|
public void world() {
|
|
|
response.async();
|
|
@@ -76,6 +73,18 @@ public class DBController extends Controller {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void cachedWorlds() {
|
|
|
+ int count = Math.min(Math.max(params().getInt("count", 1), 1), 500);
|
|
|
+ CachedWorld[] worlds = new CachedWorld[count];
|
|
|
+
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ worlds[i] = cachedWorldService.get(getRandomNumber());
|
|
|
+ }
|
|
|
+
|
|
|
+ response.appendHeader(Header.KV_CONTENT_TYPE_APPLICATION_JSON);
|
|
|
+ response.body(JsonTools.serializeToBytes(worlds));
|
|
|
+ }
|
|
|
+
|
|
|
public void updates() {
|
|
|
int queries = Math.min(Math.max(params().getInt("queries", 1), 1), 500);
|
|
|
World[] worlds = new World[queries];
|
|
@@ -122,6 +131,45 @@ public class DBController extends Controller {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static final class Fortune {
|
|
|
+ public final int id;
|
|
|
+ public final String message;
|
|
|
+
|
|
|
+ public Fortune(int id, String message) {
|
|
|
+ this.id = id;
|
|
|
+ this.message = Objects.requireNonNull(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void fortunes() {
|
|
|
+ response.async();
|
|
|
+ dbService.getClient().preparedQuery("SELECT * FROM fortune", res -> {
|
|
|
+ try {
|
|
|
+ if (res.succeeded()) {
|
|
|
+ PgRowSet result = res.result();
|
|
|
+ ArrayList<Fortune> fortunes = new ArrayList<>(result.size() + 1);
|
|
|
+ for (Row row : result) {
|
|
|
+ fortunes.add(new Fortune(row.getInteger(0), row.getString(1)));
|
|
|
+ }
|
|
|
+
|
|
|
+ fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
|
+ fortunes.sort(Comparator.comparing(fortune -> fortune.message));
|
|
|
+
|
|
|
+ model().append("fortunes", fortunes);
|
|
|
+ response.setBody(renderView("fortunes").renderReadableData());
|
|
|
+ response.appendHeader(Header.KV_CONTENT_TYPE_HTML_UTF8);
|
|
|
+ } else {
|
|
|
+ res.cause().printStackTrace();
|
|
|
+ response.status(Status._500).body(res.cause().getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ commitAsyncResponse();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
static ThreadLocal<ByteBufferProvider> byteBufferProviderThreadLocal = ThreadLocal.<ByteBufferProvider>withInitial(() -> {
|
|
|
ByteBufferWrapper wrapper = new ByteBufferWrapper(64 * 1024);
|
|
|
return () -> wrapper;
|
|
@@ -133,24 +181,11 @@ public class DBController extends Controller {
|
|
|
response.commit(connection, bufferProvider);
|
|
|
connection.flush(bufferProvider);
|
|
|
response.reset();
|
|
|
+ Unchecked.run(connection::onFinishingHandling);
|
|
|
}
|
|
|
|
|
|
protected int getRandomNumber() {
|
|
|
return 1 + ThreadLocalRandom.current().nextInt(10000);
|
|
|
}
|
|
|
|
|
|
- public static final class World implements Comparable<World> {
|
|
|
- public int id;
|
|
|
- public int randomNumber;
|
|
|
-
|
|
|
- public World(int id, int randomNumber) {
|
|
|
- this.id = id;
|
|
|
- this.randomNumber = randomNumber;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int compareTo(World o) {
|
|
|
- return Integer.compare(id, o.id);
|
|
|
- }
|
|
|
- }
|
|
|
}
|