| 
									
										
										
										
											2017-05-03 08:02:29 +02:00
										 |  |  | // Package with supporting functionality to run the microservice
 | 
					
						
							| 
									
										
										
										
											2017-04-07 11:56:28 +02:00
										 |  |  | package runtime | 
					
						
							| 
									
										
										
										
											2017-04-03 14:59:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/genofire/hs_master-kss-monolith/lib/log" | 
					
						
							| 
									
										
										
										
											2017-05-12 11:50:51 +02:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 08:02:29 +02:00
										 |  |  | // URL to the microservice which manages the products (product catalogue)
 | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | var ProductURL string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 08:02:29 +02:00
										 |  |  | // Struct tht holds the information on the microservice cache
 | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | type boolMicroServiceCache struct { | 
					
						
							|  |  |  | 	LastCheck time.Time | 
					
						
							|  |  |  | 	Value     bool | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 08:02:29 +02:00
										 |  |  | // Cache for existing products
 | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | var productExistCache map[int64]boolMicroServiceCache | 
					
						
							| 
									
										
										
										
											2017-05-12 11:50:51 +02:00
										 |  |  | var productMutex sync.Mutex | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 08:02:29 +02:00
										 |  |  | // Function to initialize the cache for existing products
 | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | func init() { | 
					
						
							|  |  |  | 	productExistCache = make(map[int64]boolMicroServiceCache) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 08:02:29 +02:00
										 |  |  | // Function to check on the other microservice (product catalogue) if the product exists
 | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | func ProductExists(id int64) (bool, error) { | 
					
						
							| 
									
										
										
										
											2017-05-12 11:50:51 +02:00
										 |  |  | 	productMutex.Lock() | 
					
						
							|  |  |  | 	defer productMutex.Unlock() | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | 	if cache, ok := productExistCache[id]; ok { | 
					
						
							|  |  |  | 		return cache.Value, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	url := fmt.Sprintf(ProductURL, id) | 
					
						
							| 
									
										
										
										
											2017-05-05 10:44:19 +02:00
										 |  |  | 	log.Log.WithField("url", url).Info("does the product exist?") | 
					
						
							| 
									
										
										
										
											2017-04-07 13:13:37 +02:00
										 |  |  | 	res, err := http.Get(url) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return false, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	productExistCache[id] = boolMicroServiceCache{ | 
					
						
							|  |  |  | 		LastCheck: time.Now(), | 
					
						
							|  |  |  | 		Value:     (res.StatusCode == http.StatusOK), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return productExistCache[id].Value, nil | 
					
						
							| 
									
										
										
										
											2017-04-03 14:59:43 +02:00
										 |  |  | } |