Skip to content

Commit

Permalink
[server] Add appData virtualthread and jetty config
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Feb 21, 2024
1 parent f89bca0 commit 19e009c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
14 changes: 13 additions & 1 deletion docs/src/modules/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ types:
desc_ru: Останавливает работу сервера
- name: ContextValue
functions:
- name: appData
args: 'key'
desc: gets an appData value by key. See config.appData
desc_ru: получает значение appData по ключу `key`. См. config.appData
- name: attribute
args: 'key, value = ""'
desc: gets or sets an attribute by key
Expand Down Expand Up @@ -266,6 +270,9 @@ types:
"etags": true,
"maxRequestSize": 1_000_000,
"defaultHost": "localhost",
"defaultPort": 8000,
"caseInsensitiveRoutes": true,
"ignoreTrailingSlashes": true,
"multipleSlashesAsSingle": true,
Expand All @@ -274,5 +281,10 @@ types:
"basicAuth": ["user", "password"],
"dev": true,
"showBanner": false,
"sslRedirects": true
"sslRedirects": true,
"virtualThreads": true,
"appData": {
"key1": "value1",
"key2": "value2"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.annimon.ownlang.modules.server;

import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.*;
import io.javalin.config.JavalinConfig;
import io.javalin.config.Key;
import io.javalin.http.staticfiles.Location;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -21,6 +19,9 @@
* "etags": true,
* "maxRequestSize": 1_000_000,
*
* "defaultHost": "localhost",
* "defaultPort": 8000,
*
* "caseInsensitiveRoutes": true,
* "ignoreTrailingSlashes": true,
* "multipleSlashesAsSingle": true,
Expand All @@ -29,7 +30,12 @@
* "basicAuth": ["user", "password"],
* "dev": true,
* "showBanner": false,
* "sslRedirects": true
* "sslRedirects": true,
* "virtualThreads": true,
* "appData": {
* "key1": "value1",
* "key2": "value2"
* }
* }
*/

Expand Down Expand Up @@ -60,6 +66,10 @@ public void setup(JavalinConfig config) {
ifBoolean("etags", flag -> config.http.generateEtags = flag);
ifNumber("maxRequestSize", value -> config.http.maxRequestSize = value.asLong());

// jetty
ifString("defaultHost", value -> config.jetty.defaultHost = value);
ifNumber("defaultPort", value -> config.jetty.defaultPort = value.asInt());

// routing
ifBoolean("caseInsensitiveRoutes", flag -> config.router.caseInsensitiveRoutes = flag);
ifBoolean("ignoreTrailingSlashes", flag -> config.router.ignoreTrailingSlashes = flag);
Expand All @@ -71,6 +81,9 @@ public void setup(JavalinConfig config) {
ifBoolean("showBanner", flag -> config.showJavalinBanner = flag);
ifTrue("dev", config.bundledPlugins::enableDevLogging);
ifTrue("sslRedirects", config.bundledPlugins::enableSslRedirects);
ifBoolean("virtualThreads", flag -> config.useVirtualThreads = flag);
ifMap("appData", appData -> appData.getMapStringKeys()
.forEach((key, value) -> config.appData(new Key<>(key), value)));
}

private void ifTrue(String key, Runnable action) {
Expand Down Expand Up @@ -104,4 +117,12 @@ private void ifArray(String key, Consumer<ArrayValue> consumer) {
consumer.accept((ArrayValue) value);
}
}

private void ifMap(String key, Consumer<MapValue> consumer) {
if (!map.containsKey(key)) return;
final Value value = map.get(key);
if (value.type() == Types.MAP) {
consumer.accept((MapValue) value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.annimon.ownlang.modules.server;

import com.annimon.ownlang.lib.*;
import io.javalin.config.Key;
import io.javalin.http.Context;
import io.javalin.http.HttpStatus;
import org.jetbrains.annotations.NotNull;
Expand All @@ -12,12 +13,13 @@ class ContextValue extends MapValue {
private final Context ctx;

public ContextValue(@NotNull Context ctx) {
super(32);
super(40);
this.ctx = ctx;
init();
}

private void init() {
set("appData", this::appData);
set("attribute", this::attribute);
set("basicAuthCredentials", this::basicAuthCredentials);
set("body", Converters.voidToString(ctx::body));
Expand Down Expand Up @@ -59,6 +61,12 @@ private void init() {
set("userAgent", Converters.voidToString(ctx::userAgent));
}

private Value appData(Value[] args) {
Arguments.check(1, args.length);
String key = args[0].asString();
return ctx.appData(new Key<>(key));
}

private Value attribute(Value[] args) {
Arguments.checkOrOr(1, 2, args.length);
String key = args[0].asString();
Expand Down

0 comments on commit 19e009c

Please sign in to comment.