예제 #1
0
        /// <summary>
        /// Méthode d'extesnsion pour valider la configuration KongRegister du fichier appsettings.json
        /// </summary>
        /// <param name="kongRegisterConfig"></param>
        public static void Validate(this KongRegisterConfig kongRegisterConfig)
        {
            if (kongRegisterConfig.KongApiUrl == null)
            {
                throw new ArgumentNullException(nameof(kongRegisterConfig.KongApiUrl));
            }

            if ((kongRegisterConfig.TargetHostDiscovery == null || !kongRegisterConfig.TargetHostDiscovery.Equals("dynamic", StringComparison.InvariantCultureIgnoreCase)) &&
                kongRegisterConfig.TargetHost == null)
            {
                throw new ArgumentNullException(nameof(kongRegisterConfig.TargetHost));
            }

            if ((kongRegisterConfig.TargetPortDiscovery == null || !kongRegisterConfig.TargetPortDiscovery.Equals("dynamic", StringComparison.InvariantCultureIgnoreCase)) &&
                kongRegisterConfig.TargetPort == null)
            {
                throw new ArgumentNullException(nameof(kongRegisterConfig.TargetPort));
            }

            if (kongRegisterConfig.UpstreamId == null)
            {
                throw new ArgumentNullException(nameof(kongRegisterConfig.UpstreamId));
            }

            if (kongRegisterConfig.TargetWeight != null &&
                (kongRegisterConfig.TargetWeight < 0 || kongRegisterConfig.TargetWeight > 1000))
            {
                throw new ArgumentOutOfRangeException(nameof(kongRegisterConfig.TargetWeight), "Weight value must be in range 0-1000");
            }
        }
예제 #2
0
        /// <summary>
        /// Service for registering the hosted application in Kong API Gateway.
        /// </summary>
        /// <param name="kongConfig"></param>
        /// <param name="logger"></param>
        /// <param name="server"></param>
        public KongRegisterBusiness(IOptions <KongRegisterConfig> kongConfig, ILogger <KongRegisterBackgroudService> logger, IServer server)
        {
            _logger     = logger ?? throw new ArgumentNullException(nameof(logger));
            _server     = server ?? throw new ArgumentNullException(nameof(server));
            _kongConfig = kongConfig.Value ?? throw new ArgumentNullException(nameof(kongConfig));
            try
            {
                _kongConfig.Validate();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error configuration file {ex.Message}");
            }

            _kongUrl = $"{_kongConfig.KongApiUrl}/upstreams/{_kongConfig.UpstreamId}/targets";
        }