///////////////////////////////////////////////////////////////////////////////

    Copyright (c) 2020 Oracle and/or its affiliates.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

///////////////////////////////////////////////////////////////////////////////

= Using CORS in Helidon MP Built-in Services
:toc:
:toc-placement: preamble
:h1Prefix: MP
:pagename: cors-built-in-service-support
:description: Helidon MP CORS Support in Built-in Services
:keywords: helidon, java, cors, mp, services
:helidon-tag: https://github.com/oracle/helidon/tree/{helidon-version}
:quickstart-example: {helidon-tag}/examples/quickstarts/helidon-quickstart-mp
:javadoc-base-url-api: {javadoc-base-url}io.helidon.microprofile.cors/io/helidon/microprofile/cors
:javadoc-base-url-webserver: {javadoc-base-url}io.helidon.webserver/io/helidon/webserver
:cors-se-intro: 01_introduction.adoc
:cors-config-table-src: {cors-se-intro}
:health-page: mp/health/01_introduction.adoc
:metrics-page: mp/metrics/01_introduction.adoc
:openapi-page: mp/openapi/01_openapi.adoc
:cors-config-table-exclude-methods:
:cors-services-is-se!:
:helidon-variant: MP
:cors-se-include: ../../se/cors
:se-built-in-services-page-include: {cors-se-include}/04_support-in-builtin-services.adoc
:cors-se-using-page: 02_using-the-api.adoc
:cors-dependency-src: ../../se/cors/{cors-se-using-page}
:cors-se-api-doc: ../../se/cors/02_using-the-api.adoc
// Following is resolved in an SE file, so the relative path must lead to the MP file.
:cors-mp-using-cors-page-inc: ../../mp/cors/02_using-cors.adoc
:mp-pages-ref-prefix: mp/cors
:mp-cors-config-ref: {mp-pages-ref-prefix}/03_configuration-with-cors-mp.adoc
:actual-cors-dependency-src: {cors-mp-using-cors-page-inc}
:se-cors-home-inc: ../../se/cors
:se-cors-dependency-page: 02_using-the-api.adoc
:dependency-inc: {se-cors-home-inc}/{se-cors-dependency-page}
:common-page-prefix-inc: ../../shared/cors/common_shared.adoc

Several built-in Helidon services -- health, metrics, and OpenAPI -- have integrated CORS support.
You can include these services in your application and control their CORS behavior.

include::{common-page-prefix-inc}[tag=understanding-cors-support-in-services]

include::{common-page-prefix-inc}[tag=builtin-getting-started]

include::{common-page-prefix-inc}[tags=configuring-cors-for-builtin-services;!se-config-example;!se-code-changes-for-builtin-services-config]

The following example restricts sharing of

* the `/health` resource, provided by the health built-in service, to only the origin `\http://there.com`, and
* the `/metrics` resource, provided by the metrics built-in service, to only the origin `\http://foo.com`.

[source,hocon]
----
...
health:
  cors:
    allow-origins: [http://there.com]
metrics:
  cors:
    allow-origins: [http://foo.com]
...
----

include::{common-page-prefix-inc}[tag=accessing-shared-resources-intro]
[source,bash]
----
mvn package
java -jar target/helidon-quickstart-mp.jar
...
2020.05.12 05:44:08 INFO io.helidon.microprofile.server.ServerCdiExtension Thread[main,5,main]: Server started on http://localhost:8080 (and all other host addresses) in 5280 milliseconds (since JVM startup).
...
----

include::{common-page-prefix-inc}[tag=accessing-shared-resources-main]
