Skip to content

Commit

Permalink
#3 Api versions
Browse files Browse the repository at this point in the history
all is tested
  • Loading branch information
Alexander Emelianov authored and Alexander Emelianov committed Feb 19, 2019
1 parent 29ebde6 commit 49547a4
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* Enabling ZuulSpringfoxSwaggerPlugin annotation.
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package ru.reliabletech.zuul.swagger;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.zuul.filters.discovery.ServiceRouteMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import ru.reliabletech.zuul.swagger.props.ServicesSwaggerInfo;
import ru.reliabletech.zuul.swagger.service.GenericRouteService;
import ru.reliabletech.zuul.swagger.service.RouteService;
import ru.reliabletech.zuul.swagger.service.ServiceRouteMapperRouteService;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
* Main configuration for ZuulSpringfoxPlugin
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
@Configuration
@ComponentScan(basePackageClasses = ZuulSpringfoxSwaggerConfiguration.class)
@EnableConfigurationProperties(ServicesSwaggerInfo.class)
public class ZuulSpringfoxSwaggerConfiguration {
Expand All @@ -29,4 +38,25 @@ public RestTemplate restTemplate() {
return new RestTemplate();
}

/**
* Configure automatically {@link ServiceRouteMapper } based {@link RouteService }
*
* @return
*/
@Bean
@ConditionalOnBean(ServiceRouteMapper.class)
public RouteService versionedRouteService() {
return new ServiceRouteMapperRouteService();
}

/**
* Default {@link RouteService}
*
* @return
*/
@Bean
@ConditionalOnMissingBean(RouteService.class)
public RouteService genericRouteService() {
return new GenericRouteService();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import ru.reliabletech.zuul.swagger.service.SwaggerService;

/**
* Controller for api
* Controller for swagger-ui documentation requests
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.Data;

/**
* Service info
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.reliabletech.zuul.swagger.props;

import lombok.*;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
Expand All @@ -9,6 +9,8 @@
import java.util.Set;

/**
* Extended Zuul configuration for plugin purposes
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package ru.reliabletech.zuul.swagger.service;

import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;
import ru.reliabletech.zuul.swagger.props.ServicesSwaggerInfo;

/**
* General implementation
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 15.02.19.
*/
@ConditionalOnMissingBean(RouteService.class)
@Component
@AllArgsConstructor
public class GenericRouteService implements RouteService {

@Autowired
private ServicesSwaggerInfo servicesSwaggerInfo;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import org.springframework.web.client.RestTemplate;
import ru.reliabletech.zuul.swagger.exception.NotFoundException;
import ru.reliabletech.zuul.swagger.props.ServicesSwaggerInfo;
import ru.reliabletech.zuul.swagger.service.RouteService;

/**
* General implementation
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
Expand Down Expand Up @@ -44,7 +45,7 @@ public ObjectNode getOriginalSwaggerDoc(String route) {
if (e.getMessage() == null || !e.getMessage().startsWith("No instances available for")) {
throw e;
}
throw new NotFoundException(); // TODO
throw new NotFoundException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package ru.reliabletech.zuul.swagger.service;

/**
* Service for mapping routes to proxy path
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 15.02.19.
*/
public interface RouteService {

/**
* Perform route mapping to proxy path
*
* @param route
* @return
*/
String getPath(String route);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package ru.reliabletech.zuul.swagger.service;

import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.discovery.ServiceRouteMapper;
import org.springframework.stereotype.Component;
import ru.reliabletech.zuul.swagger.props.ServicesSwaggerInfo;

/**
* Realization? based on {@link ServiceRouteMapper }
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 15.02.19.
*/
@Component
@ConditionalOnBean(ServiceRouteMapper.class)
@AllArgsConstructor
public class VersionedPatternRouteService implements RouteService {
public class ServiceRouteMapperRouteService implements RouteService {

@Autowired
private ServicesSwaggerInfo servicesSwaggerInfo;
@Autowired
private ServiceRouteMapper serviceRouteMapper;

@Override
public String getPath(String route) {
return servicesSwaggerInfo.getServicePath(route)
return servicesSwaggerInfo
.getServicePath(route)
.orElseGet(() -> serviceRouteMapper.apply(route));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
* Service for operations over connected services swagger documentations
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
public interface SwaggerService {

/**
* Obtain original documentation of service, represented by route
*
* @param route
* @return
*/
ObjectNode getOriginalSwaggerDoc(String route);

/**
* Obtain modified for proxy's swagger-ui documentation of service, represented by route
*
* @param route
* @return
*/
ObjectNode getSwaggerDoc(String route);

/**
* Obtain swagger documentation version for service, represented by route
*
* @param route
* @return
*/
default String getSwaggerVersion(String route) {
ObjectNode swaggerDocumentation = getOriginalSwaggerDoc(route);
return swaggerDocumentation == null ? "" : swaggerDocumentation.get("swagger").asText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import ru.reliabletech.zuul.swagger.props.ServicesSwaggerInfo;
Expand All @@ -10,9 +11,13 @@
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Provide swagger resources for swagger-ui using discovery client available services list and configured statically route mappings
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
Expand All @@ -25,17 +30,25 @@ public class ZuulSwaggerResourceProvider implements SwaggerResourcesProvider {
private ServicesSwaggerInfo servicesSwaggerInfo;
@Autowired
private SwaggerService swaggerService;
@Autowired
private DiscoveryClient discoveryClient;

@Override
public List<SwaggerResource> get() {
return servicesSwaggerInfo.getRouteNames() // TODO by available services
.stream()
.map(this::generateSwaggerDocumentationResource)
.collect(Collectors.toList());
return Stream.concat(discoveryClient.getServices().stream(), servicesSwaggerInfo.getRouteNames().stream())
.map(this::generateSwaggerDocumentationResource)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

private SwaggerResource generateSwaggerDocumentationResource(String route) {
String swaggerVersion = swaggerService.getSwaggerVersion(route);
String swaggerVersion;
try {
swaggerVersion = swaggerService.getSwaggerVersion(route);
} catch (Exception e) {
log.error(String.format("Some error during obtain swagger documentation for route %s", route), e);
return null;
}
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(route);
swaggerResource.setLocation("/api-docs?route=" + route);
Expand Down

0 comments on commit 49547a4

Please sign in to comment.