Add support for rendering mustache views and start with the get/200 view
This commit is contained in:
parent
95dc237b05
commit
be60079b1c
|
@ -31,6 +31,7 @@ repositories {
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'org.codehaus.groovy:groovy-all:[2.4.7,2.5.0)'
|
compile 'org.codehaus.groovy:groovy-all:[2.4.7,2.5.0)'
|
||||||
compile "io.dropwizard:dropwizard-core:${dropwizardVersion}"
|
compile "io.dropwizard:dropwizard-core:${dropwizardVersion}"
|
||||||
|
compile "io.dropwizard:dropwizard-views-mustache:${dropwizardVersion}"
|
||||||
|
|
||||||
testCompile "io.dropwizard:dropwizard-testing:${dropwizardVersion}"
|
testCompile "io.dropwizard:dropwizard-testing:${dropwizardVersion}"
|
||||||
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
|
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
|
||||||
|
|
|
@ -21,9 +21,11 @@ package io.lasagna.httpwizard
|
||||||
import io.lasagna.httpwizard.checks.VersionCheck
|
import io.lasagna.httpwizard.checks.VersionCheck
|
||||||
import io.lasagna.httpwizard.resources.GetResource
|
import io.lasagna.httpwizard.resources.GetResource
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap
|
||||||
import io.dropwizard.Application
|
import io.dropwizard.Application
|
||||||
import io.dropwizard.setup.Bootstrap
|
import io.dropwizard.setup.Bootstrap
|
||||||
import io.dropwizard.setup.Environment
|
import io.dropwizard.setup.Environment
|
||||||
|
import io.dropwizard.views.ViewBundle
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HttpWizard is the main Dropwizard application class
|
* HttpWizard is the main Dropwizard application class
|
||||||
|
@ -44,6 +46,11 @@ class HttpWizard extends Application<HttpWizardConfiguration> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void initialize(Bootstrap<HttpWizardConfiguration> bootstrap) {
|
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
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
|
|
||||||
package io.lasagna.httpwizard
|
package io.lasagna.httpwizard
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty
|
||||||
|
import com.google.common.collect.ImmutableMap
|
||||||
import io.dropwizard.Configuration
|
import io.dropwizard.Configuration
|
||||||
import org.hibernate.validator.constraints.NotEmpty
|
import org.hibernate.validator.constraints.NotEmpty
|
||||||
|
|
||||||
|
@ -29,4 +31,8 @@ import org.hibernate.validator.constraints.NotEmpty
|
||||||
class HttpWizardConfiguration extends Configuration {
|
class HttpWizardConfiguration extends Configuration {
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
String appVersion
|
String appVersion
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
private ImmutableMap<String, ImmutableMap<String, String>> \
|
||||||
|
viewRendererConfiguration = ImmutableMap.of()
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,9 @@
|
||||||
package io.lasagna.httpwizard
|
package io.lasagna.httpwizard
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty
|
import com.fasterxml.jackson.annotation.JsonProperty
|
||||||
|
import javax.ws.rs.core.HttpHeaders
|
||||||
import javax.ws.rs.core.MultivaluedMap
|
import javax.ws.rs.core.MultivaluedMap
|
||||||
|
import javax.ws.rs.core.UriInfo
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -30,4 +32,12 @@ class StandardResponse {
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
String uri
|
String uri
|
||||||
|
|
||||||
|
|
||||||
|
static StandardResponse fromRequest(UriInfo info, HttpHeaders headers) {
|
||||||
|
StandardResponse r = new StandardResponse()
|
||||||
|
r.uri = info.absolutePath
|
||||||
|
r.headers = headers.requestHeaders
|
||||||
|
return r
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,30 +20,52 @@ package io.lasagna.httpwizard.resources
|
||||||
|
|
||||||
import io.lasagna.httpwizard.StandardResponse
|
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 com.codahale.metrics.annotation.Metered
|
||||||
import groovy.transform.TypeChecked
|
import groovy.transform.TypeChecked
|
||||||
import javax.ws.rs.core.Context
|
import javax.ws.rs.core.Context
|
||||||
import javax.ws.rs.core.UriInfo
|
|
||||||
import javax.ws.rs.core.HttpHeaders
|
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.GET
|
||||||
|
import javax.ws.rs.QueryParam
|
||||||
import javax.ws.rs.Path
|
import javax.ws.rs.Path
|
||||||
import javax.ws.rs.Produces
|
import javax.ws.rs.Produces
|
||||||
import javax.ws.rs.core.MediaType
|
import io.dropwizard.views.View
|
||||||
|
|
||||||
@Path('/get')
|
@Path('/get')
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
|
||||||
@TypeChecked
|
@TypeChecked
|
||||||
class GetResource {
|
class GetResource {
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path('200')
|
@Path('200')
|
||||||
@Metered
|
@Metered
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
StandardResponse returns200(@Context UriInfo ui,
|
StandardResponse returns200(@Context UriInfo ui,
|
||||||
@Context HttpHeaders headers) {
|
@Context HttpHeaders headers) {
|
||||||
StandardResponse r = new StandardResponse()
|
return StandardResponse.fromRequest(ui, headers)
|
||||||
r.uri = ui.absolutePath
|
|
||||||
r.headers = headers.requestHeaders
|
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue