Skip to content

Instantly share code, notes, and snippets.

View rpiotrow's full-sized avatar

Rafal Piotrowski rpiotrow

View GitHub Profile
class EmployeeRouting<F>(
private val repository: EmployeeRepository<F>,
suspendable: Suspendable<F>): Suspendable<F> by suspendable {
fun Routing.employees(path: String) {
get(path) {
call.respond(repository
.findAll()
.suspended()
)
interface Suspendable<F> {
suspend fun <A: Any> Kind<F, A>.suspended(): A
}
fun IO.Companion.suspendable(): Suspendable<ForIO> = object: Suspendable<ForIO> {
override suspend fun <A : Any> Kind<ForIO, A>.suspended(): A {
val io = this.fix()
return io.suspended()
}
}
when(val result = repository.findAll().attempt().unsafeRunSync()) {
is Either.Right -> call.respond(result.b)
else -> call.respond(HttpStatusCode.InternalServerError)
}
fun main(args: Array<String>) {
val repository: EmployeeRepository<ForIO> = IOEmployeeRepository()
val server = embeddedServer(Netty, port = 8080) {
configureJsonSerialization()
install(Routing) {
get("/employees") {
call.respond(repository.findAll())
}
}
}
interface EmployeeRepository<F> {
fun findAll(): Kind<F, List<Employee>>
}
class IOEmployeeRepository: EmployeeRepository<ForIO> {
override fun findAll(): IO<List<Employee>> = IO {
listOf(Employee(1, "John", "Smith"))
}
}
fun main(args: Array<String>) {
val server = embeddedServer(Netty, port = 8080) {
configureJsonSerialization()
install(Routing) {
get("/employees") {
call.respond(listOf(Employee(1, "John", "Smith")))
}
}
}
server.start(wait = true)
data class Employee (
val id: Long,
val firstName: String,
val lastName: String
)

Keybase proof

I hereby claim:

  • I am rpiotrow on github.
  • I am rpiotrow (https://keybase.io/rpiotrow) on keybase.
  • I have a public key whose fingerprint is 7007 ED7C 6C39 4872 F8E1 6F31 E350 7A55 C8A3 09EF

To claim this, I am signing this object: