Ocelot: New Feature - Merge configuration files

Created on 29 Mar 2018  路  12Comments  路  Source: ThreeMammals/Ocelot

Expected Behavior / New Feature

This may already be a feature but I see no mention of it in the documentation and it isn't working in my project.

It would be good to support multiple .json files, potentially with better naming conventions. I have an app with a number of services and routes, and it would be nice to separate them out into dedicated files rather then have one huge configuration.json.

For example:

  • AccountRoutes.json <- Would have all routes for the account controller/service
  • ComponentActions.json <- Would have all the routes for component actions controller/service

etc. etc.

Is this already possible? It seems that when I define multiple .AddJsonFile() it only picks up the routes in the last file it pinks up with the ReRoutes field.

enhancement help wanted medium effort

Most helpful comment

BTW, and sorry for the double post, it would be great to extend this feature into detecting file naming as the following:

ocelot.customers.{environment}.json
ocelot.users.{environment}.json
ocelot.products.{environment}.json

This way this would allow us to better isolate groups of APIs also for each environment.

All 12 comments

@AaronFixer thanks for your interest in the project!

This feature isn't supported yet I will but it on the backlog but if you would like to have a go yourself please feel free to submit a PR.

There are a couple of considerations here.

  1. Ocelot uses configuration.json as a database so a solution would need to take multiple files and merge them into configuration.json or have a second file that ocelot uses as a database (not sure I like this).

  2. Been thinking about this for a while completely change how configuration works. I was thinking of making it more like identity server where it is provided statically in c# / database.

  3. You could just do this yourself in ConfigureAppConfiguration with something like below and it would not need to be part of Ocelot.

.ConfigureAppConfiguration((hostingContext, config) =>
                {
                    merger.Merge();

                    config
                        .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                        .AddJsonFile("configuration.json");
                })

I think 1 and 3 are better options at the moment for me but that could change!

Anyway let me know if you want to give this a try and I will be of as much assistance as I can...otherwise I'm afraid you will have to wait a bit for the feature :(

For me this is definitely something I think I (and many others) could potentially benefit from with large projects wanting to use a Gateway!

I'll have to do this in my spare time, although I don't imagine it should take too long.

1 and 3 both work. 3 is something I could just use for the time being as a workaround. I'll see if I get a chance to work on a PR for 1. in the meantime.

Thanks for getting back to me!

Yep I agree this would be a useful feature! If you don't get round to it its on my list and I expect to do it in the next months or so.

@AaronFixer Ive merged a change that lets you have multiple configuration files..the docs for this are here. Can you let me know if you have any suggestions or this meets you use case before I release the change to NuGet?

@TomPallister Sorry for not doing this myself, I got caught up with the project I'm working on and didn't get a chance to look into this.

I'll definitely take a look at the changes and provide some feedback ASAP! Thank you.

@AaronFixer no worries, Ive just released this in 5.5.3, hopefully its OK, if not let me know and re-open issue!

Hello, people.
I think I'm hitting a bug with this feature.

I have ocelot.Development.json, ocelot.Testing.json and ocelot.global.json.
When I run the app on debug from VS, which should stick to the Development environment, it loads the Testing configuration in ocelot.json.

After taking a look at Ocelot's source code, I see this line:

string excludeConfigName = env?.EnvironmentName != null ? $"ocelot.{env.EnvironmentName}.json" : string.Empty;
...
var files = new DirectoryInfo(folder)
                .EnumerateFiles()
                .Where(fi => reg.IsMatch(fi.Name) && (fi.Name != excludeConfigName))
                .ToList();

Unless I'm getting this wrong, it's excluding my Development environment file and keeping only the Testing one, which I think goes against the purpose of this feature, isn't it? It should include Development as it's the current environment in which the app is running and exclude all other environment files so they don't get merged into a conflicting configuration.

BTW, and sorry for the double post, it would be great to extend this feature into detecting file naming as the following:

ocelot.customers.{environment}.json
ocelot.users.{environment}.json
ocelot.products.{environment}.json

This way this would allow us to better isolate groups of APIs also for each environment.

Can this method support IHostEnvironment Or How transform IHostEnvironment to IWebHostEnvironment ?

Can this method support IHostEnvironment Or How transform IHostEnvironment to IWebHostEnvironment ?

@zhutoutou use this

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddOcelot("Routes", hostingContext.HostingEnvironment);
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("ocelot.json",true)
.AddEnvironmentVariables();
})
.ConfigureServices(services =>
{
services.AddOcelot();
})
.Configure(app =>
{
//app.UseRouting();
//app.UseEndpoints(endpoints => {
// endpoints.MapControllers();
//});
app.UseOcelot().Wait();

ocelot

Hi,
Please how do you then make the program.cs file identify this json files

Was this page helpful?
0 / 5 - 0 ratings