Skip to content

Commit

Permalink
Bump finatra
Browse files Browse the repository at this point in the history
- Bump Finatra version to 0.2.1
- Fixes for Finatra changes

Author: @franklinhu
Fixes #84
URL: #84
  • Loading branch information
Franklin Hu committed Jul 25, 2012
1 parent 2082fc8 commit f6e8543
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 86 deletions.
2 changes: 1 addition & 1 deletion project/Project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ object Zipkin extends Build {
resolvers += "codahale" at "http://repo.codahale.com",

libraryDependencies ++= Seq(
"com.posterous" % "finatra" % "4.3.2",
"com.twitter" % "finatra" % "0.2.1",

"com.twitter.common.zookeeper" % "server-set" % "1.0.7",

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,7 @@
</div>
<div class="container">
<div class="content">
{{#flash}}
{{#message}}
<div class="alert alert-success high-visibility">
<a class="close" data-dismiss="alert" href="#">x</a>
<p><strong>{{body}}</strong></p>
</div>
{{/message}}

{{#error}}
<div class="alert alert-error high-visibility">
<a class="close" data-dismiss="alert" href="#">x</a>
<p><strong>{{body}}</strong></p>
</div>
{{/error}}
{{/flash}}

{{render}}
{{body}}
</div>
</div>
</body>
Expand Down
66 changes: 34 additions & 32 deletions zipkin-finatra/src/main/scala/com/twitter/zipkin/web/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package com.twitter.zipkin.web

import com.posterous.finatra.{Request, FinatraApp}
import com.twitter.finatra.{Response, Controller, View, Request}
import com.twitter.logging.Logger
import com.twitter.util.Future
import com.twitter.zipkin.adapter.{JsonQueryAdapter, JsonAdapter, ThriftQueryAdapter, ThriftAdapter}
Expand All @@ -25,14 +25,13 @@ import com.twitter.zipkin.config.ZipkinWebConfig
import java.nio.ByteBuffer
import java.text.SimpleDateFormat
import java.util.Calendar
import org.jboss.netty.handler.codec.http.HttpResponse

/**
* Application that handles ZipkinWeb routes
* @param config ZipkinWebConfig
* @param client Thrift client to ZipkinQuery
*/
class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) extends FinatraApp {
class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) extends Controller {

val log = Logger.get()
val dateFormat = new SimpleDateFormat("MM-dd-yyyy")
Expand All @@ -42,17 +41,17 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten

/* Index page */
get("/") { request =>
render(path = "index.mustache", exports = new IndexObject(getDate, getTime))
render.view(wrapView(new IndexView(getDate, getTime))).toFuture
}

/* Trace page */
get("/show/:id") { request =>
render(path = "show.mustache", exports = new ShowObject(request.params("id")))
render.view(wrapView(new ShowView(request.params("id")))).toFuture
}

/* Static page for render trace from JSON */
get("/static") { request =>
render(path = "static.mustache", exports = new StaticObject)
render.view(wrapView(new StaticView)).toFuture
}

/**
Expand Down Expand Up @@ -101,7 +100,7 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
}
}
}
}.flatten.map(toJson(_)).flatten
}.map(render.json(_))
}

/**
Expand All @@ -111,8 +110,8 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
get("/api/services") { request =>
log.debug("/api/services")
client.getServiceNames().map { services =>
toJson(services.toSeq.sorted)
}.flatten
render.json(services.toSeq.sorted)
}
}

/**
Expand All @@ -126,8 +125,8 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
log.debug("/api/spans")
withServiceName(request) { serviceName =>
client.getSpanNames(serviceName).map { spans =>
toJson(spans.toSeq.sorted)
}.flatten
render.json(spans.toSeq.sorted)
}
}
}

Expand All @@ -141,8 +140,8 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
get("/api/top_annotations") { request =>
withServiceName(request) { serviceName =>
client.getTopAnnotations(serviceName).map { anns =>
toJson(anns.toSeq.sorted)
}.flatten
render.json(anns.toSeq.sorted)
}
}
}

Expand All @@ -156,8 +155,8 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
get("/api/top_kv_annotations") { request =>
withServiceName(request) { serviceName =>
client.getTopKeyValueAnnotations(serviceName).map { anns =>
toJson(anns.toSeq.sorted)
}.flatten
render.json(anns.toSeq.sorted)
}
}
}

Expand All @@ -178,8 +177,8 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
log.debug(ids.toString())

client.getTraceCombosByIds(ids, adjusters).map { _.map { ThriftQueryAdapter(_) }.head }.map { combo =>
toJson(JsonQueryAdapter(combo))
}.flatten
render.json(JsonQueryAdapter(combo))
}
}

/**
Expand All @@ -191,7 +190,7 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
*/
get("/api/is_pinned/:id") { request =>
val id = request.params("id").toLong
client.getTraceTimeToLive(id).map(toJson(_)).flatten
client.getTraceTimeToLive(id).map(render.json(_))
}

/**
Expand All @@ -206,24 +205,24 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
val id = request.params("id").toLong
request.params("state").toLowerCase match {
case "true" => {
togglePinState(id, true).map(toJson(_)).flatten
togglePinState(id, true).map(render.json(_))
}
case "false" => {
togglePinState(id, false).map(toJson(_)).flatten
togglePinState(id, false).map(render.json(_))
}
case _ => {
render(400, "Must be true or false")
render.status(400).body("Must be true or false").toFuture
}
}
}

private def withServiceName(request: Request)(f: String => Future[HttpResponse]): Future[HttpResponse] = {
private def withServiceName(request: Request)(f: String => Future[Response]): Future[Response] = {
request.params.get("serviceName") match {
case Some(s) => {
f(s)
}
case None => {
render(401, "Invalid service name")
render.status(401).body("Invalid service name").toFuture
}
}
}
Expand Down Expand Up @@ -258,23 +257,26 @@ class App(config: ZipkinWebConfig, client: gen.ZipkinQuery.FinagledClient) exten
}
}
}
}

trait Attribute
trait ExportObject {
def environment: Attribute = new Attribute { def production = false }
def flash: Option[Attribute] = None
val clockSkew: Boolean = true
private def wrapView(v: View) = new View {
val template = "templates/layouts/application.mustache"
val rootUrl = config.rootUrl
val innerView: View = v
lazy val body = innerView.render
}
}

class IndexObject(val endDate: String, val endTime: String) extends ExportObject {
class IndexView(val endDate: String, val endTime: String) extends View {
val template = "templates/index.mustache"
val inlineJs = "$(Zipkin.Application.Index.initialize());"
}

class ShowObject(traceId: String) extends ExportObject {
class ShowView(traceId: String) extends View {
val template = "templates/show.mustache"
val inlineJs = "$(Zipkin.Application.Show.initialize(\"" + traceId + "\"));"
}

class StaticObject extends ExportObject {
class StaticView extends View {
val template = "templates/static.mustache"
val inlineJs = "$(Zipkin.Application.Static.initialize());"
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.twitter.zipkin.web

import com.twitter.finatra.Request
import com.twitter.util.Time
import com.twitter.zipkin.gen
import java.text.SimpleDateFormat
import com.twitter.util.Time
import com.posterous.finatra.Request

object QueryRequest {
val fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.twitter.zipkin.web

import com.twitter.zipkin.config.ZipkinWebConfig
import com.posterous.finatra.FinatraServer.FinatraService
import com.twitter.common.zookeeper.ServerSetImpl
import com.twitter.finagle.http.Http
import java.net.InetSocketAddress
import com.twitter.finagle.builder.{ClientBuilder, ServerBuilder, Server}
import com.twitter.finagle.thrift.ThriftClientFramedCodec
import com.twitter.finagle.zookeeper.ZookeeperServerSetCluster
import com.twitter.finatra.{AppService, Controller, FinatraServer}
import com.twitter.ostrich.admin.{ServiceTracker, Service}
import com.twitter.logging.Logger
import com.posterous.finatra.{FinatraResponse, FinatraApp, FileHandler, FinatraServer}
import com.twitter.io.{Files, TempFile}
import com.twitter.zipkin.config.ZipkinWebConfig
import com.twitter.zipkin.gen
import com.twitter.finagle.builder.{ClientBuilder, ServerBuilder, Server}
import com.twitter.finagle.thrift.ThriftClientFramedCodec
import com.twitter.common.zookeeper.ServerSetImpl
import com.twitter.finagle.zookeeper.ZookeeperServerSetCluster
import java.net.InetSocketAddress

class ZipkinWeb(config: ZipkinWebConfig) extends Service {

Expand Down Expand Up @@ -46,11 +45,9 @@ class ZipkinWeb(config: ZipkinWebConfig) extends Service {

FinatraServer.register(resource)
FinatraServer.register(app)
FinatraServer.layoutHelperFactory = new ZipkinLayoutHelperFactory(config)

val finatraService = new FinatraService
val fileHandler = new FileHandler
val service = fileHandler andThen finatraService
val finatraService = new AppService
val service = finatraService

server = Some {
ServerBuilder()
Expand All @@ -68,19 +65,14 @@ class ZipkinWeb(config: ZipkinWebConfig) extends Service {
}
}

class Resource(resourceDirs: Map[String, String]) extends FinatraApp {
class Resource(resourceDirs: Map[String, String]) extends Controller {
resourceDirs.foreach { case (dir, contentType) =>
get("/public/" + dir + "/:id") { request =>
val file = TempFile.fromResourcePath("/public/" + dir + "/" + request.params("id"))
if (file.exists()) {
val resp = new FinatraResponse
resp.status = 200
resp.binBody = Some(Files.readBytes(file))
resp.headers += ("Content-Type" -> contentType)
resp.layout = None
resp.build
render.status(200).body(Files.readBytes(file)).header("Content-Type", contentType).toFuture
} else {
response(status = 404, body = "Not Found")
render.status(404).body("Not Found").toFuture
}
}
}
Expand Down

0 comments on commit f6e8543

Please sign in to comment.