Skip to content

Commit d1ecfaf

Browse files
authored
#1912 Use top-level statements for Program in samples and update Getting Started docs (#2244)
* Improve Ocelot logger * Fix tests * Refactor Basic sample * Create Basic Configuration sample * Update gettingstarted.rst: Getting Started docs in Introduction * Update AdministrationApi sample * Update Eureka sample * Update GraphQL sample * Update Kubernetes sample * Update OpenTracing sample * Update ServiceDiscovery sample * Update ServiceFabric sample
1 parent 9304dd2 commit d1ecfaf

File tree

119 files changed

+1444
-1250
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1444
-1250
lines changed

Ocelot.Release.sln

+12-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{8FA0
5757
EndProject
5858
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ocelot.Samples.AdministrationApi", "samples\Administration\Ocelot.Samples.AdministrationApi.csproj", "{A7F0CAFA-AECB-43CA-BE89-5F5B728E7C22}"
5959
EndProject
60-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ocelot.Samples.Basic.ApiGateway", "samples\Basic\Ocelot.Samples.Basic.ApiGateway.csproj", "{F00C73F4-019D-490D-8194-CA1754D717FA}"
60+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ocelot.Samples.Basic", "samples\Basic\Ocelot.Samples.Basic.csproj", "{F00C73F4-019D-490D-8194-CA1754D717FA}"
61+
EndProject
62+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ocelot.Samples.Configuration", "samples\Configuration\Ocelot.Samples.Configuration.csproj", "{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}"
6163
EndProject
6264
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ocelot.Samples.Eureka.ApiGateway", "samples\Eureka\ApiGateway\Ocelot.Samples.Eureka.ApiGateway.csproj", "{FECB0C8B-5778-4441-B10E-0C815F5106D5}"
6365
EndProject
@@ -313,6 +315,14 @@ Global
313315
{1A00E87D-2B0B-4D61-A606-3D747C1E43F8}.Release|Any CPU.Build.0 = Release|Any CPU
314316
{1A00E87D-2B0B-4D61-A606-3D747C1E43F8}.Release|x64.ActiveCfg = Release|Any CPU
315317
{1A00E87D-2B0B-4D61-A606-3D747C1E43F8}.Release|x64.Build.0 = Release|Any CPU
318+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
319+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
320+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}.Debug|x64.ActiveCfg = Debug|Any CPU
321+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}.Debug|x64.Build.0 = Debug|Any CPU
322+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
323+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}.Release|Any CPU.Build.0 = Release|Any CPU
324+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}.Release|x64.ActiveCfg = Release|Any CPU
325+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA}.Release|x64.Build.0 = Release|Any CPU
316326
EndGlobalSection
317327
GlobalSection(SolutionProperties) = preSolution
318328
HideSolutionNode = FALSE
@@ -346,6 +356,7 @@ Global
346356
{D991C694-01F0-4F04-8135-5C133DC8E029} = {8FA0CBA0-0338-48EB-B37F-83CA5022237C}
347357
{AD09D124-7DD7-4C9E-9BCC-782B579B1786} = {8FA0CBA0-0338-48EB-B37F-83CA5022237C}
348358
{1A00E87D-2B0B-4D61-A606-3D747C1E43F8} = {8FA0CBA0-0338-48EB-B37F-83CA5022237C}
359+
{A33D6A6F-4FAB-4C22-A8AF-2B8C1DF122BA} = {8FA0CBA0-0338-48EB-B37F-83CA5022237C}
349360
EndGlobalSection
350361
GlobalSection(ExtensibilityGlobals) = postSolution
351362
SolutionGuid = {21476EFF-778A-4F97-8A56-D1AF1CEC0C48}

docs/features/administration.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,4 @@ The region is whatever you set against the **Region** field in the `FileCacheOpt
146146

147147
""""
148148

149-
.. [#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.
149+
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to the DI container. You can 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 the :doc:`../features/dependencyinjection` feature.

docs/features/headerstransformation.rst

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ If you want to return the `Butterfly APM <https://github.com/liuhaoyang/butterfl
4545
"AnyKey": "{TraceId}"
4646
}
4747
48+
.. _ht-find-and-replace:
49+
4850
Find and Replace
4951
----------------
5052

docs/features/qualityofservice.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ And finally, if you want to define your own set of exceptions to map, you can us
142142
143143
""""
144144

145-
.. [#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.
145+
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to the DI container. You can 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 the :doc:`../features/dependencyinjection` feature.
146146
.. [#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.
147147
.. [#f3] We upgraded `Polly`_ version from v7.x to v8.x! The :ref:`qos-extensibility` feature was requested in issue `1875`_ and delivered by PR `1914`_ as a part of version `23.2`_.
148148

docs/features/requestaggregation.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,4 @@ Gotchas
227227
""""
228228

229229
.. [#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>`_.
230-
.. [#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.
230+
.. [#f2] :ref:`di-the-addocelot-method` adds default ASP.NET services to the DI container. You can 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 the :doc:`../features/dependencyinjection` feature.

docs/features/servicediscovery.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ But you can leave this ``Type`` option for compatibility between both designs.
541541

542542
""""
543543

544-
.. [#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.
544+
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to the DI container. You can 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 the :doc:`../features/dependencyinjection` feature.
545545
.. [#f2] :ref:`sd-consul-configuration-key` feature was requested in issue `346`_ as a part of version `7.0.0`_.
546546
.. [#f3] Customization of :ref:`sd-consul-service-builder` was implemented as a part of bug `954`_ fixing and the feature was delivered in version `23.3`_.
547547

docs/features/tracing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ Ocelot will now send tracing information to Butterfly when this Route is called.
8888

8989
""""
9090

91-
.. [#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.
91+
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to the DI container. You can 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 the :doc:`../features/dependencyinjection` feature.

docs/introduction/gettingstarted.rst

+72-79
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
Getting Started
22
===============
33

4-
Ocelot is designed to work with ASP.NET and is currently on ``net6.0``, ``net7.0`` and ``net8.0`` frameworks.
4+
Ocelot is designed to work with `ASP.NET Core <https://learn.microsoft.com/en-us/aspnet/core/?view=aspnetcore-9.0>`_ and is currently on `.NET 8 <https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core#lifecycle>`_ `LTS <https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core#release-types>`_
5+
and `.NET 9 <https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core#lifecycle>`_ `STS <https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core#release-types>`_ frameworks.
56

6-
.NET 8.0
7-
--------
8-
9-
Install NuGet package
10-
^^^^^^^^^^^^^^^^^^^^^
7+
Install
8+
-------
119

1210
Install Ocelot and it's dependencies using `NuGet <https://www.nuget.org/>`_.
13-
You will need to create a `ASP.NET Core minimal API project <https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api>`_ and bring the package into it.
11+
You will need to create a `ASP.NET Core minimal API project <https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api>`_ with "ASP.NET Core Empty" template but without ``app.Map*`` methods, and bring the package into it.
1412
Then follow the startup below and :doc:`../features/configuration` sections to get up and running.
1513

1614
.. code-block:: powershell
@@ -20,108 +18,103 @@ Then follow the startup below and :doc:`../features/configuration` sections to g
2018
All versions can be found in the `NuGet Gallery | Ocelot <https://www.nuget.org/packages/Ocelot/>`_.
2119

2220
Configuration
23-
^^^^^^^^^^^^^
21+
-------------
2422

25-
The following is a very basic **ocelot.json**. It won't do anything but should get Ocelot starting.
23+
The following is a very basic `ocelot.json`_.
24+
It won't do anything but should get Ocelot starting.
2625

2726
.. code-block:: json
2827
2928
{
30-
"Routes": [],
31-
"GlobalConfiguration": {
32-
"BaseUrl": "https://api.mybusiness.com"
33-
}
29+
"Routes": [],
30+
"DynamicRoutes": [],
31+
"Aggregates": [],
32+
"GlobalConfiguration": {
33+
"BaseUrl": "https://api.mybusiness.com"
34+
}
3435
}
3536
3637
If you want some example that actually does something use the following:
3738

3839
.. code-block:: json
3940
4041
{
41-
"Routes": [
42-
{
43-
"DownstreamPathTemplate": "/todos/{id}",
44-
"DownstreamScheme": "https",
45-
"DownstreamHostAndPorts": [
46-
{
47-
"Host": "jsonplaceholder.typicode.com",
48-
"Port": 443
49-
}
50-
],
51-
"UpstreamPathTemplate": "/todos/{id}",
52-
"UpstreamHttpMethod": [ "Get" ]
53-
}
54-
],
55-
"GlobalConfiguration": {
56-
"BaseUrl": "https://localhost:5000"
42+
"Routes": [
43+
{
44+
"UpstreamHttpMethod": [ "Get" ],
45+
"UpstreamPathTemplate": "/ocelot/posts/{id}",
46+
"DownstreamPathTemplate": "/todos/{id}",
47+
"DownstreamScheme": "https",
48+
"DownstreamHostAndPorts": [
49+
{ "Host": "jsonplaceholder.typicode.com", "Port": 443 }
50+
]
5751
}
52+
],
53+
"GlobalConfiguration": {
54+
"BaseUrl": "https://localhost:7777"
55+
}
5856
}
5957
60-
The most important thing to note here is **BaseUrl** property.
61-
Ocelot needs to know the URL it is running under in order to do Header find & replace and for certain administration configurations.
58+
The most important thing to note here is ``BaseUrl`` property.
59+
Ocelot needs to know the URL it is running under in order to do Header :ref:`ht-find-and-replace` and for certain :doc:`../features/administration` configurations.
6260
When setting this URL it should be the external URL that clients will see Ocelot running on e.g.
63-
If you are running containers Ocelot might run on the URL ``http://123.12.1.1:6543`` but has something like **nginx** in front of it responding on ``https://api.mybusiness.com``.
64-
In this case the Ocelot **BaseUrl** should be ``https://api.mybusiness.com``.
61+
If you are running containers Ocelot might run on the URL ``http://123.12.1.2:6543`` but has something like `nginx <https://nginx.org/>`_ in front of it responding on ``https://api.mybusiness.com``.
62+
In this case the Ocelot ``BaseUrl`` should be ``https://api.mybusiness.com``.
6563

66-
If you are using containers and require Ocelot to respond to clients on ``http://123.12.1.1:6543`` then you can do this,
64+
If you are using containers and require Ocelot to respond to clients on ``http://123.12.1.2:6543`` then you can do this,
6765
however if you are deploying multiple Ocelot's you will probably want to pass this on the command line in some kind of script.
6866
Hopefully whatever scheduler you are using can pass the IP.
6967

7068
Program
71-
^^^^^^^
72-
73-
Then in your **Program.cs** you will want to have the following.
74-
75-
The main things to note are
69+
-------
7670

77-
* ``AddOcelot()`` adds Ocelot required and default services [#f1]_
78-
* ``UseOcelot().Wait()`` sets up all the Ocelot middlewares.
71+
Then in your `Program.cs <https://github.com/ThreeMammals/Ocelot/blob/main/samples/Basic/Program.cs>`_ (with `top-level statements <https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements>`_) you will want to have the following.
7972

8073
.. code-block:: csharp
8174
82-
using System.IO;
83-
using Microsoft.AspNetCore.Hosting;
84-
using Microsoft.Extensions.Configuration;
85-
using Microsoft.Extensions.Hosting;
8675
using Ocelot.DependencyInjection;
8776
using Ocelot.Middleware;
8877
89-
namespace OcelotBasic
78+
var builder = WebApplication.CreateBuilder(args);
79+
80+
// Ocelot Basic setup
81+
builder.Configuration
82+
.SetBasePath(builder.Environment.ContentRootPath)
83+
.AddOcelot(); // single ocelot.json file in read-only mode
84+
builder.Services
85+
.AddOcelot(builder.Configuration);
86+
87+
// Add your features
88+
if (builder.Environment.IsDevelopment())
9089
{
91-
public class Program
92-
{
93-
public static void Main(string[] args)
94-
{
95-
new WebHostBuilder()
96-
.UseKestrel()
97-
.UseContentRoot(Directory.GetCurrentDirectory())
98-
.ConfigureAppConfiguration((hostingContext, config) =>
99-
{
100-
config
101-
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
102-
.AddJsonFile("appsettings.json", true, true)
103-
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
104-
.AddJsonFile("ocelot.json")
105-
.AddEnvironmentVariables();
106-
})
107-
.ConfigureServices(s => {
108-
s.AddOcelot();
109-
})
110-
.ConfigureLogging((hostingContext, logging) =>
111-
{
112-
//add your logging
113-
})
114-
.UseIISIntegration()
115-
.Configure(app =>
116-
{
117-
app.UseOcelot().Wait();
118-
})
119-
.Build()
120-
.Run();
121-
}
122-
}
90+
builder.Logging.AddConsole();
12391
}
12492
93+
// Add middlewares aka app.Use*()
94+
var app = builder.Build();
95+
await app.UseOcelot();
96+
app.Run();
97+
98+
The main things to note are
99+
100+
* ``builder.Configuration.AddOcelot()`` adds single `ocelot.json`_ configuration file in read-only mode.
101+
* ``builder.Services.AddOcelot(builder.Configuration)`` adds Ocelot required and default services [#f1]_
102+
* ``app.UseOcelot()`` sets up all the Ocelot middlewares. Note, we have to await the threading result before calling ``app.Run()``
103+
* Do not add endpoint mappings (minimal API methods) such as ``app.MapGet()`` because the Ocelot pipeline is not compatible with them!
104+
105+
Samples
106+
-------
107+
108+
For beginners, we have prepared basic `samples <https://github.com/ThreeMammals/Ocelot/tree/main/samples>`_ to help Ocelot newbies clone, compile, and get it running.
109+
110+
* `Basic <https://github.com/ThreeMammals/Ocelot/tree/main/samples/Basic>`_ sample: It has a single configuration file, `ocelot.json`_.
111+
* `Basic Configuration <https://github.com/ThreeMammals/Ocelot/tree/main/samples/Configuration>`_ sample: It has multiple configuration files (``ocelot.*.json``) to be merged into ``ocelot.json`` and written back to disk.
112+
113+
After running in Visual Studio [#f2]_, you may use ``API.http`` files to send testing requests to the ``localhost`` Ocelot application instance.
114+
125115
""""
126116

127-
.. [#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.
117+
.. [#f1] :ref:`di-the-addocelot-method` adds default ASP.NET services to the DI container. You can 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 the :doc:`../features/dependencyinjection` feature.
118+
.. [#f2] All sample projects are organized as the `Ocelot.Samples.sln <https://github.com/ThreeMammals/Ocelot/blob/main/samples/Ocelot.Samples.sln>`_ file for Visual Studio 2022 IDE.
119+
120+
.. _ocelot.json: https://github.com/ThreeMammals/Ocelot/blob/main/samples/Basic/ocelot.json

samples/Administration/Ocelot.Samples.AdministrationApi.csproj

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
33
<TargetFramework>net9.0</TargetFramework>
4-
<ImplicitUsings>disable</ImplicitUsings>
5-
<Nullable>disable</Nullable>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<VersionPrefix>24.0.0</VersionPrefix>
7+
<AssemblyVersion>24.0.0</AssemblyVersion>
8+
<Copyright>ThreeMammals</Copyright>
69
</PropertyGroup>
710
<ItemGroup>
811
<ProjectReference Include="..\..\src\Ocelot.Administration\Ocelot.Administration.csproj" />

0 commit comments

Comments
 (0)