26
26
import com .graphhopper .navigation .NavigateResource ;
27
27
import io .dropwizard .Application ;
28
28
import io .dropwizard .assets .AssetsBundle ;
29
+ import io .dropwizard .configuration .EnvironmentVariableSubstitutor ;
30
+ import io .dropwizard .configuration .SubstitutingSourceProvider ;
29
31
import io .dropwizard .setup .Bootstrap ;
30
32
import io .dropwizard .setup .Environment ;
31
-
32
- import javax .servlet .DispatcherType ;
33
+ import io .swagger .v3 .core .converter .ModelConverters ;
34
+ import io .swagger .v3 .jaxrs2 .integration .JaxrsOpenApiContextBuilder ;
35
+ import io .swagger .v3 .jaxrs2 .integration .resources .OpenApiResource ;
36
+ import io .swagger .v3 .oas .integration .OpenApiConfigurationException ;
37
+ import io .swagger .v3 .oas .integration .SwaggerConfiguration ;
38
+ import io .swagger .v3 .oas .models .OpenAPI ;
39
+ import io .swagger .v3 .oas .models .info .Info ;
40
+ import io .swagger .v3 .oas .models .servers .Server ;
41
+ import java .util .Collections ;
33
42
import java .util .EnumSet ;
43
+ import java .util .stream .Collectors ;
44
+ import java .util .stream .Stream ;
45
+ import javax .servlet .DispatcherType ;
34
46
35
47
public final class GraphHopperApplication extends Application <GraphHopperServerConfiguration > {
36
48
@@ -40,6 +52,9 @@ public static void main(String[] args) throws Exception {
40
52
41
53
@ Override
42
54
public void initialize (Bootstrap <GraphHopperServerConfiguration > bootstrap ) {
55
+ bootstrap .setConfigurationSourceProvider (new SubstitutingSourceProvider (
56
+ bootstrap .getConfigurationSourceProvider (),
57
+ new EnvironmentVariableSubstitutor (false )));
43
58
bootstrap .addBundle (new GraphHopperBundle ());
44
59
bootstrap .addBundle (new RealtimeBundle ());
45
60
bootstrap .addCommand (new ImportCommand ());
@@ -51,8 +66,34 @@ public void initialize(Bootstrap<GraphHopperServerConfiguration> bootstrap) {
51
66
52
67
@ Override
53
68
public void run (GraphHopperServerConfiguration configuration , Environment environment ) {
69
+ setupOpenApi (configuration .getGraphHopperConfiguration ().getString ("api_basepath" ,"http://localhost:8989/" ));
70
+ ModelConverters .getInstance ().addConverter (new ObjectNodeConverter ());
54
71
environment .jersey ().register (new RootResource ());
72
+ environment .jersey ().register (new OpenApiResource ());
55
73
environment .jersey ().register (NavigateResource .class );
56
74
environment .servlets ().addFilter ("cors" , CORSFilter .class ).addMappingForUrlPatterns (EnumSet .allOf (DispatcherType .class ), false , "*" );
57
75
}
76
+
77
+ private void setupOpenApi (String apiBasePath ) {
78
+ OpenAPI oas = new OpenAPI ()
79
+ .info (new Info ()
80
+ .title ("Graphhopper API" )
81
+ .description ("Basic Graphhopper autogenerated REST API documentation." ))
82
+ .servers (Collections .singletonList (new Server ().url (apiBasePath )));
83
+
84
+ SwaggerConfiguration oasConfig = new SwaggerConfiguration ()
85
+ .openAPI (oas )
86
+ .prettyPrint (true )
87
+ .resourcePackages (Stream .of ("com.graphhopper" ).collect (Collectors .toSet ()));
88
+
89
+ try {
90
+ new JaxrsOpenApiContextBuilder ()
91
+ //.servletConfig(servletConfig)
92
+ //.application(this)
93
+ .openApiConfiguration (oasConfig )
94
+ .buildContext (true );
95
+ } catch (OpenApiConfigurationException e ) {
96
+ throw new RuntimeException (e .getMessage (), e );
97
+ }
98
+ }
58
99
}
0 commit comments