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

Hh addfileroute #705

Merged
merged 5 commits into from
Feb 19, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/Assets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function add_fileroute(assets_config::Genie.Assets.AssetsConfig, filename::Abstr
basedir = pwd(),
type::Union{Nothing, String} = nothing,
content_type::Union{Nothing, Symbol} = nothing,
ext::Union{Nothing, String} = nothing, kwargs...)
ext::Union{Nothing, String} = nothing, named::Union{Symbol, Nothing} = nothing, kwargs...)

file, ex = splitext(filename)
ext = isnothing(ext) ? ex : ext
Expand All @@ -345,7 +345,7 @@ function add_fileroute(assets_config::Genie.Assets.AssetsConfig, filename::Abstr
Symbol("*.*")
end : content_type

Genie.Router.route(Genie.Assets.asset_path(assets_config, type; file, ext, kwargs...)) do
Genie.Router.route(Genie.Assets.asset_path(assets_config, type; file, ext, kwargs...); named) do
Genie.Renderer.WebRenderable(
Genie.Assets.embedded(Genie.Assets.asset_file(cwd=basedir; type, file)),
content_type) |> Genie.Renderer.respond
Expand Down
43 changes: 30 additions & 13 deletions src/Server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using HTTP, Sockets, HTTP.WebSockets
import Millboard, Distributed, Logging
import Genie
import Distributed
import HTTP.Servers: Listener, forceclose


"""
Expand All @@ -15,8 +16,8 @@ import Distributed
Represents a object containing references to Genie's web and websockets servers.
"""
Base.@kwdef mutable struct ServersCollection
webserver::Union{Task,Nothing} = nothing
websockets::Union{Task,Nothing} = nothing
webserver::Union{T,Nothing} where T <: HTTP.Server = nothing
websockets::Union{T,Nothing} where T <: HTTP.Server = nothing
end

"""
Expand Down Expand Up @@ -101,7 +102,7 @@ function up(port::Int,
if Genie.config.websockets_server !== nothing && port !== ws_port
print_server_status("Web Sockets server starting at $host:$ws_port")

new_server.websockets = @async HTTP.listen(host, ws_port; verbose = verbose, rate_limit = ratelimit, server = wsserver,
new_server.websockets = HTTP.listen!(host, ws_port; verbose = verbose, rate_limit = ratelimit, server = wsserver,
reuseaddr = reuseaddr, http_kwargs...) do http::HTTP.Stream
if HTTP.WebSockets.isupgrade(http.message)
HTTP.WebSockets.upgrade(http) do ws
Expand All @@ -112,7 +113,7 @@ function up(port::Int,
end

command = () -> begin
HTTP.listen(parse(Sockets.IPAddr, host), port; verbose = verbose, rate_limit = ratelimit, server = server,
HTTP.listen!(parse(Sockets.IPAddr, host), port; verbose = verbose, rate_limit = ratelimit, server = server,
reuseaddr = reuseaddr, http_kwargs...) do stream::HTTP.Stream
try
if Genie.config.websockets_server !== nothing && port === ws_port && HTTP.WebSockets.isupgrade(stream.message)
Expand All @@ -134,16 +135,31 @@ function up(port::Int,
server_url *= ("?" * join(["$(k)=$(v)" for (k, v) in query], "&"))
end

status = if async
if async
print_server_status("Web Server starting at $server_url")
@async command()
else
print_server_status("Web Server starting at $server_url - press Ctrl/Cmd+C to stop the server.")
end

listener = try
command()
catch
nothing
end
if !async && !isnothing(listener)
try
wait(listener)
catch
nothing
finally
close(listener)
# close the corresponding websocket server
new_server.websockets !== nothing && isopen(new_server.websockets) && close(new_server.websockets)
end
end

if status !== nothing && status.state === :runnable
new_server.webserver = status
if listener !== nothing && isopen(listener)
new_server.webserver = listener

try
open_browser && openbrowser(server_url)
Expand Down Expand Up @@ -237,18 +253,19 @@ end
Shuts down the servers optionally indicating which of the `webserver` and `websockets` servers to be stopped.
It does not remove the servers from the `SERVERS` collection. Returns the collection.
"""
function down(; webserver::Bool = true, websockets::Bool = true) :: Vector{ServersCollection}
function down(; webserver::Bool = true, websockets::Bool = true, force::Bool = true) :: Vector{ServersCollection}
for i in 1:length(SERVERS)
down(SERVERS[i]; webserver, websockets)
down(SERVERS[i]; webserver, websockets, force)
end

SERVERS
end


function down(server::ServersCollection; webserver::Bool = true, websockets::Bool = true) :: ServersCollection
webserver && (@async Base.throwto(server.webserver, InterruptException()))
isnothing(websockets) || (websockets && (@async Base.throwto(server.websockets, InterruptException())))
function down(server::ServersCollection; webserver::Bool = true, websockets::Bool = true, force::Bool = true) :: ServersCollection
close_cmd = force ? forceclose : close
webserver && !isnothing(server.webserver) && isopen(server.webserver) && close_cmd(server.webserver)
websockets && !isnothing(server.websockets) && isopen(server.websockets) && close_cmd(server.websockets)

server
end
Expand Down
12 changes: 6 additions & 6 deletions test/tests_AppServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
empty!(Genie.Server.SERVERS)

servers = Genie.Server.up()
@test servers.webserver.state == :runnable
@test isopen(servers.webserver)

servers = Genie.Server.down(servers)
sleep(1)
@test servers.webserver.state == :failed
@test Genie.Server.SERVERS[1].webserver.state == :failed
@test !isopen(servers.webserver)
@test !isopen(Genie.Server.SERVERS[1].webserver)

servers = Genie.Server.down!()
empty!(Genie.Server.SERVERS)

servers = Genie.Server.up(; open_browser = false)
Genie.Server.down(servers; webserver = false)
@test servers.webserver.state == :runnable
@test isopen(servers.webserver)

servers = Genie.Server.down(servers; webserver = true)
sleep(1)
@test servers.webserver.state == :failed
@test Genie.Server.SERVERS[1].webserver.state == :failed
@test !isopen(servers.webserver)
@test !isopen(Genie.Server.SERVERS[1].webserver)

servers = nothing
end;
Expand Down
Loading