Skip to content

Commit

Permalink
Add swagger UI
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Sep 20, 2024
1 parent ce3403d commit 7429278
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
api("org.http4k:http4k-config")
api("org.http4k:http4k-multipart")
api("org.http4k:http4k-contract")
api("org.http4k:http4k-contract-ui-swagger")
api("org.http4k:http4k-format-jackson")
api("org.http4k:http4k-security-oauth")
api("org.http4k:http4k-template-rocker")
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/kotlin/http4kbox/Http4kBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fun Http4kBox(env: Environment, http: HttpHandler, clock: Clock): HttpHandler {
return CatchAll().then(
routes(
"/oauth/callback" bind GET to oAuthProvider.callback,
oAuthProvider.authFilter.then(routes(Api(CREDENTIALS(env), fs), web(fs)))
"/api" bind Api(CREDENTIALS(env), fs),
oAuthProvider.authFilter.then(web(fs))
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/http4kbox/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import org.http4k.lens.value
object Settings {
val OAUTH_BASE_URI by EnvironmentKey.uri().of().defaulted(Uri.of("https://github.com"))
val CALLBACK by EnvironmentKey.uri().of().required()
val CREDENTIALS by EnvironmentKey.map { it.split(":") }.map { Credentials(it[0], it[0]) }.of().required()
val CREDENTIALS by EnvironmentKey.map { it.split(":") }.map { Credentials(it[0], it[1]) }.of().required()
val AWS_BUCKET by EnvironmentKey.value(BucketName).of().required()
}
21 changes: 16 additions & 5 deletions app/src/main/kotlin/http4kbox/endpoints/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ import org.http4k.contract.contract
import org.http4k.contract.openapi.ApiInfo
import org.http4k.contract.openapi.v3.OpenApi3
import org.http4k.contract.security.ApiKeySecurity
import org.http4k.contract.ui.swagger.swaggerUiWebjar
import org.http4k.core.Credentials
import org.http4k.lens.Header
import org.http4k.routing.routes

fun Api(credentials: Credentials, fs: FileStorage) = contract {
security = ApiKeySecurity(Header.required("http4k-api-key"), { it == credentials.password })
renderer = OpenApi3(ApiInfo("http4kbox", "0.0"))
routes += ListFiles(fs)
}
fun Api(credentials: Credentials, fs: FileStorage) =
routes(
swaggerUiWebjar {
pageTitle = "http4kbox API"
url = "/api/openapi.json"
},
contract {
descriptionPath = "/openapi.json"
println(credentials.password)
security = ApiKeySecurity(Header.required("http4k-api-key"), { it == credentials.password })
renderer = OpenApi3(ApiInfo("http4kbox", "0.0"))
routes += ListFiles(fs)
}
)
3 changes: 1 addition & 2 deletions app/src/test/kotlin/env/DevRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ fun main() {

val github = FakeGitHub(clock).asServer(SunHttp(10000)).start()


val http = reverseProxy(
"github" to SetBaseUriFrom(Uri.of("http://localhost:${github.port()}")).then(JavaHttpClient()),
"localhost" to SetBaseUriFrom(Uri.of("http://localhost:${github.port()}")).then(JavaHttpClient()),
"s3" to SetBaseUriFrom(Uri.of("http://localhost:${s3.port()}")).then(JavaHttpClient()),
)

Expand Down

0 comments on commit 7429278

Please sign in to comment.