Skip to content

Commit

Permalink
First cut.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkokar committed Nov 25, 2019
1 parent 6eebf58 commit a53e976
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,7 @@ public final class HttpHeaderNames {
public static final CharSequence TRAILER = newEntity(Names.TRAILER);
public static final CharSequence UPGRADE = newEntity(Names.UPGRADE);
public static final CharSequence VIA = newEntity(Names.VIA);
public static final CharSequence CACHE_CONTROL = newEntity(Names.CACHE_CONTROL);

private HttpHeaderNames() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.hotels.styx.admin.handlers.StartupConfigHandler;
import com.hotels.styx.admin.handlers.StyxConfigurationHandler;
import com.hotels.styx.admin.handlers.ThreadsHandler;
import com.hotels.styx.admin.handlers.UptimeHandler;
import com.hotels.styx.admin.handlers.VersionTextHandler;
import com.hotels.styx.admin.tasks.OriginsCommandHandler;
import com.hotels.styx.admin.tasks.OriginsReloadCommandHandler;
Expand Down Expand Up @@ -131,6 +132,7 @@ private HttpHandler adminEndpoints(StyxConfig styxConfig, StartupConfig startupC
httpRouter.aggregate("/", new IndexHandler(indexLinkPaths(styxConfig)));
httpRouter.aggregate("/version.txt", new VersionTextHandler(styxConfig.versionFiles(startupConfig)));
httpRouter.aggregate("/admin", new IndexHandler(indexLinkPaths(styxConfig)));
httpRouter.aggregate("/admin/uptime", new UptimeHandler(environment.metricRegistry()));
httpRouter.aggregate("/admin/ping", new PingHandler());
httpRouter.aggregate("/admin/threads", new ThreadsHandler());
httpRouter.aggregate("/admin/current_requests", new CurrentRequestsHandler(CurrentRequestTracker.INSTANCE));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright (C) 2013-2019 Expedia 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.hotels.styx.admin.handlers;

import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.api.MetricRegistry;
import com.hotels.styx.api.WebServiceHandler;

import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.HttpResponseStatus.OK;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Provides an uptime via admin interface.
*/
public class UptimeHandler implements WebServiceHandler {
private final MetricRegistry metricRegistry;

public UptimeHandler(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
}

@Override
public Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context) {
Object uptime = metricRegistry.getGauges().get("jvm.uptime.formatted").getValue();

return Eventual.of(HttpResponse.response(OK)
.disableCaching()
.addHeader(CONTENT_TYPE, "application/json")
.body(format("\"%s\"", uptime), UTF_8)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.hotels.styx.admin

import com.hotels.styx.support.StyxServerProvider
import com.hotels.styx.support.adminRequest
import io.kotlintest.Spec
import io.kotlintest.matchers.string.shouldMatch
import io.kotlintest.specs.FeatureSpec
import java.nio.charset.StandardCharsets.UTF_8

class AdminInterfaceSpec : FeatureSpec() {
val styxServer = StyxServerProvider(
defaultConfig = """
---
proxy:
connectors:
http:
port: 0
admin:
connectors:
http:
port: 0
httpPipeline:
type: StaticResponseHandler
config:
status: 200
""".trimIndent()
)

init {
feature("Styx Server Admin Interface") {
styxServer.restart()

scenario("Uptime endpoint") {
styxServer.adminRequest("/admin/uptime")
.bodyAs(UTF_8)
.shouldMatch("\"[0-9]{1,2}d [0-9]{1,2}h [0-9]{1,2}m\"".toRegex())
}
}
}

override fun afterSpec(spec: Spec) {
styxServer.stop()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@ package com.hotels.styx.admin

import com.google.common.net.MediaType.PLAIN_TEXT_UTF_8
import com.hotels.styx.api.HttpHandler
import com.hotels.styx.api.HttpHeaderNames.HOST
import com.hotels.styx.api.HttpInterceptor
import com.hotels.styx.api.HttpRequest.get
import com.hotels.styx.api.HttpResponse
import com.hotels.styx.api.LiveHttpRequest
import com.hotels.styx.api.plugins.spi.Plugin
import com.hotels.styx.client.StyxHttpClient
import com.hotels.styx.common.http.handler.HttpAggregator
import com.hotels.styx.common.http.handler.StaticBodyHttpHandler
import com.hotels.styx.support.StyxServerProvider
import com.hotels.styx.support.adminHostHeader
import com.hotels.styx.support.wait
import com.hotels.styx.support.adminRequest
import io.kotlintest.Spec
import io.kotlintest.matchers.string.shouldInclude
import io.kotlintest.shouldBe
Expand Down Expand Up @@ -117,14 +112,6 @@ class PluginAdminInterfaceSpec : FeatureSpec() {
}
}

val client: StyxHttpClient = StyxHttpClient.Builder().build()

fun StyxServerProvider.adminRequest(endpoint: String): HttpResponse = client
.send(get(endpoint)
.header(HOST, this().adminHostHeader())
.build())
.wait()

override fun afterSpec(spec: Spec) {
styxServer.stop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.hotels.styx.StyxConfig
import com.hotels.styx.StyxServer
import com.hotels.styx.api.HttpHeaderNames
import com.hotels.styx.api.HttpHeaderNames.HOST
import com.hotels.styx.api.HttpRequest
import com.hotels.styx.api.HttpResponse
Expand Down Expand Up @@ -106,6 +107,14 @@ class StyxServerProvider(
}
}

val testClient: StyxHttpClient = StyxHttpClient.Builder().build()

fun StyxServerProvider.adminRequest(endpoint: String, debug: Boolean = false): HttpResponse = testClient
.send(HttpRequest.get(endpoint)
.header(HttpHeaderNames.HOST, this().adminHostHeader())
.build())
.wait(debug = debug)

fun CompletableFuture<HttpResponse>.wait(debug: Boolean = false) = this.toMono()
.doOnNext {
if (debug) {
Expand Down

0 comments on commit a53e976

Please sign in to comment.