-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Setup server JSON serialization * WIP: Fetch lessons from private repo * Introduce dev mode for the server * WIP: Lessons API * Lessons API almost working! * Configure polymorphic serialization for the LessonModel * Make `:shared` a library
- Loading branch information
1 parent
e763950
commit 9a4fec5
Showing
18 changed files
with
262 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,36 @@ | ||
package ivy.learn | ||
|
||
import io.ktor.serialization.kotlinx.json.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.engine.* | ||
import io.ktor.server.netty.* | ||
import io.ktor.server.plugins.contentnegotiation.* | ||
import ivy.di.Di | ||
import ivy.di.SharedModule | ||
import ivy.learn.data.di.DataModule | ||
import ivy.learn.di.AppModule | ||
import kotlinx.serialization.json.Json | ||
|
||
fun main() { | ||
Di.init(modules = setOf(SharedModule, DataModule, AppModule)) | ||
fun main(args: Array<String>) { | ||
val devMode = "dev" in args | ||
Di.init(modules = setOf(SharedModule, DataModule, AppModule(devMode = devMode))) | ||
val app = Di.get<LearnServer>() | ||
|
||
val port = System.getenv("PORT")?.toInt() ?: 8081 | ||
println("Starting server on port $port...") | ||
embeddedServer( | ||
Netty, | ||
port = System.getenv("PORT")?.toInt() ?: 8080, | ||
port = port, | ||
host = "0.0.0.0", | ||
module = { app.init(this) }, | ||
module = { | ||
configureSever() | ||
app.init(this) | ||
}, | ||
).start(wait = true) | ||
} | ||
|
||
private fun Application.configureSever() { | ||
install(ContentNegotiation) { | ||
json(json = Di.get<Json>()) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ivy.learn.api | ||
|
||
import arrow.core.raise.ensureNotNull | ||
import io.ktor.server.routing.* | ||
import ivy.learn.api.common.Api | ||
import ivy.learn.api.common.endpoint | ||
import ivy.learn.api.common.model.ServerError | ||
import ivy.learn.data.repository.LessonsRepository | ||
import ivy.model.Lesson | ||
import ivy.model.LessonId | ||
|
||
class LessonsApi( | ||
private val repository: LessonsRepository | ||
) : Api { | ||
override fun Routing.endpoints() { | ||
// Endpoint that gets a lesson by ID | ||
get("/lesson/{id}", endpoint<Lesson> { params -> | ||
val lessonId = params["id"]?.let(::LessonId) | ||
ensureNotNull(lessonId) { ServerError.BadRequest("Lesson ID is missing!") } | ||
repository.fetchLessonById(lessonId) | ||
.mapLeft(ServerError::Unknown).bind() | ||
}) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ivy.learn.api | ||
|
||
import io.ktor.server.routing.* | ||
import ivy.learn.api.common.Api | ||
import ivy.learn.api.common.endpoint | ||
import kotlinx.serialization.Serializable | ||
|
||
class StatusApi : Api { | ||
override fun Routing.endpoints() { | ||
get("/hello", endpoint { | ||
HelloResponse( | ||
message = "Hello, world!", | ||
time = System.currentTimeMillis(), | ||
) | ||
}) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class HelloResponse( | ||
val message: String, | ||
val time: Long, | ||
) |
2 changes: 1 addition & 1 deletion
2
server/src/main/kotlin/ivy/learn/api/Api.kt → ...c/main/kotlin/ivy/learn/api/common/Api.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ivy.learn.api | ||
package ivy.learn.api.common | ||
|
||
import io.ktor.server.routing.* | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ivy.learn.api.common | ||
|
||
import arrow.core.raise.Raise | ||
import arrow.core.raise.either | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.util.pipeline.* | ||
import ivy.learn.api.common.model.ServerError | ||
import ivy.learn.api.common.model.ServerErrorResponse | ||
|
||
inline fun <reified T : Any> endpoint( | ||
crossinline handler: suspend Raise<ServerError>.(Parameters) -> T | ||
): suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit = { | ||
either { | ||
handler(call.parameters) | ||
}.onLeft { error -> | ||
call.respond( | ||
status = when (error) { | ||
is ServerError.BadRequest -> HttpStatusCode.BadRequest | ||
is ServerError.Unknown -> HttpStatusCode.InternalServerError | ||
}, | ||
message = ServerErrorResponse(error.msg) | ||
) | ||
}.onRight { response -> | ||
call.respond(HttpStatusCode.OK, response) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
server/src/main/kotlin/ivy/learn/api/common/model/ServerError.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ivy.learn.api.common.model | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
sealed interface ServerError { | ||
val msg: String | ||
|
||
data class BadRequest(override val msg: String) : ServerError | ||
data class Unknown(override val msg: String) : ServerError | ||
} | ||
|
||
@Serializable | ||
data class ServerErrorResponse( | ||
val message: String, | ||
) |
This file contains 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
17 changes: 17 additions & 0 deletions
17
server/src/main/kotlin/ivy/learn/data/repository/LessonsRepository.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ivy.learn.data.repository | ||
|
||
import arrow.core.Either | ||
import arrow.core.raise.catch | ||
import ivy.learn.data.source.LessonDataSource | ||
import ivy.model.Lesson | ||
import ivy.model.LessonId | ||
|
||
class LessonsRepository( | ||
private val lessonDataSource: LessonDataSource | ||
) { | ||
suspend fun fetchLessonById(id: LessonId): Either<String, Lesson> = catch({ | ||
Either.Right(lessonDataSource.fetchLessonById(id)) | ||
}, { | ||
Either.Left("Failed to fetch '${id.value}' lesson: $it}") | ||
}) | ||
} |
22 changes: 22 additions & 0 deletions
22
server/src/main/kotlin/ivy/learn/data/source/LessonDataSource.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ivy.learn.data.source | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import ivy.model.Lesson | ||
import ivy.model.LessonId | ||
|
||
class LessonDataSource( | ||
private val httpClient: HttpClient | ||
) { | ||
suspend fun fetchLessonById(id: LessonId): Lesson = httpClient.get( | ||
urlString = "https://raw.githubusercontent.com/Ivy-Apps/learn-content" + | ||
"/main/content/lessons" + | ||
"/${id.value}.json" | ||
) { | ||
headers { | ||
append("Authorization", "token ${System.getenv("IVY_LEARN_GITHUB_PAT")}") | ||
append("Accept", "application/vnd.github.v3+json") | ||
} | ||
}.body<Lesson>() | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.