Skip to content

Instantly share code, notes, and snippets.

@fredrikekre
Created March 16, 2022 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrikekre/c7b2a91625ccb58610c67120f7617204 to your computer and use it in GitHub Desktop.
Save fredrikekre/c7b2a91625ccb58610c67120f7617204 to your computer and use it in GitHub Desktop.
LiveServer.jl convenience script.
#!/bin/bash
#=
exec julia --startup-file=no --compile=min -O0 -e 'include(popfirst!(ARGS))' "${BASH_SOURCE[0]}" "$@"
=#
port::Int = 8000
host::String = "127.0.0.1"
verbose::Bool = false
dir::String = pwd()
function usage(rc)
name = basename(@__FILE__)
io = rc == 0 ? stdout : stderr
printstyled(io, "NAME\n", bold=true)
println(io, " $(name) - run a webserver")
println(io)
printstyled(io, "SYNOPSIS\n", bold=true)
println(io, " $(basename(@__FILE__)) [-h <host>] [-p <port>] [-v] [--help] <directory>")
println(io)
printstyled(io, "DESCRIPTION\n", bold=true)
println(io, """ $(name) starts a web server serving the contents of the specified
filesystem directory using the LiveServer.jl Julia package.
Thus, it is required that (i) julia is available in PATH, and
(ii) that LiveServer.jl is available in Julias LOAD_PATH.
""")
printstyled(io, "OPTIONS\n", bold=true)
println(io, " <directory>")
println(io, " Path to the root directory of the server (default: pwd)")
println(io, " -h <host>")
println(io, " Specify the host (default: 127.0.0.1)")
println(io, " -p <port>")
println(io, " Specify the port (default: 8000)")
println(io, " -v")
println(io, " Enable verbose output")
println(io, " --help")
println(io, " Show this message")
exit(rc)
end
while length(ARGS) > 0
x = popfirst!(ARGS)
if x == "-p"
p = tryparse(Int, popfirst!(ARGS))
p === nothing && usage(1)
global port = p
elseif x == "-h"
global host = popfirst!(ARGS)
elseif x == "-v"
global verbose = true
elseif x == "--help"
usage(0)
elseif !startswith(x, "-") && isdir(x)
global dir = x
length(ARGS) == 0 || usage(1)
break
else
usage(1)
end
end
import LiveServer
LiveServer.serve(; host, port, dir, verbose)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment