Skip to content

Commit

Permalink
Configurable cache Max-Age (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrekker authored Jul 17, 2023
1 parent 5a2432f commit b26f64b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import Glob
"Besides handling slider server request, should we also run a static file server of the export output folder? Set to `false` if you are serving the HTML files in another way, e.g. using GitHub Pages, and, for some reason, you do not want to *also* serve the HTML files using this serve."
serve_static_export_folder::Bool = true
simulated_lag::Real = 0
"Cache-Control header sent on requests in which caching is enabled. Set to `no-store, no-cache` to completely disable caching"
cache_control::String = "public, max-age=315600000, immutable"
end

@extract_docs @option struct ExportSettings
Expand Down
23 changes: 11 additions & 12 deletions src/HTTPRouter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ function make_router(
)
router = HTTP.Router()

with_cacheable_configured! = with_cacheable!(settings.SliderServer.cache_control)

function get_sesh(request::HTTP.Request)
uri = HTTP.URI(request.target)

Expand All @@ -55,7 +57,8 @@ function make_router(
If this is an automated setup, then this could happen inbetween deployments.
If this is a manual setup, then running the .jl notebook file might have caused a small change (e.g. the version number or a whitespace change). Copy notebooks to a temporary directory before running them using the bind server. =#
@info "Request hash not found. See error hint in my source code." notebook_hash maxlog=50
@info "Request hash not found. See error hint in my source code." notebook_hash maxlog =
50
nothing
else
notebook_sessions[i]
Expand Down Expand Up @@ -165,7 +168,7 @@ function make_router(
),
),
) |>
with_cacheable! |>
with_cacheable_configured! |>
with_cors! |>
with_msgpack!
elseif queued_for_bonds(sesh)
Expand All @@ -183,7 +186,7 @@ function make_router(
response = if ready_for_bonds(sesh)
HTTP.Response(200, Pluto.pack(sesh.run.bond_connections)) |>
with_cors! |>
with_cacheable! |>
with_cacheable_configured! |>
with_msgpack!
elseif queued_for_bonds(sesh)
HTTP.Response(503, "Still loading the notebooks... check back later!") |>
Expand Down Expand Up @@ -288,15 +291,11 @@ function with_cors!(response::HTTP.Response)
response
end

function with_cacheable!(response::HTTP.Response)
second = 1
minute = 60second
hour = 60minute
day = 24hour
year = 365day

HTTP.setheader(response, "Cache-Control" => "public, max-age=$(10year), immutable")
response
function with_cacheable!(cache_control::String)
return (response::HTTP.Response) -> begin
HTTP.setheader(response, "Cache-Control" => cache_control)
response
end
end

function with_not_cacheable!(response::HTTP.Response)
Expand Down

0 comments on commit b26f64b

Please sign in to comment.