Skip to content

Commit

Permalink
Add CORS header to /ping.
Browse files Browse the repository at this point in the history
This is allows a client to do health checks from JavaScript

Review: https://rb.mesosphe.re/r/573
  • Loading branch information
guenter committed Sep 24, 2013
1 parent 26995be commit d9c111b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/main/scala/mesosphere/chaos/http/PingServlet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package mesosphere.chaos.http

import javax.servlet.http.{HttpServletRequest, HttpServletResponse, HttpServlet}

/**
* An HTTP servlets which outputs a {@code text/plain} {@code "pong"} response.
*
* PingServlet from com.codahale.metrics, with CORBS header added.
*/

class PingServlet extends HttpServlet {

private final val CONTENT_TYPE = "text/plain"
private final val CONTENT = "pong"
private final val CACHE_CONTROL = "Cache-Control"
private final val NO_CACHE = "must-revalidate,no-cache,no-store"
private final val ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin"
private final val STAR = "*"

protected override def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
resp.setStatus(HttpServletResponse.SC_OK)
resp.setHeader(CACHE_CONTROL, NO_CACHE)
resp.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, STAR)
resp.setContentType(CONTENT_TYPE)

val writer = resp.getWriter
try {
writer.println(CONTENT)
}
finally {
writer.close()
}
}
}
2 changes: 1 addition & 1 deletion src/main/scala/mesosphere/chaos/http/RestModule.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mesosphere.chaos.http

import com.codahale.metrics.servlets.{MetricsServlet, PingServlet}
import com.codahale.metrics.servlets.MetricsServlet
import com.google.inject.{Singleton, Provides, Scopes}
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
Expand Down

0 comments on commit d9c111b

Please sign in to comment.