Skip to content

Commit d24f2cf

Browse files
Convert generic_http provider to extension
1 parent 464b82a commit d24f2cf

File tree

4 files changed

+49
-38
lines changed

4 files changed

+49
-38
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1616
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1717
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
1818
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
19-
WebSockets = "104b5d7c-a370-577a-8038-80a2059c5097"
2019
Widgets = "cc8bc4a8-27d6-5769-a93b-9d913e69aa62"
2120

2221
[weakdeps]
2322
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
2423
Mux = "a975b10e-0019-58db-a62f-e48ff68538c9"
24+
WebSockets = "104b5d7c-a370-577a-8038-80a2059c5097"
2525

2626
[extensions]
2727
IJuliaExt = "IJulia"
2828
MuxExt = "Mux"
29+
WebSocketsExt = "WebSockets"
2930

3031
[compat]
3132
AssetRegistry = "0.1.0"

ext/generic_http.jl renamed to ext/WebSocketsExt.jl

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using .Sockets
2-
import .AssetRegistry, .JSON
3-
using .WebIO
4-
using .WebSockets: is_upgrade, upgrade, writeguarded
5-
using .WebSockets: HTTP
1+
module WebSocketsExt
62

3+
using WebIO, JSON, AssetRegistry
4+
using Sockets
5+
6+
using WebIO: WEBIO_APPLICATION_MIME, GENERIC_HTTP_BUNDLE_PATH, bundle_key,
7+
global_server_config, WebIOServer, singleton_instance, routing_callback,
8+
webio_server_config
9+
using WebSockets: WebSockets, HTTP, is_upgrade, upgrade, writeguarded
710

811
struct WSConnection{T} <: WebIO.AbstractConnection
912
sock::T
@@ -12,15 +15,7 @@ end
1215
Sockets.send(p::WSConnection, data) = writeguarded(p.sock, JSON.json(data))
1316
Base.isopen(p::WSConnection) = isopen(p.sock)
1417

15-
if !isfile(GENERIC_HTTP_BUNDLE_PATH)
16-
error(
17-
"Unable to find WebIO JavaScript bundle for generic HTTP provider; "
18-
* "try rebuilding WebIO (via `Pkg.build(\"WebIO\")`)."
19-
)
20-
end
21-
const bundle_key = AssetRegistry.register(GENERIC_HTTP_BUNDLE_PATH)
22-
23-
include(joinpath(@__DIR__, "..", "..", "deps", "mimetypes.jl"))
18+
include(joinpath(@__DIR__, "..", "deps", "mimetypes.jl"))
2419

2520
"""
2621
Serve an asset from the asset registry.
@@ -52,17 +47,8 @@ function websocket_handler(ws)
5247
end
5348
end
5449

55-
struct WebIOServer{S}
56-
server::S
57-
serve_task::Task
58-
end
59-
6050
kill!(server::WebIOServer) = put!(server.server.in, HTTP.Servers.KILL)
6151

62-
const singleton_instance = Ref{WebIOServer}()
63-
64-
const routing_callback = Ref{Any}((req)-> missing)
65-
6652
"""
6753
Run the WebIO server.
6854
@@ -84,7 +70,7 @@ end
8470
server = WebIO.WebIOServer(serve_app, verbose = true)
8571
```
8672
"""
87-
function WebIOServer(
73+
function WebIO.WebIOServer(
8874
default_response::Function = (req)-> missing;
8975
baseurl::String = "127.0.0.1", http_port::Int = 8081,
9076
verbose = false, singleton = true,
@@ -111,7 +97,7 @@ function WebIOServer(
11197
else # relative url
11298
string("http://", baseurl, ":", http_port, WebIO.baseurl[])
11399
end
114-
string(base, bundle_key)
100+
string(base, bundle_key[])
115101
end
116102
wait_time = 5; start = time() # wait for max 5 s
117103
while time() - start < wait_time
@@ -127,24 +113,22 @@ function WebIOServer(
127113
return singleton_instance[]
128114
end
129115

130-
const webio_server_config = Ref{typeof((url = "", bundle_url = "", http_port = 0, ws_url = ""))}()
131-
132116
"""
133117
Fetches the global configuration for our http + websocket server from environment
134118
variables. It will memoise the result, so after a first call, any update to
135119
the environment will get ignored.
136120
"""
137-
function global_server_config()
121+
function WebIO.global_server_config()
138122
if !isassigned(webio_server_config)
139123

140-
setbaseurl!(get(ENV, "JULIA_WEBIO_BASEURL", ""))
124+
WebIO.setbaseurl!(get(ENV, "JULIA_WEBIO_BASEURL", ""))
141125

142126
url = get(ENV, "WEBIO_SERVER_HOST_URL", "127.0.0.1")
143127
http_port = parse(Int, get(ENV, "WEBIO_HTTP_PORT", "8081"))
144128
ws_default = string("ws://", url, ":", http_port, "/webio_websocket/")
145129
ws_url = get(ENV, "WEBIO_WEBSOCKT_URL", ws_default)
146130
# make it possible, to e.g. host the bundle online
147-
bundle_url = get(ENV, "WEBIO_BUNDLE_URL", string(WebIO.baseurl[], bundle_key))
131+
bundle_url = get(ENV, "WEBIO_BUNDLE_URL", string(WebIO.baseurl[], bundle_key[]))
148132
webio_server_config[] = (
149133
url = url, bundle_url = bundle_url,
150134
http_port = http_port, ws_url = ws_url
@@ -176,3 +160,15 @@ function Base.show(io::IO, m::WEBIO_APPLICATION_MIME, app::Application)
176160
show(io, "text/html", app)
177161
return
178162
end
163+
164+
function __init__()
165+
if !isfile(GENERIC_HTTP_BUNDLE_PATH)
166+
error(
167+
"Unable to find WebIO JavaScript bundle for generic HTTP provider; "
168+
* "try rebuilding WebIO (via `Pkg.build(\"WebIO\")`)."
169+
)
170+
end
171+
bundle_key[] = AssetRegistry.register(GENERIC_HTTP_BUNDLE_PATH)
172+
end
173+
174+
end

src/WebIO.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ function prefetch_provider_file(basename)
9494
(file = filepath, code = code)
9595
end
9696

97-
provider_generic_http = prefetch_provider_file("generic_http.jl")
98-
9997
struct _IJuliaInit
10098
function _IJuliaInit()
10199
# based on assert_extension from longemen3000/ExtensionsExt
@@ -107,14 +105,30 @@ struct _IJuliaInit
107105
end
108106
end
109107

108+
struct WebIOServer{S}
109+
server::S
110+
serve_task::Task
111+
112+
function WebIOServer(server::S, serve_task::Task) where S
113+
# based on assert_extension from longemen3000/ExtensionsExt
114+
ext = Base.get_extension(@__MODULE__, :WebSocketsExt)
115+
if isnothing(ext)
116+
throw(error("Extension `WebSocketsExt` must be loaded to construct the type `WebIOServer`."))
117+
end
118+
return new{S}(server, serve_task)
119+
end
120+
end
121+
110122
function webio_serve end
123+
function global_server_config end
124+
125+
const bundle_key = Ref{String}()
126+
const singleton_instance = Ref{WebIOServer}()
127+
const routing_callback = Ref{Any}((req) -> missing)
128+
const webio_server_config = Ref{typeof((url = "", bundle_url = "", http_port = 0, ws_url = ""))}()
111129

112130
function __init__()
113131
push!(Observables.addhandler_callbacks, WebIO.setup_comm)
114-
@require WebSockets="104b5d7c-a370-577a-8038-80a2059c5097" begin
115-
include_string(@__MODULE__, provider_generic_http.code, provider_generic_http.file)
116-
end
117-
118132
end
119133

120134
end # module

test/http-tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ using Test
77
@test occursin("_webIOWebSocketURL = ", output)
88
@test occursin("ws://127.0.0.1:8081/webio_websocket/", output)
99
@test occursin("""hello, world""", output)
10-
@test WebIO.webio_server_config[] == (url = "127.0.0.1", bundle_url = WebIO.bundle_key, http_port = 8081, ws_url = "ws://127.0.0.1:8081/webio_websocket/")
10+
@test WebIO.webio_server_config[] == (url = "127.0.0.1", bundle_url = WebIO.bundle_key[], http_port = 8081, ws_url = "ws://127.0.0.1:8081/webio_websocket/")
1111
@test isassigned(WebIO.singleton_instance)
1212
end

0 commit comments

Comments
 (0)