Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Configurable CDN for Default Pode Static Pages #1522

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/PetStore/server.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
OpenApi = @{
DefaultDefinitionTag = 'v3.0.3'
}
Static = @{
ContentDeliveryNetwork = 'https://cdn.jsdelivr.net/npm'
}
}
}
7 changes: 4 additions & 3 deletions examples/server.psd1
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
@{
Web = @{
Static = @{
Defaults = @(
Defaults = @(
'index.html',
'default.html'
)
Cache = @{
Cache = @{
Enable = $true
MaxAge = 15
Include = @(
'*.jpg'
)
}
ContentDeliveryNetwork = 'https://cdn.jsdelivr.net/npm'
}
ErrorPages = @{
ShowExceptions = $true
Expand All @@ -35,7 +36,7 @@
}
Logging = @{
Masking = @{
Patterns = @(
Patterns = @(
'(?<keep_before>Password=)\w+',
'(?<keep_before>AppleWebKit\/)\d+\.\d+(?(<keep_after)\s+\(KHTML)'
)
Expand Down
6 changes: 3 additions & 3 deletions src/Misc/default-doc-bookmarks.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

<head>
<title>OpenAPI Documentation Bookmarks</title>
<link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.8.0/styles/monokai-sublime.min.css">
<script src="https://unpkg.com/@highlightjs/cdn-assets@11.8.0/highlight.min.js"></script>
<link rel="stylesheet" href="$($data.ContentDeliveryNetwork)/@highlightjs/cdn-assets@11.11.1/styles/monokai-sublime.min.css">
<script src="$($data.ContentDeliveryNetwork)/@highlightjs/cdn-assets@11.11.1/highlight.min.js"></script>

<!-- and it's easy to individually load additional languages -->
<script src="https://unpkg.com/@highlightjs/cdn-assets@11.8.0/languages/go.min.js"></script>
<script src="$($data.ContentDeliveryNetwork)/@highlightjs/cdn-assets@11.11.1/languages/go.min.js"></script>

<script>hljs.highlightAll();</script>
<style>
Expand Down
6 changes: 3 additions & 3 deletions src/Misc/default-explorer.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<title>$($data.Title)</title>
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/cdn-[email protected]/build/styles/default.min.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
<link rel="stylesheet" href="$($data.ContentDeliveryNetwork)/@highlightjs/cdn-[email protected]/styles/default.min.css">
<link href="$($data.ContentDeliveryNetwork)/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css">

<script type="module" src="https://unpkg.com/openapi-explorer@0/dist/browser/openapi-explorer.min.js"></script>
<script type="module" src="$($data.ContentDeliveryNetwork)/openapi-explorer@0/dist/browser/openapi-explorer.min.js"></script>
<!-- Or use a local deployed copy -->
<!-- <script type="module" src="node_modules/openapi-explorer/dist/openapi-explorer.min.js"></script> -->
<style>
Expand Down
2 changes: 1 addition & 1 deletion src/Misc/default-rapidoc.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<title>$($data.Title)</title>
<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 characters -->
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
<script type="module" src="$($data.ContentDeliveryNetwork)/rapidoc/dist/rapidoc-min.js"></script>

</head>

Expand Down
2 changes: 1 addition & 1 deletion src/Misc/default-redoc.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<body>
<redoc spec-url='$($data.OpenApi)'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js" theme="$($data.Theme)"></script>
<script src="$($data.ContentDeliveryNetwork)/redoc@next/bundles/redoc.standalone.js" theme="$($data.Theme)"></script>
</body>

</html>
4 changes: 2 additions & 2 deletions src/Misc/default-stoplight.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>$($data.Title)</title>

<script src="https://unpkg.com/@stoplight/elements/web-components.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css">
<script src="$($data.ContentDeliveryNetwork)/@stoplight/elements/web-components.min.js"></script>
<link rel="stylesheet" href="$($data.ContentDeliveryNetwork)/@stoplight/elements/styles.min.css">
</head>

<body>
Expand Down
10 changes: 5 additions & 5 deletions src/Misc/default-swagger-editor.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
}

</style>
<link href="$($data.SwaggerEditorDist)/swagger-editor.css" rel="stylesheet">
<link rel="icon" type="image/png" href="$($data.SwaggerEditorDist)/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="$($data.SwaggerEditorDist)/favicon-16x16.png" sizes="16x16" />
<link href="$($data.ContentDeliveryNetwork)/swagger-editor-dist@4/swagger-editor.css" rel="stylesheet">
<link rel="icon" type="image/png" href="$($data.ContentDeliveryNetwork)/swagger-editor-dist@4/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="$($data.ContentDeliveryNetwork)/swagger-editor-dist@4/favicon-16x16.png" sizes="16x16" />
</head>

<body>
<div id="swagger-editor"></div>
<script src="$($data.SwaggerEditorDist)/swagger-editor-bundle.js" crossorigin> </script>
<script src="$($data.SwaggerEditorDist)/swagger-editor-standalone-preset.js"> </script>
<script src="$($data.ContentDeliveryNetwork)/swagger-editor-dist@4/swagger-editor-bundle.js" crossorigin> </script>
<script src="$($data.ContentDeliveryNetwork)/swagger-editor-dist@4/swagger-editor-standalone-preset.js"> </script>
<script>
window.onload = function() {
// Build a system
Expand Down
4 changes: 2 additions & 2 deletions src/Misc/default-swagger.html.pode
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<title>$($data.Title)</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css">
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js" crossorigin></script>
<link rel="stylesheet" type="text/css" href="$($data.ContentDeliveryNetwork)/swagger-ui-dist@5/swagger-ui.css">
<script src="$($data.ContentDeliveryNetwork)/swagger-ui-dist@5/swagger-ui-bundle.js" crossorigin></script>
<script type="text/javascript">
window.onload = function() {
SwaggerUIBundle({
Expand Down
45 changes: 36 additions & 9 deletions src/Private/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,29 @@ function Set-PodeServerConfiguration {

}

<#
.SYNOPSIS
Configures the web server settings for a Pode-based application.

.DESCRIPTION
This function initializes and configures various web-related settings for a Pode server,
including static content handling, caching, error pages, content types, transfer encoding,
compression, and OpenAPI settings.

.PARAMETER Configuration
A hashtable containing the configuration settings for the web server.

.PARAMETER Context
The Pode server context that will be modified with the new configuration settings.

.NOTES
- The ContentDeliveryNetwork (CDN) setting supports the following values:
* 'https://unpkg.com'
* 'https://cdn.jsdelivr.net/npm'
* 'https://cdnjs.cloudflare.com/ajax/libs' (requires some URL changes to work correctly)
- Protect-PodeValue is used to handle secure default values.
- Convert-PodePathPatternToRegex ensures path patterns are correctly formatted.
#>
function Set-PodeWebConfiguration {
param(
[Parameter()]
Expand All @@ -1093,18 +1116,22 @@ function Set-PodeWebConfiguration {
$Context
)

# setup the main web config
# Setup the main web configuration settings
$Context.Server.Web = @{
Static = @{
Defaults = $Configuration.Static.Defaults
RedirectToDefault = [bool]$Configuration.Static.RedirectToDefault
Cache = @{
Defaults = $Configuration.Static.Defaults
RedirectToDefault = [bool]$Configuration.Static.RedirectToDefault
Cache = @{
Enabled = [bool]$Configuration.Static.Cache.Enable
MaxAge = [int](Protect-PodeValue -Value $Configuration.Static.Cache.MaxAge -Default 3600)
Include = (Convert-PodePathPatternsToRegex -Paths @($Configuration.Static.Cache.Include) -NotSlashes)
Exclude = (Convert-PodePathPatternsToRegex -Paths @($Configuration.Static.Cache.Exclude) -NotSlashes)
}
ValidateLast = [bool]$Configuration.Static.ValidateLast
ValidateLast = [bool]$Configuration.Static.ValidateLast

# Content Delivery Network (CDN) settings
# Possible values: 'https://unpkg.com', 'https://cdn.jsdelivr.net/npm', 'https://cdnjs.cloudflare.com/ajax/libs' (requires some URL changes)
ContentDeliveryNetwork = (Protect-PodeValue -Value $Configuration.Static.ContentDeliveryNetwork -Default 'https://cdn.jsdelivr.net/npm')
}
ErrorPages = @{
ShowExceptions = [bool]$Configuration.ErrorPages.ShowExceptions
Expand All @@ -1128,32 +1155,32 @@ function Set-PodeWebConfiguration {
}
}

# Enable internal Pode YAML processing for OpenAPI if configured
if ($Configuration.OpenApi -and $Configuration.OpenApi.ContainsKey('UsePodeYamlInternal')) {
$Context.Server.Web.OpenApi.UsePodeYamlInternal = $Configuration.OpenApi.UsePodeYamlInternal
}

# setup content type route patterns for forced content types
# Configure forced content types for specific route patterns
$Configuration.ContentType.Routes.Keys | Where-Object { ![string]::IsNullOrWhiteSpace($_) } | ForEach-Object {
$_type = $Configuration.ContentType.Routes[$_]
$_pattern = (Convert-PodePathPatternToRegex -Path $_ -NotSlashes)
$Context.Server.Web.ContentType.Routes[$_pattern] = $_type
}

# setup transfer encoding route patterns for forced transfer encodings
# Configure forced transfer encoding for specific route patterns
$Configuration.TransferEncoding.Routes.Keys | Where-Object { ![string]::IsNullOrWhiteSpace($_) } | ForEach-Object {
$_type = $Configuration.TransferEncoding.Routes[$_]
$_pattern = (Convert-PodePathPatternToRegex -Path $_ -NotSlashes)
$Context.Server.Web.TransferEncoding.Routes[$_pattern] = $_type
}

# setup content type route patterns for error pages
# Configure error page routes
$Configuration.ErrorPages.Routes.Keys | Where-Object { ![string]::IsNullOrWhiteSpace($_) } | ForEach-Object {
$_type = $Configuration.ErrorPages.Routes[$_]
$_pattern = (Convert-PodePathPatternToRegex -Path $_ -NotSlashes)
$Context.Server.Web.ErrorPages.Routes[$_pattern] = $_type
}
}

function New-PodeAutoRestartServer {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSPossibleIncorrectComparisonWithNull', '')]
[CmdletBinding()]
Expand Down
48 changes: 26 additions & 22 deletions src/Public/OpenApi.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1919,11 +1919,11 @@ function Enable-PodeOAViewer {
}
# setup meta info
$meta = @{
Title = $Title
OpenApi = "$($OpenApiUrl)?format=yaml"
DarkMode = $DarkMode
DefinitionTag = $DefinitionTag
SwaggerEditorDist = 'https://unpkg.com/swagger-editor-dist@4'
Title = $Title
OpenApi = "$($OpenApiUrl)?format=yaml"
DarkMode = $DarkMode
DefinitionTag = $DefinitionTag
ContentDeliveryNetwork = $Podecontext.Server.Web.Static.ContentDeliveryNetwork
}
Add-PodeRoute -Method Get -Path $Path `
-Middleware $Middleware -ArgumentList $meta `
Expand All @@ -1932,9 +1932,9 @@ function Enable-PodeOAViewer {
-ScriptBlock {
param($meta)
$Data = @{
Title = $meta.Title
OpenApi = $meta.OpenApi
SwaggerEditorDist = $meta.SwaggerEditorDist
Title = $meta.Title
OpenApi = $meta.OpenApi
ContentDeliveryNetwork = $meta.ContentDeliveryNetwork
}

$podeRoot = Get-PodeModuleMiscPath
Expand All @@ -1952,10 +1952,11 @@ function Enable-PodeOAViewer {
}
# setup meta info
$meta = @{
Title = $Title
OpenApi = $OpenApiUrl
DarkMode = $DarkMode
DefinitionTag = $DefinitionTag
Title = $Title
OpenApi = $OpenApiUrl
DarkMode = $DarkMode
DefinitionTag = $DefinitionTag
ContentDeliveryNetwork = $Podecontext.Server.Web.Static.ContentDeliveryNetwork
}

$route = Add-PodeRoute -Method Get -Path $Path `
Expand All @@ -1965,8 +1966,9 @@ function Enable-PodeOAViewer {
-PassThru -ScriptBlock {
param($meta)
$Data = @{
Title = $meta.Title
OpenApi = $meta.OpenApi
Title = $meta.Title
OpenApi = $meta.OpenApi
ContentDeliveryNetwork = $meta.ContentDeliveryNetwork
}
$DefinitionTag = $meta.DefinitionTag
foreach ($type in $PodeContext.Server.OpenAPI.Definitions[$DefinitionTag].hiddenComponents.viewer.Keys) {
Expand Down Expand Up @@ -2000,10 +2002,11 @@ function Enable-PodeOAViewer {
}
# setup meta info
$meta = @{
Type = $Type.ToLowerInvariant()
Title = $Title
OpenApi = $OpenApiUrl
DarkMode = $DarkMode
Type = $Type.ToLowerInvariant()
Title = $Title
OpenApi = $OpenApiUrl
DarkMode = $DarkMode
ContentDeliveryNetwork = $Podecontext.Server.Web.Static.ContentDeliveryNetwork
}
$PodeContext.Server.OpenAPI.Definitions[$DefinitionTag].hiddenComponents.viewer[$($meta.Type)] = $Path
# add the viewer route
Expand All @@ -2015,10 +2018,11 @@ function Enable-PodeOAViewer {
$podeRoot = Get-PodeModuleMiscPath
if ( $meta.DarkMode) { $Theme = 'dark' } else { $Theme = 'light' }
Write-PodeFileResponseInternal -Path ([System.IO.Path]::Combine($podeRoot, "default-$($meta.Type).html.pode")) -Data @{
Title = $meta.Title
OpenApi = $meta.OpenApi
DarkMode = $meta.DarkMode
Theme = $Theme
Title = $meta.Title
OpenApi = $meta.OpenApi
DarkMode = $meta.DarkMode
Theme = $Theme
ContentDeliveryNetwork = $meta.ContentDeliveryNetwork
}
}
}
Expand Down