Skip to content

Commit

Permalink
GH-458 Generate and link frontend with a dedicated resource handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Jun 27, 2021
1 parent 0346876 commit 6757450
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,47 @@ import com.dzikoysk.openapi.annotations.HttpMethod
import com.dzikoysk.openapi.annotations.OpenApi
import com.dzikoysk.openapi.annotations.OpenApiResponse
import io.javalin.http.Context
import org.eclipse.jetty.http.HttpStatus
import org.eclipse.jetty.http.MimeTypes
import org.panda_lang.reposilite.frontend.FrontendFacade
import org.panda_lang.reposilite.web.api.RouteHandler
import org.panda_lang.reposilite.web.api.RouteMethod.GET
import org.panda_lang.reposilite.web.encoding

private const val ROUTE = "/*"

internal class FrontendHandler(private val frontendFacade: FrontendFacade) : RouteHandler {

override val route = "/js/app.js"
override val route = ROUTE
override val methods = listOf(GET)

@OpenApi(
path = "/js/app.js",
path = ROUTE,
method = HttpMethod.GET,
operationId = "getApp",
operationId = "frontend",
summary = "Get frontend application",
description = "Returns Vue.js application wrapped into one app.js file",
tags = [ "Resource" ],
responses = [ OpenApiResponse(status = "200", description = "Default response") ]
)
override fun handle(context: Context) {
with(context.result(frontendFacade.getApp())) {
header("Content-Type", "application/javascript")
res.characterEncoding = "UTF-8"
var qualifier = context.splat(0) ?: ""

if (qualifier.isEmpty()) {
qualifier = "index.html"
}

val resource = FrontendFacade::class.java.getResourceAsStream("/static/$qualifier")

if (resource == null) {
context.status(HttpStatus.NOT_FOUND_404)
return
}

context
.result(resource)
.contentType(MimeTypes.getDefaultMimeByExtension(qualifier))
.encoding("UTF-8")
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6757450

Please sign in to comment.