Last active
November 29, 2015 12:10
-
-
Save dakatsuka/2371beedb3c5a47efcef to your computer and use it in GitHub Desktop.
Finch test
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
name := "finchtest" | |
version := "0.1.0" | |
scalaVersion := "2.11.7" | |
resolvers += Resolver.sonatypeRepo("snapshots") | |
libraryDependencies ++= Seq( | |
"com.twitter" %% "finagle-mysql" % "6.30.0", | |
"com.github.finagle" %% "finch-core" % "0.9.2-SNAPSHOT" changing(), | |
"com.github.finagle" %% "finch-argonaut" % "0.9.2-SNAPSHOT" changing() | |
) |
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 jp.dakatsuka.finch | |
import com.twitter.finagle.Http | |
import com.twitter.finagle.exp.Mysql | |
import com.twitter.util.Await | |
import io.finch._ | |
import io.finch.argonaut._ | |
object Main { | |
def main(args: Array[String]): Unit = { | |
implicit val client = Mysql.client | |
.withCredentials("user", "password") | |
.withDatabase("database") | |
.newRichClient("127.0.0.1:3306") | |
val listUser: Endpoint[Seq[User]] = get("users") { | |
Ok(User.all) | |
} | |
val showUser: Endpoint[User] = get("users" / long) { id: Long => | |
User.find(id).map { | |
case Some(user) => Ok(user) | |
case _ => NotFound(new Exception("Record Not Found")) | |
} | |
} | |
val userService = listUser :+: showUser | |
val server = Http.serve(":8080", userService.toService) | |
Await.ready(server) | |
} | |
} |
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 jp.dakatsuka.finch | |
import argonaut.Argonaut._ | |
import argonaut.CodecJson | |
import com.twitter.finagle.exp.mysql._ | |
import com.twitter.util.Future | |
case class User(id: Long, email: String, screen_name: String) | |
object User { | |
implicit val userCodec: CodecJson[User] = | |
casecodec3(User.apply, User.unapply)("id", "email", "screen_name") | |
def all()(implicit client: Client): Future[Seq[User]] = { | |
client.select("SELECT * FROM users")(convertToEntity) | |
} | |
def find(id: Long)(implicit client: Client): Future[Option[User]] = { | |
client.prepare("SELECT * FROM users WHERE id = ?")(id).map { | |
case rs: ResultSet => rs.rows.map(convertToEntity).headOption | |
case _ => throw new Exception | |
} | |
} | |
def convertToEntity(row: Row): User = { | |
val IntValue(id) = row("id").get | |
val StringValue(email) = row("email").get | |
val StringValue(screen_name) = row("screen_name").get | |
User(id, email, screen_name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment