Skip to content

Instantly share code, notes, and snippets.

@bdemers
Created August 10, 2017 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdemers/ef6996130249a0b3993b725ecfd712f2 to your computer and use it in GitHub Desktop.
Save bdemers/ef6996130249a0b3993b725ecfd712f2 to your computer and use it in GitHub Desktop.
package com.example.edgeservice
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
import org.springframework.cloud.netflix.feign.EnableFeignClients
import org.springframework.cloud.netflix.feign.FeignClient
import org.springframework.cloud.netflix.zuul.EnableZuulProxy
import org.springframework.context.annotation.Bean
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.UrlBasedCorsConfigurationSource
import org.springframework.web.filter.CorsFilter
import org.springframework.core.Ordered
@EnableCircuitBreaker
@EnableFeignClients
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
class EdgeServiceApplication {
@Bean
open fun simpleCorsFilter(): FilterRegistrationBean {
val source = UrlBasedCorsConfigurationSource()
val config = CorsConfiguration()
config.allowCredentials = true
config.addAllowedOrigin("*")
config.addAllowedHeader("*")
config.addAllowedMethod("*")
source.registerCorsConfiguration("/**", config)
val bean = FilterRegistrationBean(CorsFilter(source))
bean.order = Ordered.HIGHEST_PRECEDENCE
return bean
}
}
fun main(args: Array<String>) {
SpringApplication.run(EdgeServiceApplication::class.java, *args)
}
@FeignClient("beer-catalog-service")
interface BeerClient {
@GetMapping("/beers")
fun read(): Array<Beer>
}
@RestController
class BeerApiAdapterRestController(val beerClient: BeerClient) {
fun fallback(): Collection <Beer> = arrayListOf()
@HystrixCommand(fallbackMethod = "fallback")
@GetMapping("/good-beers")
// @CrossOrigin(origins = arrayOf("http://localhost:4200"))
fun goodBeers(): Collection<Beer> =
beerClient
.read()
.filter { !arrayOf("Coors Light", "PBR", "Budweiser", "Heineken").contains(it.name) }
}
class Beer(var id: Long? = null, var name: String? = null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment