Last active
November 22, 2019 23:18
-
-
Save ctoestreich/75aba446b1f3b22aa71c9010d5b0542f to your computer and use it in GitHub Desktop.
SchemaRegistryStubController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017-2019 original authors | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.company | |
import groovy.util.logging.Slf4j | |
import io.micronaut.http.HttpResponse | |
import io.micronaut.http.annotation.Body | |
import io.micronaut.http.annotation.Controller | |
import io.micronaut.http.annotation.Get | |
import io.micronaut.http.annotation.Post | |
import javax.validation.constraints.NotBlank | |
@Controller | |
@Slf4j | |
class SchemaRegistryStubController { | |
private Map<String, Schema> schemas = new HashMap<>() | |
/** | |
* Schemas will self register with controller | |
*/ | |
@Post(value = "/subjects/{subject}/versions", consumes = "*/*") | |
HttpResponse<String> registerSchema(@NotBlank String subject, @Body String body) { | |
log.info "~~~~~~ Schema Stub ~~~~~~~~~" | |
log.info "Registering schema for: $subject" | |
int id = schemas.size() + 1 | |
schemas.putIfAbsent(subject, new Schema(id, body)) | |
return HttpResponse.ok("{\"id\":" + schemas.get(subject).id + "}") | |
} | |
@Get(value = "/schemas/ids/{id}", consumes = "*/*") | |
HttpResponse<String> getSchema(Integer id) { | |
log.info "~~~~~~ Schema Stub ~~~~~~~~~" | |
log.info "Getting schema for: $id" | |
return HttpResponse.ok( | |
(schemas | |
.values() | |
.find { schema -> (schema?.id == id) })?.schema | |
?: "{\"schema\":\"\\\"\\\"\"}") | |
} | |
class Schema { | |
int id | |
String schema | |
Schema(final int id, final String schema) { | |
this.id = id | |
this.schema = schema | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment