Permalink
Browse files

first commit

  • Loading branch information...
paulosuzart committed Oct 14, 2011
0 parents commit bb22a604ffb811f334761fee9d456d74d400dac8
Showing with 135 additions and 0 deletions.
  1. +5 −0 README
  2. +16 −0 build.sbt
  3. +3 −0 project/plugins.sbt
  4. +111 −0 src/main/scala/codemountain/finagle/Servers.scala
5 README
@@ -0,0 +1,5 @@
+Todo:
+
+1. Implementar o verificador de assinatura como filtro
+2. Criar projeto bill_it-http para guardar o pacote http
+3. Acesso ao reposit�rio de chaves para obter identidade do usu�rio assim como chave privada (atende todo 1)
@@ -0,0 +1,16 @@
+name := "hellofinagle"
+
+version := "1.0"
+
+scalaVersion := "2.9.1"
+
+fork in run := true
+
+resolvers ++= Seq("Twitter Repo" at "http://maven.twttr.com/",
+ "Scala Tools Releases" at "https://repository.jboss.org/nexus/content/repositories/scala-tools-releases")
+
+libraryDependencies ++= Seq("com.twitter" % "finagle-core" % "1.9.2",
+ "com.twitter" % "finagle-http" % "1.9.2",
+ "com.twitter" % "util" % "1.11.8",
+ "org.mockito" % "mockito-all" % "1.8.5" % "test" withSources(),
+ "org.scala-tools.testing" %% "specs" % "1.6.9" % "test" withSources())
@@ -0,0 +1,3 @@
+resolvers += Classpaths.typesafeResolver
+
+addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse" % "1.4.0")
@@ -0,0 +1,111 @@
+package codemountain.finagle
+
+
+import org.jboss.netty.handler.codec.http.HttpRequest
+import com.twitter.util.Future
+import org.jboss.netty.handler.codec.http.DefaultHttpResponse
+import org.jboss.netty.util.CharsetUtil.UTF_8
+import com.twitter.finagle.http.Status.{ Ok, MethodNotAllowed }
+import com.twitter.finagle.Service
+import org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1
+import org.jboss.netty.handler.codec.http.HttpResponse
+import org.jboss.netty.buffer.ChannelBuffers.copiedBuffer
+import com.twitter.finagle.builder.ServerBuilder
+import com.twitter.finagle.http.Http
+import com.twitter.finagle.builder.Server
+import java.net.InetSocketAddress
+import com.twitter.finagle.http.Response
+import com.twitter.finagle.http.Request
+import com.twitter.finagle.http.path.Path
+import com.twitter.finagle.http.path._
+import com.twitter.finagle.http.RichHttp
+import com.twitter.finagle.http.filter.ExceptionFilter
+import org.jboss.netty.handler.codec.http.HttpMethod
+import com.twitter.finagle.http.Method._
+
+object Helpers {
+ val methNotAllwd: PartialFunction[(HttpMethod, Request), Future[Response]] = {
+ case (_, request) =>
+ val response = request.response
+ response.status = MethodNotAllowed
+ Future.value(response)
+ }
+
+ def ~~(body: PartialFunction[(HttpMethod, Request), Future[Response]]) = {
+ new Service[Request, Response] {
+ def apply(request: Request): Future[Response] = {
+
+ val pf: PartialFunction[(HttpMethod, Request), Future[Response]] = {
+ body orElse methNotAllwd
+ }
+ pf(request.method, request)
+ }
+ }
+
+ }
+}
+
+class SimpleService extends Service[HttpRequest, HttpResponse] {
+ def apply(request: HttpRequest): Future[HttpResponse] = {
+ val response = new DefaultHttpResponse(HTTP_1_1, Ok)
+ Future.value(response)
+ }
+}
+
+class AdvancedService extends Service[Request, Response] {
+ def apply(request: Request): Future[Response] = {
+ (Path(request.path)) match {
+ case Root / "user" / Integer(id) =>
+ val response = request.response
+ response.setContentString("The user id is %d\n\n" format id)
+ Future.value(response)
+ }
+ }
+}
+
+object Simple {
+
+ def main(args: Array[String]) {
+ val service = new SimpleService
+ val server: Server = ServerBuilder()
+ .codec(new Http)
+ .bindTo(new InetSocketAddress(8099))
+ .name("simple")
+ .build(service)
+ }
+
+}
+
+object Advanced {
+ def main(args: Array[String]) {
+ val advancedService = new AdvancedService
+
+ val serve: Server = ServerBuilder()
+ .codec(new RichHttp[Request](new Http()))
+ .bindTo(new InetSocketAddress(8099))
+ .name("advanced")
+ .build(ExceptionFilter andThen advancedService)
+
+ }
+}
+
+object SuperAdvanced {
+ import Helpers._
+
+ val superAd = ~~ {
+ case (Get, request) =>
+ (Path(request.path)) match {
+ case Root / "user" / Integer(id) =>
+ val response = request.response
+ response.setContentString("The user id is %d\n\n" format id)
+ Future.value(response)
+ }
+ }
+
+ val serve: Server = ServerBuilder()
+ .codec(new RichHttp[Request](new Http()))
+ .bindTo(new InetSocketAddress(8099))
+ .name("suprAd")
+ .build(ExceptionFilter andThen superAd)
+
+}

0 comments on commit bb22a60

Please sign in to comment.