Skip to content

Commit

Permalink
Update to Pluto 0.19.10 & HTTP 1.0 (#79)
Browse files Browse the repository at this point in the history
Co-authored-by: Fons van der Plas <[email protected]>
  • Loading branch information
Pangoraw and fonsp authored Sep 5, 2022
1 parent d2aac2c commit ec67aeb
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 97 deletions.
7 changes: 4 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ Configurations = "0.16, 0.17"
FromFile = "0.1"
Git = "1"
GitHubActions = "0.1"
HTTP = "^0.9.3"
HTTP = "^1.0.2"
JSON = "0.21"
Pluto = "0.19.5"
Pluto = "0.19.10"
TerminalLoggers = "0.1"
julia = "1.6"

[extras]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["Test", "Random"]
2 changes: 1 addition & 1 deletion src/Export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function try_get_exact_pluto_version()
catch e
if get(ENV, "HIDE_PLUTO_EXACT_VERSION_WARNING", "false") == "false"
@error "Failed to get exact Pluto version from dependency. Your website is not guaranteed to work forever." exception =
(e, catch_backtrace())
(e, catch_backtrace()) maxlog = 1
end
Pluto.PLUTO_VERSION
end
Expand Down
28 changes: 18 additions & 10 deletions src/HTTPRouter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function make_router(
end
end

HTTP.@register(
HTTP.register!(
router,
"GET",
"/",
Expand All @@ -217,11 +217,11 @@ function make_router(
end |>
with_cors! |>
with_not_cacheable!
end
end,
)


HTTP.@register(
HTTP.register!(
router,
"GET",
"/pluto_export.json",
Expand All @@ -230,15 +230,15 @@ function make_router(
with_json! |>
with_cors! |>
with_not_cacheable!
end
end,
)

# !!!! IDEAAAA also have a get endpoint with the same thing but the bond data is base64 encoded in the URL
# only use it when the amount of data is not too much :o

HTTP.@register(router, "POST", "/staterequest/*/", serve_staterequest)
HTTP.@register(router, "GET", "/staterequest/*/*", serve_staterequest)
HTTP.@register(router, "GET", "/bondconnections/*/", serve_bondconnections)
HTTP.register!(router, "POST", "/staterequest/*/", serve_staterequest)
HTTP.register!(router, "GET", "/staterequest/*/*", serve_staterequest)
HTTP.register!(router, "GET", "/bondconnections/*/", serve_bondconnections)

if static_dir !== nothing
function serve_pluto_asset(request::HTTP.Request)
Expand All @@ -250,14 +250,14 @@ function make_router(
)
Pluto.asset_response(filepath)
end
HTTP.@register(router, "GET", "/pluto_asset/*", serve_pluto_asset)
HTTP.register!(router, "GET", "/pluto_asset/*", serve_pluto_asset)
function serve_asset(request::HTTP.Request)
uri = HTTP.URI(request.target)

filepath = joinpath(static_dir, relpath(HTTP.unescapeuri(uri.path), "/"))
Pluto.asset_response(filepath)
end
HTTP.@register(router, "GET", "/*", serve_asset)
HTTP.register!(router, "GET", "/*", serve_asset)
end

router
Expand Down Expand Up @@ -294,6 +294,14 @@ function with_cacheable!(response::HTTP.Response)
end

function with_not_cacheable!(response::HTTP.Response)
push!(response.headers, "Cache-Control" => "no-store, no-cache, max-age=5")
push!(response.headers, "Cache-Control" => "no-store, no-cache")
response
end

function ReferrerMiddleware(handler)
return function (req::HTTP.Request)
response = handler(req)
push!(response.headers, "Referrer-Policy" => "origin-when-cross-origin")
return response
end
end
73 changes: 32 additions & 41 deletions src/PlutoSliderServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using FromFile
@from "./ConfigurationDocs.jl" import @extract_docs,
get_kwdocs, list_options_md, list_options_toml
@from "./ReloadFolder.jl" import update_sessions!, select
@from "./HTTPRouter.jl" import make_router
@from "./HTTPRouter.jl" import make_router, ReferrerMiddleware
@from "./gitpull.jl" import fetch_pull

@from "./PlutoHash.jl" import plutohash, base64urlencode, base64urldecode
Expand Down Expand Up @@ -48,7 +48,9 @@ function load_cool_logger()
logger_loaded[] = true
if ((global_logger() isa ConsoleLogger) && !is_inside_pluto())
if get(ENV, "GITHUB_ACTIONS", "false") == "true"
global_logger(GitHubActionsLogger())
# TODO: disabled because of https://github.com/JuliaWeb/HTTP.jl/issues/921

# global_logger(GitHubActionsLogger())
else
global_logger(try
TerminalLogger(; margin=1)
Expand Down Expand Up @@ -272,39 +274,17 @@ function run_directory(

@info "# Starting server..." address

# This is boilerplate HTTP code, don't read it
# We start the HTTP server before launching notebooks so that the server responds to heroku/digitalocean garbage fast enough
http_server_task = @async HTTP.serve(
http_server = HTTP.serve!(
router |> ReferrerMiddleware,
hostIP,
UInt16(port),
stream=true,
server=serversocket,
) do http::HTTP.Stream
request::HTTP.Request = http.message
request.body = read(http)
HTTP.closeread(http)

params = HTTP.queryparams(HTTP.URI(request.target))

response_body = Base.invokelatest(HTTP.handle, router, request)

request.response::HTTP.Response = response_body
request.response.request = request
try
HTTP.setheader(http, "Referrer-Policy" => "origin-when-cross-origin")
HTTP.startwrite(http)
write(http, request.response.body)
HTTP.closewrite(http)
catch e
if isa(e, Base.IOError) || isa(e, ArgumentError)
# @warn "Attempted to write to a closed stream at $(request.target)"
else
rethrow(e)
end
end
end
)

@info "# Server started"
else
http_server_task = @async 1 + 1
http_server = nothing
serversocket = nothing
end

Expand Down Expand Up @@ -406,20 +386,31 @@ function run_directory(
watch_folder(debounced, start_dir)
end

if should_watch
# todo: skip first watch_folder so that we dont need this sleepo
sleep(2)
end


on_ready((; serversocket, server_session, notebook_sessions))
if http_server === nothing
on_ready((; serversocket, http_server, server_session, notebook_sessions))
else
try
if should_watch
# todo: skip first watch_folder so that we dont need this sleepo (EDIT: i forgot why this sleep is here.. oops!)
sleep(2)
end
on_ready((; serversocket, http_server, server_session, notebook_sessions))

try
wait(http_server_task)
catch e
@ignorefailure close(serversocket)
@ignorefailure schedule(watch_dir_task, e; error=true)
e isa InterruptException || rethrow(e)
# blocking call, waiting for a Ctrl-C interrupt
wait(http_server)
catch e
@info "# Closing web server..."
@ignorefailure close(http_server)
if should_watch
@info "Stopping directory watching..."
istaskdone(watch_dir_task) ||
@ignorefailure schedule(watch_dir_task, e; error=true)
end
e isa InterruptException || rethrow(e)
@info "Server exited ✅"
end
end
end

Expand Down
File renamed without changes.
31 changes: 12 additions & 19 deletions test/filewatching.jl → test/Folder watching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ function cp_nb_with_tweaks(from::String, to::String)
end

@testset "Folder watching" begin
test_dir = tempname(cleanup=false)
mkdir(test_dir)
test_dir = mktempdir(cleanup=false)

try
# open the folder on macos:
Expand All @@ -52,6 +51,7 @@ end
cp_nb_with_tweaks(joinpath(@__DIR__, p), joinpath(test_dir, p))
end

Random.seed!(time_ns())
port = rand(12345:65000)


Expand All @@ -62,22 +62,14 @@ end
still_booting[] = false
end

t = Pluto.@asynclog begin
try
PlutoSliderServer.run_directory(
test_dir;
Export_enabled=false,
Export_output_dir=test_dir,
SliderServer_port=port,
SliderServer_watch_dir=true,
on_ready,
)
catch e
if !(e isa TaskFailedException)
showerror(stderr, e, stacktrace(catch_backtrace()))
end
end
end
t = Pluto.@asynclog PlutoSliderServer.run_directory(
test_dir;
Export_enabled=false,
Export_output_dir=test_dir,
SliderServer_port=port,
SliderServer_watch_dir=true,
on_ready,
)


while still_booting[]
Expand All @@ -92,6 +84,7 @@ end

json_nbs() = index_json()["notebooks"] |> keys |> collect

@test length(notebook_sessions) == 1
@test json_nbs() == ["basic2.jl"]

@test index_json()["notebooks"]["basic2.jl"]["frontmatter"]["title"] == "Pancakes"
Expand Down Expand Up @@ -247,7 +240,7 @@ end
end

sleep(2)
close(ready_result[].serversocket)
close(ready_result[].http_server)

try
wait(t)
Expand Down
25 changes: 10 additions & 15 deletions test/staterequest.jl → test/HTTP requests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import PlutoSliderServer.Pluto
import PlutoSliderServer.HTTP

using Test
using UUIDs
using UUIDs, Random

@testset "HTTP requests" begin
Random.seed!(time_ns())
test_dir = tempname(cleanup=false)
cp(@__DIR__, test_dir)

Expand All @@ -22,19 +23,13 @@ using UUIDs
end

t = Pluto.@asynclog begin
try
PlutoSliderServer.run_directory(
test_dir;
Export_enabled=false,
SliderServer_port=port,
notebook_paths,
on_ready,
)
catch e
if !(e isa TaskFailedException)
showerror(stderr, e, stacktrace(catch_backtrace()))
end
end
PlutoSliderServer.run_directory(
test_dir;
Export_enabled=false,
SliderServer_port=port,
notebook_paths,
on_ready,
)
end


Expand Down Expand Up @@ -110,7 +105,7 @@ using UUIDs
end
end

close(ready_result[].serversocket)
close(ready_result[].http_server)

try
wait(t)
Expand Down
2 changes: 1 addition & 1 deletion test/plutohash.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Test
import PlutoSliderServer: plutohash, base64urlencode, base64urldecode
import HTTP

@testset "PlutoHash" begin
@testset "plutohash" begin
@test plutohash("Hannes") == "OI48wVWerxEEnz5lIj6CPPRB8NOwwba-LkFYTDp4aUU"
@test base64urlencode(UInt8[0, 0, 63, 0, 0, 62, 42]) == "AAA_AAA-Kg"

Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ else
include("./plutohash.jl")
include("./configuration.jl")
include("./static export.jl")
include("./staterequest.jl")
include("./filewatching.jl")
include("./connections.jl")
include("./HTTP requests.jl")
include("./Folder watching.jl")
include("./Bond connections.jl")
end
1 change: 1 addition & 0 deletions test/runtestserver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using PlutoSliderServer
ENV["JULIA_DEBUG"] = PlutoSliderServer


Random.seed!(time_ns())
test_dir = tempname(cleanup=false)
cp(@__DIR__, test_dir)

Expand Down
Loading

0 comments on commit ec67aeb

Please sign in to comment.