| 1 |
[[embedding]] |
| 2 |
= Embedding and Integrating Cockpit |
| 3 |
|
| 4 |
Cockpit can be embedded in other web applications either as a whole or |
| 5 |
specific Cockpit components can be integrated. Due to frame security |
| 6 |
policy restrictions, this only works if Cockpit and the web application |
| 7 |
have the _same origin_; this is commonly achieved by running both from a |
| 8 |
common reverse proxy. |
| 9 |
|
| 10 |
[[embedding-full]] |
| 11 |
== Embedding the Cockpit Interface |
| 12 |
|
| 13 |
Cockpit can be embedded into a larger web page as a frame. To embed the |
| 14 |
entire Cockpit Window use the URI: `+https://server.example.com:9090/+` |
| 15 |
|
| 16 |
[source,html] |
| 17 |
---- |
| 18 |
<html> |
| 19 |
<head> |
| 20 |
<title>Embedded Cockpit</title> |
| 21 |
</head> |
| 22 |
<body> |
| 23 |
This is Cockpit. |
| 24 |
<br/> |
| 25 |
<iframe width="800px" height="600px" |
| 26 |
src="https://server.example.com:9090/"/> |
| 27 |
</body> |
| 28 |
</html> |
| 29 |
---- |
| 30 |
|
| 31 |
[[embedding-components]] |
| 32 |
== Integrating Cockpit Components into Web Applications |
| 33 |
|
| 34 |
Instead of embedding the entirety of Cockpit, you can integrate specific |
| 35 |
components. Only those components explicitly documented as API should be |
| 36 |
integrated. Other components can and will change regularly. |
| 37 |
|
| 38 |
The component will load from the server in question and a WebSocket |
| 39 |
connection will be established with the server to relay the component's |
| 40 |
message stream. |
| 41 |
|
| 42 |
Cockpit components are HTML files contained in xref:packages.adoc[packages]. |
| 43 |
These can be placed in an iframe or web browser window. Each documented |
| 44 |
and stable component has a well-known URL and these are documented in |
| 45 |
the xref:development.adoc[API reference]. Each component URL begins with the |
| 46 |
string `+/cockpit/@localhost/+` followed a package name, and then the |
| 47 |
component itself. |
| 48 |
|
| 49 |
For example the xref:api-terminal-html.adoc[terminal.html] in the |
| 50 |
xref:api-system.adoc[system] package, has this URL: |
| 51 |
`+/cockpit/@localhost/system/terminal.html+` |
| 52 |
|
| 53 |
[source,html] |
| 54 |
---- |
| 55 |
<html> |
| 56 |
<head> |
| 57 |
<title>Embedded Terminal</title> |
| 58 |
</head> |
| 59 |
<body> |
| 60 |
This is a terminal. |
| 61 |
<br/> |
| 62 |
<iframe width="800px" height="600px" |
| 63 |
src="https://server.example.com:9090/cockpit/@localhost/system/terminal.html"/> |
| 64 |
</body> |
| 65 |
</html> |
| 66 |
---- |
| 67 |
|
| 68 |
[[embedding-deep]] |
| 69 |
== Deep Integration |
| 70 |
|
| 71 |
Most often link:#embedding-components[simple integration] will be used |
| 72 |
to bring Cockpit components into web applications. However it is also |
| 73 |
possible to do deep integration for embedders who wish to perform |
| 74 |
non-standard authentication with the server, and relay the component's |
| 75 |
message stream to the server themselves. |
| 76 |
|
| 77 |
[WARNING] |
| 78 |
==== |
| 79 |
Deep integration capability is in heavy flux and is not yet documented. |
| 80 |
==== |
| 81 |
|
| 82 |
[[embedding-cors]] |
| 83 |
== Pinging Cockpit |
| 84 |
|
| 85 |
When embedding Cockpit or integrating Cockpit components, it may be |
| 86 |
necessary to check whether Cockpit is available on a server before |
| 87 |
proceeding. |
| 88 |
|
| 89 |
To do this perform a `+/ping+` request to Cockpit. This is a simple HTTP |
| 90 |
GET request. It returns the following: |
| 91 |
|
| 92 |
.... |
| 93 |
GET: https://server.example.com:9090/ping |
| 94 |
200 OK: { "service": "cockpit" } |
| 95 |
.... |
| 96 |
|
| 97 |
The `+/ping+` request allows |
| 98 |
https://en.wikipedia.org/wiki/Cross-origin_resource_sharing[Cross Origin |
| 99 |
Resource Sharing] headers and as such can be performed from Javascript |
| 100 |
code with any origin. The request can also be made via plain HTTP |
| 101 |
without SSL. It is by design that no further information is present in |
| 102 |
the response. |
| 103 |
|
| 104 |
A complete example of using `+/ping+` is available in the Cockpit |
| 105 |
sources in the `+/examples/ping-server/+` directory. |