Created
August 18, 2014 17:50
-
-
Save NiteshKant/08e71d0d027f8746cbad to your computer and use it in GitHub Desktop.
karyon-simple-router
This file contains hidden or 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 2013 Netflix, Inc. | |
* | |
* 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.netflix.karyon.examples.simpleroute.health; | |
import com.google.inject.Singleton; | |
import com.netflix.karyon.health.HealthCheckHandler; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.annotation.PostConstruct; | |
/** | |
* @author Nitesh Kant ([email protected]) | |
*/ | |
@Singleton | |
public class HealthCheck implements HealthCheckHandler { | |
private static final Logger logger = LoggerFactory.getLogger(HealthCheck.class); | |
@PostConstruct | |
public void init() { | |
logger.info("Health check initialized."); | |
} | |
@Override | |
public int getStatus() { | |
// TODO: Health check logic. | |
logger.info("Health check invoked."); | |
return 200; | |
} | |
} |
This file contains hidden or 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
package com.netflix.karyon.examples.simpleroute; | |
import com.google.inject.binder.AnnotatedBindingBuilder; | |
import com.netflix.governator.annotations.Modules; | |
import com.netflix.karyon.KaryonBootstrap; | |
import com.netflix.karyon.KaryonServer; | |
import com.netflix.karyon.archaius.ArchaiusBootstrap; | |
import com.netflix.karyon.examples.hellonoss.server.health.HealthCheck; | |
import com.netflix.karyon.transport.http.AbstractHttpModule; | |
import com.netflix.karyon.transport.http.HttpRequestRouter; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.handler.codec.http.HttpResponseStatus; | |
import io.reactivex.netty.protocol.http.server.HttpServerRequest; | |
import io.reactivex.netty.protocol.http.server.HttpServerResponse; | |
import rx.Observable; | |
/** | |
* @author Nitesh Kant | |
*/ | |
@ArchaiusBootstrap | |
@KaryonBootstrap(name = "simple-route", healthcheck = HealthCheck.class) | |
@Modules(include = {SimpleRoutingApp.SimpleRouterModule.class}) | |
public class SimpleRoutingApp { | |
public static class SimpleRouterModule extends AbstractHttpModule<ByteBuf, ByteBuf> { | |
public SimpleRouterModule() { | |
super(ByteBuf.class, ByteBuf.class); | |
} | |
@Override | |
public int serverPort() { | |
return 8888; | |
} | |
@Override | |
public int shutdownPort() { | |
return 9888; | |
} | |
@Override | |
protected void bindRequestRouter(AnnotatedBindingBuilder<HttpRequestRouter<ByteBuf, ByteBuf>> bind) { | |
bind.toInstance(new MyRequestRouter()); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
KaryonServer server = new KaryonServer(SimpleRoutingApp.class); | |
server.startAndAwait(); | |
} | |
private static class MyRequestRouter implements HttpRequestRouter<ByteBuf, ByteBuf> { | |
@Override | |
public Observable<Void> route(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) { | |
response.setStatus(HttpResponseStatus.NOT_FOUND); | |
return response.close(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment