Skip to content

Commit

Permalink
add cors config
Browse files Browse the repository at this point in the history
  • Loading branch information
bmudric committed Jul 4, 2020
1 parent 1373d79 commit 2b1f56d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/jazavac/relaysim/RelaySimApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package com.jazavac.relaysim;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Slf4j
@SpringBootApplication
public class RelaySimApplication {

public static void main(String[] args) {
SpringApplication.run(RelaySimApplication.class, args);
}

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
log.info("Adding cors mapping");
registry.addMapping("/**").allowedOrigins("*");
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -11,6 +12,7 @@
* This class is used to simulate turning a relay on and off, as well as getting the current state
*/
@RestController
@CrossOrigin
@Slf4j
public class RelayApiController {

Expand All @@ -19,8 +21,6 @@ public class RelayApiController {
@GetMapping("/status")
public String returnStatus(HttpServletResponse response) {
log.debug("Client requesting GET /status");
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Content-Type", "application/json");
return String.format("{\"relayIsOn\":%b}", relayIsOn.get());
}

Expand Down

0 comments on commit 2b1f56d

Please sign in to comment.