71 lines
1.8 KiB
Java
71 lines
1.8 KiB
Java
package com.drinkool.controller;
|
|
|
|
import com.drinkool.InternalValue;
|
|
import com.drinkool.lib.dtos.GraphqlDto;
|
|
|
|
import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient;
|
|
import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClientBuilder;
|
|
import jakarta.inject.Inject;
|
|
import jakarta.json.bind.Jsonb;
|
|
import jakarta.ws.rs.POST;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.core.Context;
|
|
import jakarta.ws.rs.core.HttpHeaders;
|
|
import jakarta.ws.rs.core.MediaType;
|
|
import jakarta.ws.rs.core.Response;
|
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
|
|
|
@Path("/api/eatery")
|
|
public class EateryController {
|
|
|
|
@ConfigProperty(name = "quarkus.smallrye-graphql-client.eatery.url")
|
|
String eateryUrl;
|
|
|
|
@Inject
|
|
Jsonb jsonb;
|
|
|
|
@POST
|
|
@Path("/graphql")
|
|
public Response forward(
|
|
GraphqlDto gqlRequest,
|
|
@Context HttpHeaders httpHeaders
|
|
) {
|
|
DynamicGraphQLClientBuilder builder =
|
|
DynamicGraphQLClientBuilder.newBuilder().url(eateryUrl);
|
|
|
|
httpHeaders
|
|
.getRequestHeaders()
|
|
.forEach((name, values) -> {
|
|
String headerName = name.toLowerCase();
|
|
|
|
if (headerName.startsWith(InternalValue.headerId.toLowerCase())) {
|
|
if (!values.isEmpty()) {
|
|
builder.header(name, values.get(0));
|
|
}
|
|
}
|
|
});
|
|
|
|
try (DynamicGraphQLClient tempClient = builder.build()) {
|
|
io.smallrye.graphql.client.Response response = tempClient.executeSync(
|
|
gqlRequest.query,
|
|
gqlRequest.variables
|
|
);
|
|
|
|
if (response.hasError()) {
|
|
return Response.status(400)
|
|
.entity(jsonb.toJson(response.getErrors()))
|
|
.build();
|
|
}
|
|
|
|
return Response.ok(jsonb.toJson(response.getData()))
|
|
.type(MediaType.APPLICATION_JSON)
|
|
.build();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return Response.serverError()
|
|
.entity("Lỗi kết nối tới service Eatery")
|
|
.build();
|
|
}
|
|
}
|
|
}
|