Skip to content

Instantly share code, notes, and snippets.

@ldoguin
Created August 2, 2016 12:37
Show Gist options
  • Save ldoguin/8f1043f23b33421bf3c8588fea5ddaa5 to your computer and use it in GitHub Desktop.
Save ldoguin/8f1043f23b33421bf3c8588fea5ddaa5 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- The parent to inherit the default dependencies and plugin configuration -->
<groupId>io.vertx.workshop</groupId>
<artifactId>vertx-microservice-workshop</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>quote-generator</artifactId>
<name>Quote Generator (Solution)</name>
<properties>
<!-- Main verticle -->
<main.verticle>io.vertx.workshop.quote.GeneratorConfigVerticle</main.verticle>
<!-- Docker discovery - we are exposing a HTTP endpoint -->
<discovery.service-type>http-endpoint</discovery.service-type>
</properties>
<dependencies>
<dependency>
<!-- This dependency provide some classes useful for the workshop -->
<groupId>io.vertx.workshop</groupId>
<artifactId>vertx-workshop-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.couchbase.client</groupId>
<artifactId>java-client</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- plugin to create a `fat-jar` -->
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<plugin>
<!-- plugin to create a docker image -->
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package io.vertx.workshop.quote;
import com.couchbase.client.java.AsyncBucket;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.query.AsyncN1qlQueryResult;
import com.couchbase.client.java.query.AsyncN1qlQueryRow;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.consistency.ScanConsistency;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServerResponse;
import rx.Observable;
/**
* This verticle exposes a HTTP endpoint to retrieve the current / last values of the maker data (quotes).
*
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
public class RestQuoteAPIVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
CouchbaseCluster cc = CouchbaseCluster.create();
AsyncBucket bucket = cc.openBucket().async();
vertx.eventBus().<io.vertx.core.json.JsonObject>consumer(GeneratorConfigVerticle.ADDRESS, message -> {
// Populate the `quotes` map with the received quote
// Quotes are json objects you can retrieve from the message body
// The map is structured as follows: name -> quote
// ----
JsonObject quote = JsonObject.from(message.body().getMap());
JsonDocument document = JsonDocument.create(quote.getString("name"), quote);
bucket.upsert(document).subscribe();
// ----
});
// Create a HTTP server that returns the quotes
// The request handler returns a specific quote if the `name` parameter is set, or the whole map if none.
// To write the response use: `request.response().end(content)`
// Responses are returned as JSON, so don't forget the "content-type": "application/json" header.
// If the symbol is set but not found, you should return 404.
// Once the request handler is set,
vertx.createHttpServer()
.requestHandler(request -> {
HttpServerResponse response = request.response()
.putHeader("content-type", "application/json");
// ----
String name = request.getParam("name");
if (name == null) {
N1qlQuery query = N1qlQuery.simple("SELECT default.* FROM `default`");
query.params().consistency(ScanConsistency.STATEMENT_PLUS);
Observable<AsyncN1qlQueryResult> resObs = bucket.query(query);
resObs.flatMap(AsyncN1qlQueryResult::rows).map(AsyncN1qlQueryRow::value)
.reduce(JsonArray.create(), JsonArray::add).single()
.subscribe(arr -> {
context.runOnContext(v -> {
response.end(arr.toString());
});
});
return;
}
bucket.get(name)
.map(JsonDocument::content)
.subscribe(quote -> {
context.runOnContext(v -> {
if (quote == null) {
response.setStatusCode(404).end();
} else {
response
.end(quote.toString());
}
});
});
// ----
})
.listen(8081, ar -> {
if (ar.succeeded()) {
System.out.println("Server started");
} else {
System.out.println("Cannot start the server: " + ar.cause());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment