Skip to content

Commit

Permalink
n-th attempt at fixing 135 (#150)
Browse files Browse the repository at this point in the history
* merge fixes and cleanup

* adding setter and resetter functions for *_DIR

* attempt to fix #135 in depth

* removing debug code

* some cleanup + notes

* joining across substring for 1.6

* fix joinpath over vector

* nth attempt at closing #135

* redirect with query or anchor

* cleaning up with uri.path

* remove 'append_slash' test
  • Loading branch information
tlienart authored Sep 14, 2022
1 parent ded962e commit 34c74e0
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 102 deletions.
4 changes: 0 additions & 4 deletions .codecov.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The LiveServer.jl package is licensed under the MIT "Expat" License:

> Copyright (c) 2019: Jonas Asprion, Thibaut Lienart.
> Copyright (c) 2019-2022: Jonas Asprion, Thibaut Lienart, and collaborators.
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 0 additions & 6 deletions NEWS.md

This file was deleted.

12 changes: 8 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
name = "LiveServer"
uuid = "16fef848-5104-11e9-1b77-fb7a48bbb589"
authors = ["Jonas Asprion <[email protected]", "Thibaut Lienart <[email protected]>"]
version = "1.0.0"
version = "1.0.1"

[deps]
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
FileWatching = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
MIMEs = "6c6e2e6c-3030-632d-7369-2d6c69616d65"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Crayons = "4"
HTTP = "1"
MIMEs = "0.1"
julia = "1.6"

[extras]
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Crayons"]
63 changes: 47 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@
[![codecov](https://codecov.io/gh/tlienart/LiveServer.jl/branch/master/graph/badge.svg?token=mNry6r2aIn)](https://codecov.io/gh/tlienart/LiveServer.jl)
[![dev-doc](https://img.shields.io/badge/docs-dev-blue.svg)](https://tlienart.github.io/LiveServer.jl/dev/)

This is a simple and lightweight development web-server written in Julia, based on [HTTP.jl](https://github.com/JuliaWeb/HTTP.jl).
It has live-reload capability, i.e. when modifying a file, every browser (tab) currently displaying the corresponding page is automatically refreshed.
This is a simple and lightweight development web-server written in Julia,
based on [HTTP.jl](https://github.com/JuliaWeb/HTTP.jl).
It has live-reload capability, i.e. when modifying a file, every browser (tab)
currently displaying the corresponding page is automatically refreshed.

LiveServer is inspired from Python's [`http.server`](https://docs.python.org/3/library/http.server.html) and Node's [`browsersync`](https://www.browsersync.io/).
LiveServer is inspired from Python's [`http.server`](https://docs.python.org/3/library/http.server.html)
and Node's [`browsersync`](https://www.browsersync.io/).

## Installation

To install it in Julia ≥ 1.3, use the package manager with
To install it in Julia ≥ 1.6, use the package manager with

```julia-repl
pkg> add LiveServer
```

### Legacy notes

For Julia `< 1.6`, you can use LiveServer's version 0.9.2:

```julia-repl
pkg> add [email protected]
```

For Julia `[1.0, 1.3)`, you can use LiveServer's version 0.7.4:

```julia-repl
Expand All @@ -25,20 +36,24 @@ pkg> add [email protected]

### Make it a shell command

LiveServer is a small package and fast to load with one main functionality (`serve`), it can be convenient to make it a shell command: (I'm using the name `lss` here but you could use something else):
LiveServer is a small package and fast to load with one main functionality (`serve`),
it can be convenient to make it a shell command: (I'm using the name `lss` here but
you could use something else):

```
alias lss='julia -e "import LiveServer as LS; LS.serve(launch_browser=true)"'
```

you can then use `lss` in any directory to show a directory listing in your browser, and if the directory has an `index.html` then that will be rendered in your browser.
you can then use `lss` in any directory to show a directory listing in your browser,
and if the directory has an `index.html` then that will be rendered in your browser.

## Usage

The main function `LiveServer` exports is `serve` which starts listening to the current folder and makes its content available to a browser.
The main function `LiveServer` exports is `serve` which starts listening to the current
folder and makes its content available to a browser.
The following code creates an example directory and serves it:

```julia
```julia-repl
julia> using LiveServer
julia> LiveServer.example() # creates an "example/" folder with some files
julia> cd("example")
Expand All @@ -47,7 +62,8 @@ julia> serve() # starts the local server & the file watching
(use CTRL+C to shut down)
```

Open a Browser and go to `http://localhost:8000/` to see the content being rendered; try modifying files (e.g. `index.html`) and watch the changes being rendered immediately in the browser.
Open a Browser and go to `http://localhost:8000/` to see the content being rendered;
try modifying files (e.g. `index.html`) and watch the changes being rendered immediately in the browser.

In the REPL:
```julia-repl
Expand All @@ -58,21 +74,25 @@ julia> serve(host="0.0.0.0", port=8001, dir=".") # starts the remote server & th
```

In the terminal:
```julia-repl
```bash
julia -e 'using LiveServer; serve(host="0.0.0.0", port=8001, dir=".")'
```

Open a browser and go to https://localhost:8001/ to see the rendered content of index.html or, if it doesn't exist, the content of the directory.
Open a browser and go to https://localhost:8001/ to see the rendered content of index.html or,
if it doesn't exist, the content of the directory.
You can set the port to a custom number.
This is similar to the [`http.server`](https://docs.python.org/3/library/http.server.html) in Python.

### Serve docs

`servedocs` is a convenience function that runs `Documenter` along with `LiveServer` to watch your doc files for any changes and render them in your browser when modifications are detected.
`servedocs` is a convenience function that runs `Documenter` along with `LiveServer` to watch
your doc files for any changes and render them in your browser when modifications are detected.

Assuming you are in `directory/to/YourPackage.jl`, that you have a `docs/` folder as prescribed by [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl) and `LiveServer` installed in your global environment, you can run:
Assuming you are in `directory/to/YourPackage.jl`, that you have a `docs/` folder as
prescribed by [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl) and `LiveServer`
installed in your global environment, you can run:

```julia
```julia-repl
$ julia
pkg> activate docs
Expand All @@ -88,12 +108,23 @@ julia> servedocs()
(use CTRL+C to shut down)
```

Open a browser and go to `http://localhost:8000/` to see your docs being rendered; try modifying files (e.g. `docs/index.md`) and watch the changes being rendered in the browser.
Open a browser and go to `http://localhost:8000/` to see your docs being rendered;
try modifying files (e.g. `docs/index.md`) and watch the changes being rendered in the browser.

To run the server with one line of code, run:

```
$ julia --project=docs -ie 'using YourPackage, LiveServer; servedocs()'
```

**Note**: this works with [Literate.jl](https://github.com/fredrikekre/Literate.jl) as well. See [the docs](https://tlienart.github.io/LiveServer.jl/dev/man/ls+lit/).
**Note**: this works with [Literate.jl](https://github.com/fredrikekre/Literate.jl) as well.
See [the docs](https://tlienart.github.io/LiveServer.jl/dev/man/ls+lit/).


## DEV/Path testing

See also issue #135 and related PRs.

* `servedocs()`, navigate to literate, images should show
* `serve()` navigate manually to `docs/build/` should show, remove trailing slash in URL `docs/build` should redirect to `docs/build/`
* `serve(dir=...)` should work + when navigating to assets etc
9 changes: 5 additions & 4 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ julia> using LiveServer
julia> LiveServer.example() # creates an "example/" folder with some files
julia> cd("example")
julia> serve() # starts the local server & the file watching
✓ LiveServer listening on http://localhost:8000...
✓ LiveServer listening on http://localhost:8000/ ...
(use CTRL+C to shut down)
```

Expand All @@ -45,9 +45,10 @@ Open a browser and go to https://localhost:8001/ to see the rendered content of
You can set the port to a custom number.
This is similar to the [`http.server`](https://docs.python.org/3/library/http.server.html) in Python.

### Serve docs
### Live serve your docs

A function derived from `serve` that will be convenient to Julia package developpers is `servedocs`. It runs [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl) along with `LiveServer` to render your docs and will track and render any modifications to your docs.
A function derived from `serve` that will be convenient to Julia package developers is `servedocs`.
It runs [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl) along with `LiveServer` to render your docs and will track and render any modifications to your docs.
This instantaneous feedback makes writing docs significantly easier and faster.

Assuming you are in `directory/to/YourPackage.jl` and that you have a `docs/` folder as prescribed by `Documenter`, just run:
Expand All @@ -66,7 +67,7 @@ julia> servedocs()
Open a browser and go to `http://localhost:8000/` to see your docs being rendered; try modifying files (e.g. `docs/index.md`) and watch the changes being rendered in the browser.
You can also use LiveServer with both Documenter and [Literate.jl](https://github.com/fredrikekre/Literate.jl).
This is explained [here](man/ls+lit.md).
This is explained [here](man/ls+lit/).
## How it works
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ LiveServer.scan_docs!

```@docs
LiveServer.example
LiveServer.setverbose
LiveServer.set_verbose
```
4 changes: 2 additions & 2 deletions docs/src/man/ls+lit.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ julia> servedocs(literate=joinpath("docs", "literate"))

if you then navigate to `localhost:8000` you should end up with

![](../assets/testlit.png)
![](/assets/testlit.png)

if you modify `test_dir/docs/literate/man/pg1.jl` for instance writing `f(4)` it will be applied directly:

![](../assets/testlit2.png)
![](/assets/testlit2.png)
19 changes: 15 additions & 4 deletions src/LiveServer.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module LiveServer

# from stdlib
using Sockets, Pkg
# the only dependency (see the patch in http_patch.jl)
import Sockets, Pkg, MIMEs
using Base.Filesystem

using HTTP

export serve, servedocs
Expand All @@ -17,7 +17,10 @@ const BROWSER_RELOAD_SCRIPT = read(joinpath(@__DIR__, "client.html"), String)
"""Whether to display messages while serving or not, see [`verbose`](@ref)."""
const VERBOSE = Ref{Bool}(false)

"""The folder to watch, either the current one or a specified one."""
"""Whether to display debug messages while serving"""
const DEBUG = Ref{Bool}(false)

"""The folder to watch, either the current one or a specified one (dir=...)."""
const CONTENT_DIR = Ref{String}("")

"""List of files being tracked with WebSocket connections."""
Expand All @@ -26,6 +29,14 @@ const WS_VIEWERS = Dict{String,Vector{HTTP.WebSockets.WebSocket}}()
"""Keep track of whether an interruption happened while processing a websocket."""
const WS_INTERRUPT = Base.Ref{Bool}(false)


set_content_dir(d::String) = (CONTENT_DIR[] = d;)
reset_content_dir() = set_content_dir("")
set_verbose(b::Bool) = (VERBOSE[] = b;)
set_debug(b::Bool) = (DEBUG[] = b;)

reset_ws_interrupt() = (WS_INTERRUPT[] = false)

#
# Functions
#
Expand Down
3 changes: 2 additions & 1 deletion src/file_watching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ end
Checks whether the file specified by `f_path` is being watched.
"""
is_watched(fw::FileWatcher, f_path::AbstractString) = any(wf -> wf.path == f_path, fw.watchedfiles)
is_watched(fw::FileWatcher, f_path::AbstractString) =
any(wf -> wf.path == f_path, fw.watchedfiles)


"""
Expand Down
Loading

2 comments on commit 34c74e0

@tlienart
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/68249

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.1 -m "<description of version>" 34c74e01a7f7e2b502296fb53867e4a60352af81
git push origin v1.0.1

Please sign in to comment.