Skip to content

Commit

Permalink
#1216 #1955 #1956 Overloaded AddOcelot method to support merging of…
Browse files Browse the repository at this point in the history
… configuration files to memory (#1227)

* Added new ConfigurationBuilderExtensions.AddOcelot
overload to support merging of configuration files to memory

* Fix warnings

* File-scoped namespaces

* Update configuration.rst

Use '^' for subsubsections.
Sections: https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#sections

* Review the code after merge and refactor

* Remove and sort usings

* Fix warnings

* Tried to fix

* A small but funny refactoring

* Update configuration.rst

* More refactoring: add overloaded extensions

* Review helpers of unit tests

* Add `FileUnitTest` and full rewrite of config tests

* Refactor `DiskFileConfigurationRepository` class.
Make unit tests parallel, independent, and thread safe

* some minor changes

* Review and update docs of the Configuration feature

* Review and update docs of the `Dependency Injection` feature

* Check references for `Dependency Injection` feature aka `ref:` and `doc:`

* Update configuration.rst

Replace "kind" with "type"

---------

Co-authored-by: Raman Maksimchuk <dotnet044@gmail.com>
Co-authored-by: Guillaume Gnaegi <58469901+ggnaegi@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 15, 2024
1 parent 5a3c55e commit 84bd6e4
Show file tree
Hide file tree
Showing 19 changed files with 949 additions and 616 deletions.
2 changes: 1 addition & 1 deletion docs/features/administration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ The region is whatever you set against the **Region** field in the `FileCacheOpt

""""

.. [#f1] The ``AddOcelot`` method adds default ASP.NET services to DI-container. You could call another more extended ``AddOcelotUsingBuilder`` method while configuring services to build and use custom builder via an ``IMvcCoreBuilder`` interface object. See more instructions in :doc:`../features/dependencyinjection`, "**The AddOcelotUsingBuilder method**" section.
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to DI container. You could call another extended :ref:`di-addocelotusingbuilder-method` while configuring services to develop your own :ref:`di-custom-builder`. See more instructions in the ":ref:`di-addocelotusingbuilder-method`" section of :doc:`../features/dependencyinjection` feature.
162 changes: 121 additions & 41 deletions docs/features/configuration.rst

Large diffs are not rendered by default.

210 changes: 170 additions & 40 deletions docs/features/dependencyinjection.rst

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/features/qualityofservice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ If someone needs this to be configurable, open an issue. [#f2]_

""""

.. [#f1] The ``AddOcelot`` method adds default ASP.NET services to DI-container. You could call another more extended ``AddOcelotUsingBuilder`` method while configuring services to build and use custom builder via an ``IMvcCoreBuilder`` interface object. See more instructions in :doc:`../features/dependencyinjection`, "**The AddOcelotUsingBuilder method**" section.
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to DI container. You could call another extended :ref:`di-addocelotusingbuilder-method` while configuring services to develop your own :ref:`di-custom-builder`. See more instructions in the ":ref:`di-addocelotusingbuilder-method`" section of :doc:`../features/dependencyinjection` feature.
.. [#f2] If something doesn't work or you get stuck, please review current `QoS issues <https://github.com/search?q=repo%3AThreeMammals%2FOcelot+QoS&type=issues>`_ filtering by |QoS_label| label.
.. |QoS_label| image:: https://img.shields.io/badge/-QoS-D3ADAF.svg
Expand Down
2 changes: 1 addition & 1 deletion docs/features/requestaggregation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,4 @@ Aggregation only supports the ``GET`` HTTP verb.
""""

.. [#f1] This feature was requested as part of `issue 79 <https://github.com/ThreeMammals/Ocelot/issues/79>`_ and further improvements were made as part of `issue 298 <https://github.com/ThreeMammals/Ocelot/issues/298>`_. A significant refactoring and revision of the `Multiplexer <https://github.com/ThreeMammals/Ocelot/tree/develop/src/Ocelot/Multiplexer>`_ design was carried out on March 4, 2024 in version `23.1 <https://github.com/ThreeMammals/Ocelot/releases/tag/23.1.0>`_, see PRs `1826 <https://github.com/ThreeMammals/Ocelot/pull/1826>`_ and `1462 <https://github.com/ThreeMammals/Ocelot/pull/1462>`_.
.. [#f2] The ``AddOcelot`` method adds default ASP.NET services to DI-container. You could call another more extended ``AddOcelotUsingBuilder`` method while configuring services to build and use custom builder via an ``IMvcCoreBuilder`` interface object. See more instructions in :doc:`../features/dependencyinjection`, "**The AddOcelotUsingBuilder method**" section.
.. [#f2] :ref:`di-the-addocelot-method` adds default ASP.NET services to DI container. You could call another extended :ref:`di-addocelotusingbuilder-method` while configuring services to develop your own :ref:`di-custom-builder`. See more instructions in the ":ref:`di-addocelotusingbuilder-method`" section of :doc:`../features/dependencyinjection` feature.
2 changes: 1 addition & 1 deletion docs/features/tracing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ Ocelot will now send tracing information to Butterfly when this Route is called.

""""

.. [#f1] The ``AddOcelot`` method adds default ASP.NET services to DI-container. You could call another more extended ``AddOcelotUsingBuilder`` method while configuring services to build and use custom builder via an ``IMvcCoreBuilder`` interface object. See more instructions in :doc:`../features/dependencyinjection`, "**The AddOcelotUsingBuilder method**" section.
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to DI container. You could call another extended :ref:`di-addocelotusingbuilder-method` while configuring services to develop your own :ref:`di-custom-builder`. See more instructions in the ":ref:`di-addocelotusingbuilder-method`" section of :doc:`../features/dependencyinjection` feature.
8 changes: 6 additions & 2 deletions docs/introduction/gettingstarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ Program
^^^^^^^

Then in your **Program.cs** you will want to have the following.
The main things to note are ``AddOcelot()`` [#f1]_ (adds Ocelot default services), ``UseOcelot().Wait()`` (sets up all the Ocelot middleware).

The main things to note are

* ``AddOcelot()`` adds Ocelot required and default services [#f1]_
* ``UseOcelot().Wait()`` sets up all the Ocelot middlewares.

.. code-block:: csharp
Expand Down Expand Up @@ -120,4 +124,4 @@ The main things to note are ``AddOcelot()`` [#f1]_ (adds Ocelot default services
""""

.. [#f1] The ``AddOcelot`` method adds default ASP.NET services to DI-container. You could call another more extended ``AddOcelotUsingBuilder`` method while configuring services to build and use custom builder via an ``IMvcCoreBuilder`` interface object. See more instructions in :doc:`../features/dependencyinjection`, "**The AddOcelotUsingBuilder method**" section.
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to DI container. You could call another extended :ref:`di-addocelotusingbuilder-method` while configuring services to develop your own :ref:`di-custom-builder`. See more instructions in the ":ref:`di-addocelotusingbuilder-method`" section of :doc:`../features/dependencyinjection` feature.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,42 @@
using Newtonsoft.Json;
using Ocelot.Configuration.ChangeTracking;
using Ocelot.Configuration.File;
using Ocelot.DependencyInjection;
using Ocelot.Responses;
using FileSys = System.IO.File;

namespace Ocelot.Configuration.Repository
{
public class DiskFileConfigurationRepository : IFileConfigurationRepository
{
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IOcelotConfigurationChangeTokenSource _changeTokenSource;
private readonly string _environmentFilePath;
private readonly string _ocelotFilePath;
private static readonly object _lock = new();
private const string ConfigurationFileName = "ocelot";
private FileInfo _ocelotFile;
private FileInfo _environmentFile;
private readonly object _lock = new();

public DiskFileConfigurationRepository(IWebHostEnvironment hostingEnvironment, IOcelotConfigurationChangeTokenSource changeTokenSource)
{
_hostingEnvironment = hostingEnvironment;
_changeTokenSource = changeTokenSource;
_environmentFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json";
Initialize(AppContext.BaseDirectory);
}

public DiskFileConfigurationRepository(IWebHostEnvironment hostingEnvironment, IOcelotConfigurationChangeTokenSource changeTokenSource, string folder)
{
_hostingEnvironment = hostingEnvironment;
_changeTokenSource = changeTokenSource;
Initialize(folder);
}

_ocelotFilePath = $"{AppContext.BaseDirectory}{ConfigurationFileName}.json";
private void Initialize(string folder)
{
folder ??= AppContext.BaseDirectory;
_ocelotFile = new FileInfo(Path.Combine(folder, ConfigurationBuilderExtensions.PrimaryConfigFile));
var envFile = !string.IsNullOrEmpty(_hostingEnvironment.EnvironmentName)
? string.Format(ConfigurationBuilderExtensions.EnvironmentConfigFile, _hostingEnvironment.EnvironmentName)
: ConfigurationBuilderExtensions.PrimaryConfigFile;
_environmentFile = new FileInfo(Path.Combine(folder, envFile));
}

public Task<Response<FileConfiguration>> Get()
Expand All @@ -28,7 +46,7 @@ public Task<Response<FileConfiguration>> Get()

lock (_lock)
{
jsonConfiguration = System.IO.File.ReadAllText(_environmentFilePath);
jsonConfiguration = FileSys.ReadAllText(_environmentFile.FullName);
}

var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(jsonConfiguration);
Expand All @@ -42,19 +60,19 @@ public Task<Response> Set(FileConfiguration fileConfiguration)

lock (_lock)
{
if (System.IO.File.Exists(_environmentFilePath))
if (_environmentFile.Exists)
{
System.IO.File.Delete(_environmentFilePath);
_environmentFile.Delete();
}

System.IO.File.WriteAllText(_environmentFilePath, jsonConfiguration);
FileSys.WriteAllText(_environmentFile.FullName, jsonConfiguration);

if (System.IO.File.Exists(_ocelotFilePath))
if (_ocelotFile.Exists)
{
System.IO.File.Delete(_ocelotFilePath);
_ocelotFile.Delete();
}

System.IO.File.WriteAllText(_ocelotFilePath, jsonConfiguration);
FileSys.WriteAllText(_ocelotFile.FullName, jsonConfiguration);
}

_changeTokenSource.Activate();
Expand Down
Loading

0 comments on commit 84bd6e4

Please sign in to comment.