Add support for rendering mustache views and start with the get/200 view

This commit is contained in:
R. Tyler Croy 2016-12-18 14:37:16 -08:00
parent 95dc237b05
commit be60079b1c
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
6 changed files with 75 additions and 8 deletions

View File

@ -31,6 +31,7 @@ repositories {
dependencies {
compile 'org.codehaus.groovy:groovy-all:[2.4.7,2.5.0)'
compile "io.dropwizard:dropwizard-core:${dropwizardVersion}"
compile "io.dropwizard:dropwizard-views-mustache:${dropwizardVersion}"
testCompile "io.dropwizard:dropwizard-testing:${dropwizardVersion}"
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'

View File

@ -21,9 +21,11 @@ package io.lasagna.httpwizard
import io.lasagna.httpwizard.checks.VersionCheck
import io.lasagna.httpwizard.resources.GetResource
import com.google.common.collect.ImmutableMap
import io.dropwizard.Application
import io.dropwizard.setup.Bootstrap
import io.dropwizard.setup.Environment
import io.dropwizard.views.ViewBundle
/**
* HttpWizard is the main Dropwizard application class
@ -44,6 +46,11 @@ class HttpWizard extends Application<HttpWizardConfiguration> {
@Override
void initialize(Bootstrap<HttpWizardConfiguration> bootstrap) {
/* nothing to do yet */
bootstrap.addBundle(new ViewBundle<HttpWizardConfiguration>() {
@Override
ImmutableMap<String, ImmutableMap<String, String>> getViewConfiguration(HttpWizardConfiguration c) {
return c.viewRendererConfiguration
}
})
}
}

View File

@ -18,6 +18,8 @@
package io.lasagna.httpwizard
import com.fasterxml.jackson.annotation.JsonProperty
import com.google.common.collect.ImmutableMap
import io.dropwizard.Configuration
import org.hibernate.validator.constraints.NotEmpty
@ -29,4 +31,8 @@ import org.hibernate.validator.constraints.NotEmpty
class HttpWizardConfiguration extends Configuration {
@NotEmpty
String appVersion
@JsonProperty
private ImmutableMap<String, ImmutableMap<String, String>> \
viewRendererConfiguration = ImmutableMap.of()
}

View File

@ -19,7 +19,9 @@
package io.lasagna.httpwizard
import com.fasterxml.jackson.annotation.JsonProperty
import javax.ws.rs.core.HttpHeaders
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.UriInfo
/**
*
@ -30,4 +32,12 @@ class StandardResponse {
@JsonProperty
String uri
static StandardResponse fromRequest(UriInfo info, HttpHeaders headers) {
StandardResponse r = new StandardResponse()
r.uri = info.absolutePath
r.headers = headers.requestHeaders
return r
}
}

View File

@ -20,30 +20,52 @@ package io.lasagna.httpwizard.resources
import io.lasagna.httpwizard.StandardResponse
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.google.common.base.Charsets
import com.codahale.metrics.annotation.Metered
import groovy.transform.TypeChecked
import javax.ws.rs.core.Context
import javax.ws.rs.core.UriInfo
import javax.ws.rs.core.HttpHeaders
import javax.ws.rs.core.MediaType
//import javax.ws.rs.core.Response
import javax.ws.rs.core.UriInfo
import javax.ws.rs.DefaultValue
import javax.ws.rs.GET
import javax.ws.rs.QueryParam
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import io.dropwizard.views.View
@Path('/get')
@Produces(MediaType.APPLICATION_JSON)
@TypeChecked
class GetResource {
@GET
@Path('200')
@Metered
@Produces(MediaType.APPLICATION_JSON)
StandardResponse returns200(@Context UriInfo ui,
@Context HttpHeaders headers) {
StandardResponse r = new StandardResponse()
r.uri = ui.absolutePath
r.headers = headers.requestHeaders
return r
return StandardResponse.fromRequest(ui, headers)
}
@GET
@Path('200')
@Metered
@Produces(MediaType.TEXT_HTML)
View returns200(@DefaultValue('true') @QueryParam('pretty') boolean pretty,
@Context UriInfo ui,
@Context HttpHeaders headers) {
return new View('/views/standard-response.mustache', Charsets.UTF_8) {
String getTitle() { return ui.path }
String getUri() { return ui.absolutePath }
String getJson() {
ObjectMapper mapper = new ObjectMapper()
mapper.enable(SerializationFeature.INDENT_OUTPUT)
return mapper.writeValueAsString(StandardResponse.fromRequest(ui, headers))
}
}
}
}

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>
{{title}}
</title>
</head>
<body>
<center>
<h1>
{{title}}
</h1>
</center>
<p>
<pre><code>{{json}}</code></pre>
</p>
</body>
<!--
vim: ft=html
-->
</html>