Skip to content

Commit

Permalink
keeping swagger config in separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-ebi committed Feb 19, 2024
1 parent 78be176 commit bc8c836
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package uk.ac.ebi.eva.submission;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
Expand All @@ -15,10 +12,4 @@ public class SubmissionApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SubmissionApplication.class, args);
}

@Bean
public OpenAPI contentApi() {
return new OpenAPI().info(new Info().title("EVA Submission Webservices").version("1.0"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.ac.ebi.eva.submission.controller.swagger;


import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class SwaggerConfig extends WebMvcConfigurerAdapter {

@Autowired
private SwaggerInterceptAdapter interceptAdapter;

@Bean
public OpenAPI seqColOpenAPI() {
return new OpenAPI()
.info(new Info().title("EVA Submission Webservices")
.description("A Service that allows users to submit to the EVA")
.version("v1.0")
.license(new License().name("Apache-2.0").url("https://raw.githubusercontent.com/EBIvariation/eva-submission-ws/main/LICENSE"))
.contact(new Contact().name("GitHub Repository").url("https://github.com/EBIvariation/eva-submission-ws").email(null)));
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptAdapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.ac.ebi.eva.submission.controller.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class SwaggerInterceptAdapter extends HandlerInterceptorAdapter {

@Value("${server.servlet.context-path:/}")
private String contextPath;

@Override
/**
* Redirecting to the swagger home page*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
String req = request.getRequestURI();

if (req.equals(contextPath) || req.equals(contextPath + "/") ||
req.equals(contextPath + "/v1") || req.equals(contextPath + "/v1/")) {
response.sendRedirect(contextPath + "/swagger-ui/index.html");
return false;
}

return super.preHandle(request, response, handler);
}
}

0 comments on commit bc8c836

Please sign in to comment.