[TASK] move url of other microservices to config
This commit is contained in:
		
							parent
							
								
									aab5e492cc
								
							
						
					
					
						commit
						4df23ae623
					
				| 
						 | 
				
			
			@ -126,5 +126,6 @@ __pycache__
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
# go project
 | 
			
		||||
profile.cov
 | 
			
		||||
config.conf
 | 
			
		||||
cmd/stock/config.conf
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,10 +29,14 @@ func main() {
 | 
			
		|||
 | 
			
		||||
	// load config
 | 
			
		||||
	config = models.ReadConfigFile(configFile)
 | 
			
		||||
 | 
			
		||||
	// Config packages:
 | 
			
		||||
	web.GoodAvailablityTemplate = config.GoodAvailablityTemplate
 | 
			
		||||
	runtime.CacheConfig = config.CacheClean
 | 
			
		||||
	runtime.ProductURL = config.MicroserviceDependencies.Product
 | 
			
		||||
	runtime.PermissionURL = config.MicroserviceDependencies.Permission
 | 
			
		||||
 | 
			
		||||
	log.Log.Info("Starting rezension monolith")
 | 
			
		||||
	log.Log.Info("Starting stock microservice")
 | 
			
		||||
 | 
			
		||||
	err := database.Open(config.Database)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -58,6 +62,8 @@ func main() {
 | 
			
		|||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	log.Log.Info("Started stock microservice")
 | 
			
		||||
 | 
			
		||||
	// Wait for system signal
 | 
			
		||||
	sigs := make(chan os.Signal, 1)
 | 
			
		||||
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,3 +17,7 @@ after = "30m"
 | 
			
		|||
[cache_clean]
 | 
			
		||||
every = "5m"
 | 
			
		||||
after = "30m"
 | 
			
		||||
 | 
			
		||||
[microservice_dependencies]
 | 
			
		||||
product = "http://localhost:8080/api-test/product/%d/"
 | 
			
		||||
permission = "http://localhost:8080/api-test/session/%s/%d/"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,8 +43,7 @@ func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logr
 | 
			
		|||
		return -1, log
 | 
			
		||||
	}
 | 
			
		||||
	log = log.WithField("productid", id)
 | 
			
		||||
	product := runtime.Product{ID: id}
 | 
			
		||||
	ok, err := product.Exists()
 | 
			
		||||
	ok, err := runtime.ProductExists(id)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Warn("product could not verified on other microservice")
 | 
			
		||||
		http.Error(w, "product could not verified on other microservice", http.StatusGatewayTimeout)
 | 
			
		||||
| 
						 | 
				
			
			@ -56,9 +55,10 @@ func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logr
 | 
			
		|||
		return -1, log
 | 
			
		||||
	}
 | 
			
		||||
	var count float64
 | 
			
		||||
	(&models.Good{}).FilterAvailable(database.Read.Where("product_id = ?", product.ID)).Count(&count)
 | 
			
		||||
	(&models.Good{}).FilterAvailable(database.Read.Where("product_id = ?", id)).Count(&count)
 | 
			
		||||
	return int(count), log
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getGoodAvailablity(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	count, log := getGoodAvailablityCount(w, r)
 | 
			
		||||
	if count < 0 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -41,6 +41,8 @@ func TestGetGoodAvailable(t *testing.T) {
 | 
			
		|||
	now := time.Now()
 | 
			
		||||
	assertion, router := test.Init(t)
 | 
			
		||||
 | 
			
		||||
	runtime.ProductURL = "http://localhost:8080/api-test/product/%d/"
 | 
			
		||||
 | 
			
		||||
	BindAPI(router)
 | 
			
		||||
	session := test.NewSession(router)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,12 +11,16 @@ import (
 | 
			
		|||
 | 
			
		||||
//Config the config File of this daemon
 | 
			
		||||
type Config struct {
 | 
			
		||||
	WebserverBind           string            `toml:"webserver_bind"`
 | 
			
		||||
	Webroot                 string            `toml:"webroot"`
 | 
			
		||||
	Database                database.Config   `toml:"database"`
 | 
			
		||||
	GoodAvailablityTemplate string            `toml:"good_availablity_template"`
 | 
			
		||||
	GoodRelease             GoodReleaseConfig `toml:"good_release"`
 | 
			
		||||
	CacheClean              CacheWorkerConfig `toml:"cache_clean"`
 | 
			
		||||
	WebserverBind            string            `toml:"webserver_bind"`
 | 
			
		||||
	Webroot                  string            `toml:"webroot"`
 | 
			
		||||
	Database                 database.Config   `toml:"database"`
 | 
			
		||||
	GoodRelease              GoodReleaseConfig `toml:"good_release"`
 | 
			
		||||
	CacheClean               CacheWorkerConfig `toml:"cache_clean"`
 | 
			
		||||
	GoodAvailablityTemplate  string            `toml:"good_availablity_template"`
 | 
			
		||||
	MicroserviceDependencies struct {
 | 
			
		||||
		Product    string `toml:"product"`
 | 
			
		||||
		Permission string `toml:"permission"`
 | 
			
		||||
	} `toml:"microservice_dependencies"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type CacheWorkerConfig struct {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										112
									
								
								profile.cov
								
								
								
								
							
							
						
						
									
										112
									
								
								profile.cov
								
								
								
								
							| 
						 | 
				
			
			@ -1,112 +0,0 @@
 | 
			
		|||
mode: count
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:19.67,21.30 2 20
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:28.2,28.19 1 19
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:32.2,34.16 3 18
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:38.2,38.14 1 17
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:55.2,55.12 1 16
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:22.14,23.32 1 19
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:24.10,25.63 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:28.19,30.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:34.16,36.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:39.11,40.50 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:41.11,42.50 1 6
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:43.11,44.48 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:45.11,46.53 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:47.11,48.57 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:49.11,50.59 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/duration.go:51.10,52.63 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/good.go:28.54,30.2 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/good.go:32.36,36.2 3 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/good.go:37.30,39.2 1 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/good.go:40.44,41.30 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/good.go:46.2,46.35 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/good.go:41.30,45.3 3 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/good.go:49.13,51.2 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/config.go:33.42,36.16 3 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/config.go:40.2,40.53 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/config.go:44.2,44.15 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/config.go:36.16,38.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/models/config.go:40.53,42.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/log/main.go:14.13,18.2 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/log/main.go:21.42,23.18 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/log/main.go:26.2,30.4 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/log/main.go:23.18,25.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:25.33,29.16 4 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:32.2,37.36 6 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:51.2,52.8 2 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:29.16,31.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:37.36,40.17 3 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:43.3,47.60 5 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:40.17,42.4 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:48.3,50.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:55.14,58.36 3 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:61.2,61.12 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:58.36,60.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/database/main.go:64.30,66.2 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/worker/worker.go:11.59,18.2 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/worker/worker.go:20.26,22.6 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/worker/worker.go:22.6,23.10 1 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/worker/worker.go:24.19,25.11 1 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/worker/worker.go:26.17,28.10 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/worker/worker.go:32.26,34.2 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/io.go:10.56,11.56 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/io.go:15.2,16.8 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/io.go:11.56,14.3 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/io.go:20.53,22.16 2 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/io.go:26.2,27.13 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/io.go:22.16,25.3 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/permission.go:7.153,8.54 1 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/permission.go:8.54,10.17 2 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/permission.go:14.3,15.17 2 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/permission.go:19.3,19.9 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/permission.go:23.3,23.53 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/permission.go:10.17,13.4 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/permission.go:15.17,18.4 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/lib/http/permission.go:19.9,22.4 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:17.56,20.16 3 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:25.2,28.30 4 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:33.2,34.18 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:20.16,24.3 3 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:28.30,32.3 3 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:37.91,40.16 3 6
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:45.2,48.16 4 5
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:53.2,53.9 1 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:58.2,60.24 3 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:40.16,44.3 3 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:48.16,52.3 3 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:53.9,57.3 3 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:62.65,64.15 2 6
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:67.2,68.38 2 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:74.2,74.18 1 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:64.15,66.3 1 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:69.26,70.22 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good.go:71.10,72.34 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good_temp.go:13.38,15.2 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good_temp.go:17.56,19.2 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/good_temp.go:21.62,37.2 10 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/main.go:8.32,12.2 3 3
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/http/status.go:12.53,31.2 8 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/auth.go:27.81,29.39 2 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/auth.go:36.2,44.36 5 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/auth.go:29.39,31.36 2 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/auth.go:31.36,33.4 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/auth.go:49.13,51.2 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/auth.go:53.64,55.9 2 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/auth.go:62.2,62.50 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/auth.go:55.9,61.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/cache_worker.go:12.19,15.46 2 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/cache_worker.go:21.2,21.44 1 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/cache_worker.go:15.46,16.36 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/cache_worker.go:16.36,18.4 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/cache_worker.go:21.44,22.36 1 4
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/cache_worker.go:22.36,24.4 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/cache_worker.go:27.42,29.2 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/good_release.go:11.72,12.53 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/good_release.go:12.53,14.3 1 5
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/good_release.go:17.51,20.2 2 7
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/product_cache.go:21.13,23.2 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/product_cache.go:25.42,26.46 1 2
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/product_cache.go:30.2,33.16 4 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/product_cache.go:39.2,39.43 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/product_cache.go:26.46,28.3 1 1
 | 
			
		||||
github.com/genofire/hs_master-kss-monolith/runtime/product_cache.go:33.16,38.3 1 1
 | 
			
		||||
| 
						 | 
				
			
			@ -8,8 +8,7 @@ import (
 | 
			
		|||
	"github.com/genofire/hs_master-kss-monolith/lib/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TODO DRAFT for a rest request to a other microservice
 | 
			
		||||
const PermissionURL = "http://localhost:8080/api-test/session/%s/%d/"
 | 
			
		||||
var PermissionURL string
 | 
			
		||||
 | 
			
		||||
type Permission int
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,6 +10,7 @@ import (
 | 
			
		|||
func TestAuth(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
 | 
			
		||||
	PermissionURL = "http://localhost:8080/api-test/session/%s/%d/"
 | 
			
		||||
	router := http.FileServer(http.Dir("../webroot"))
 | 
			
		||||
	srv := &http.Server{
 | 
			
		||||
		Addr:    ":8080",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,40 @@
 | 
			
		|||
package runtime
 | 
			
		||||
 | 
			
		||||
type Product struct {
 | 
			
		||||
	ID int64
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/genofire/hs_master-kss-monolith/lib/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var ProductURL string
 | 
			
		||||
 | 
			
		||||
type boolMicroServiceCache struct {
 | 
			
		||||
	LastCheck time.Time
 | 
			
		||||
	Value     bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var productExistCache map[int64]boolMicroServiceCache
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	productExistCache = make(map[int64]boolMicroServiceCache)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ProductExists(id int64) (bool, error) {
 | 
			
		||||
	if cache, ok := productExistCache[id]; ok {
 | 
			
		||||
		return cache.Value, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	url := fmt.Sprintf(ProductURL, id)
 | 
			
		||||
	log.Log.WithField("url", url).Info("exists product?")
 | 
			
		||||
	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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,40 +0,0 @@
 | 
			
		|||
package runtime
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/genofire/hs_master-kss-monolith/lib/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TODO DRAFT for a rest request to a other microservice
 | 
			
		||||
const ProductURL = "http://localhost:8080/api-test/product/%d/"
 | 
			
		||||
 | 
			
		||||
type boolMicroServiceCache struct {
 | 
			
		||||
	LastCheck time.Time
 | 
			
		||||
	Value     bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var productExistCache map[int64]boolMicroServiceCache
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	productExistCache = make(map[int64]boolMicroServiceCache)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (p *Product) Exists() (bool, error) {
 | 
			
		||||
	if cache, ok := productExistCache[p.ID]; ok {
 | 
			
		||||
		return cache.Value, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	url := fmt.Sprintf(ProductURL, p.ID)
 | 
			
		||||
	log.Log.WithField("url", url).Info("exists product?")
 | 
			
		||||
	res, err := http.Get(url)
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		productExistCache[p.ID] = boolMicroServiceCache{
 | 
			
		||||
			LastCheck: time.Now(),
 | 
			
		||||
			Value:     (res.StatusCode == http.StatusOK),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return productExistCache[p.ID].Value, err
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -10,6 +10,7 @@ import (
 | 
			
		|||
func TestProductExists(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
 | 
			
		||||
	ProductURL = "http://localhost:8080/api-test/product/%d/"
 | 
			
		||||
	router := http.FileServer(http.Dir("../webroot"))
 | 
			
		||||
	srv := &http.Server{
 | 
			
		||||
		Addr:    ":8080",
 | 
			
		||||
| 
						 | 
				
			
			@ -17,16 +18,20 @@ func TestProductExists(t *testing.T) {
 | 
			
		|||
	}
 | 
			
		||||
	go srv.ListenAndServe()
 | 
			
		||||
 | 
			
		||||
	ok, err := (&Product{ID: 3}).Exists()
 | 
			
		||||
	ok, err := ProductExists(3)
 | 
			
		||||
	assert.True(ok)
 | 
			
		||||
	assert.NoError(err)
 | 
			
		||||
 | 
			
		||||
	// test cache
 | 
			
		||||
	ok, err = (&Product{ID: 3}).Exists()
 | 
			
		||||
	ok, err = ProductExists(3)
 | 
			
		||||
	assert.True(ok)
 | 
			
		||||
	assert.NoError(err)
 | 
			
		||||
 | 
			
		||||
	// WARNING: test cache after 5min skipped
 | 
			
		||||
	productExistCache = make(map[int64]boolMicroServiceCache)
 | 
			
		||||
	ProductURL = "http://localhost:8081/api-test/product/%d/"
 | 
			
		||||
 | 
			
		||||
	ok, err = ProductExists(3)
 | 
			
		||||
	assert.Error(err)
 | 
			
		||||
 | 
			
		||||
	srv.Close()
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in New Issue