Skip to content

Commit

Permalink
Add a possibility to configure CORS.
Browse files Browse the repository at this point in the history
  • Loading branch information
meier-rene committed Dec 19, 2024
1 parent c10786d commit e270b33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ release. This directory needs to be configured in the environment variable
git clone https://github.com/MassBank/MassBank-data.git
export MB_DATA_DIRECTORY="./MassBank-data"
```
To configure CORS you can set the environment variable `CORS_ALLOWED_ORIGINS`.
```bash
export CORS_ALLOWED_ORIGINS=http://localhost:3000
```

## Build and Run
Build the service with Maven:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.ipb_halle.massbank3_export_service.api;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig {

@Value("${cors.allowedOrigins:*}")
private String allowedOrigins;

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(@NonNull CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(allowedOrigins.split(","))
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*");
}
};
}
}

0 comments on commit e270b33

Please sign in to comment.