Skip to content

Instantly share code, notes, and snippets.

@lasteris
Last active November 20, 2023 11:57
Show Gist options
  • Save lasteris/859c75bcd8640b15a27655cb0784c8b1 to your computer and use it in GitHub Desktop.
Save lasteris/859c75bcd8640b15a27655cb0784c8b1 to your computer and use it in GitHub Desktop.

Revisions

  1. lasteris revised this gist Nov 20, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.java
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ public void start(Promise<Void> startPromise) {

    WebClient client = WebClient.create(vertx, options);

    client.get(8881, "127.0.0.1", URLEncoder.encode("/привет", StandardCharsets.UTF_8))
    client.get(8881, "127.0.0.1", /*URLEncoder.encode("/привет", StandardCharsets.UTF_8)*/"/привет")
    .addQueryParam("hi", "Привет")
    .send()
    .onComplete(e -> Log.info(String.valueOf(e.succeeded())));
  2. lasteris revised this gist Nov 20, 2023. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions logback.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
    <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
    </appender>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>/tmp/vertx.log</file>
    <append>true</append>
    <encoder>
    <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
    </appender>
    <root level="debug">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
    </root>
    </configuration>
  3. lasteris created this gist Nov 20, 2023.
    71 changes: 71 additions & 0 deletions build.gradle.kts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
    import org.gradle.api.tasks.testing.logging.TestLogEvent.*

    plugins {
    java
    application
    id("com.github.johnrengelman.shadow") version "7.1.2"
    }

    group = "io.lasteris.dev"
    version = "1.0.0-SNAPSHOT"

    repositories {
    mavenCentral()
    }

    val vertxVersion = "4.5.0"
    val junitJupiterVersion = "5.9.1"
    val jacksonVersion = "2.15.3"

    val mainVerticleName = "io.lasteris.dev.dev_workaround_backend.MainVerticle"
    val launcherClassName = "io.vertx.core.Launcher"

    val watchForChange = "src/**/*"
    val doOnChange = "${projectDir}/gradlew classes"

    application {
    mainClass.set(launcherClassName)
    }

    dependencies {
    implementation(platform("io.vertx:vertx-stack-depchain:$vertxVersion"))
    implementation("io.vertx:vertx-web")
    implementation("io.vertx:vertx-pg-client")
    implementation("io.vertx:vertx-web-client")
    implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
    implementation("com.ongres.scram:client:2.1")
    implementation("ch.qos.logback:logback-core:1.4.11")
    implementation("org.slf4j:slf4j-api:2.0.9")
    implementation("ch.qos.logback:logback-classic:1.4.11")
    testImplementation("io.vertx:vertx-junit5")
    testImplementation("org.junit.jupiter:junit-jupiter:$junitJupiterVersion")
    testImplementation("org.testcontainers:testcontainers:1.19.1")
    testImplementation ("io.vertx:vertx-unit")
    }

    java {
    toolchain {
    vendor.set(JvmVendorSpec.AMAZON)
    languageVersion.set(JavaLanguageVersion.of(21))
    }
    }

    tasks.withType<ShadowJar> {
    archiveClassifier.set("fat")
    manifest {
    attributes(mapOf("Main-Verticle" to mainVerticleName))
    }
    mergeServiceFiles()
    }

    tasks.withType<Test> {
    useJUnitPlatform()
    testLogging {
    events = setOf(PASSED, SKIPPED, FAILED)
    }
    }

    tasks.withType<JavaExec> {
    args = listOf("run", mainVerticleName, "--redeploy=$watchForChange", "--launcher-class=$launcherClassName", "--on-redeploy=$doOnChange")
    }
    67 changes: 67 additions & 0 deletions main.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    package io.lasteris.dev.dev_workaround_backend;

    import io.netty.handler.logging.ByteBufFormat;
    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.Promise;
    import io.vertx.core.http.HttpMethod;
    import io.vertx.core.http.HttpServer;
    import io.vertx.ext.web.Router;
    import io.vertx.ext.web.client.WebClient;
    import io.vertx.ext.web.client.WebClientOptions;
    import io.vertx.ext.web.handler.LoggerHandler;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.net.URLEncoder;
    import java.nio.charset.StandardCharsets;

    public class MainVerticle extends AbstractVerticle {
    private static final Logger Log = LoggerFactory.getLogger(MainVerticle.class);

    @Override
    public void start(Promise<Void> startPromise) {

    HttpServer server = vertx.createHttpServer();

    Router router = Router.router(vertx);

    router.route("/*").handler(LoggerHandler.create());

    router.
    route("/привет").
    handler(ctx -> {
    Log.info(ctx.request().getParam("hi"));
    ctx.response().setStatusCode(202).end();
    });

    router.route(HttpMethod.GET,"/typo/").handler(ctx -> {
    Log.info(ctx.pathParam("hi"));

    WebClientOptions options = new WebClientOptions();

    options.setLogActivity(true)
    .setActivityLogDataFormat(ByteBufFormat.HEX_DUMP);

    WebClient client = WebClient.create(vertx, options);

    client.get(8881, "127.0.0.1", URLEncoder.encode("/привет", StandardCharsets.UTF_8))
    .addQueryParam("hi", "Привет")
    .send()
    .onComplete(e -> Log.info(String.valueOf(e.succeeded())));

    ctx.response().setStatusCode(201).end();
    });

    server.requestHandler(router).listen(8881, http -> {
    if (http.succeeded()) {
    startPromise.complete();
    Log.info("HTTP server started on port 8881");
    } else {
    startPromise.fail(http.cause());
    }
    });
    }

    }