Skip to content

Commit

Permalink
v1.0 stuff + variable routes
Browse files Browse the repository at this point in the history
Appveyor / Travis

Travis: 1 -> 1.0

using Test

Update README
  • Loading branch information
EricForgy committed Sep 9, 2018
1 parent 55f6f66 commit 0dda5a5
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 176 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
- linux
- osx
julia:
- 0.6
- 1.0
notifications:
email: false
# uncomment the following lines to override the default test script
Expand Down
34 changes: 10 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ This is a package designed to help get started with web APIs and some basic inte
To get the latest tagged version, try:

~~~julia
julia> Pkg.add("Pages")
pkg> add Pages
~~~

However, this package is still in early development and is likely to change often. To get the latest, try:

~~~julia
julia> Pkg.checkout("Pages")
pkg> add Pages#master
~~~

Beware this version is likely going to depend on untagged versions of other packages.

## Introduction

To get started, try the following:
Expand All @@ -31,31 +29,19 @@ julia> @async Pages.start();
Listening on 0.0.0.0:8000...
~~~

This starts a server that is listening at http://localhost:8000 and exposes the `EndPoint` type with a few methods.
This starts a server that is listening at http://localhost:8000 and exposes the `Endpoint` type with a few methods.

To create an `EndPoint`, try:
To create an `Endpoint`, try:

~~~julia
julia> Endpoint("/hello") do request::Request
"Hello world"
"Hello world"
end
Endpoint created at /hello.
~~~

This creates a web page at http://localhost:8000/hello that says `Hello world`.

You can see all current endpoints in

~~~julia
julia> Pages.pages
Dict{String,Pages.Endpoint} with 2 entries:
"/hello" => Endpoint created at /hello.
"/pages.js" => Endpoint created at /pages.js.
~~~

Note there are two endpoints already. The one we just added plus `/pages.js`. This endpoint is special and is part of Pages. It contains the JavaScript library that allows interaction between Julia and the browser. We'll discuss this in more detail below.

One nice thing about using `Pages` is that we can create pages whenever and wherever we want in our Julia code.
One nice thing about using `Pages` is that we can create pages whenever and wherever we want in our Julia code even while the server is running.

## Examples

Expand All @@ -65,19 +51,19 @@ There are a few examples included.
julia> Pages.examples()
~~~

This will start a server and launch a browser open to a page with links to some examples.
This will start a server and launch a browser open to a page with links to some simple examples.

Current examples include:

- `Plotly` - Dynamically insert plotly.js plots into a browser
- `Requests` - Send GET and POST requests from the browser to Julia and print the contents to the REPL.
- `Blank Page` - You can use this for experimemntation, e.g. use `Pages.add_library` to insert your favorite JavaScript library.
- `Blank` - You can use this for experimemntation, e.g. use `Pages.add_library` to insert your favorite JavaScript library.

You can reconstruct the `Plotly` example from the `Blank Page` via:

```julia
> Pages.add_library("https://cdn.plot.ly/plotly-1.16.1.min.js")
> Pages.add_library("https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.1/d3.min.js")
> Pages.add_library("/libs/plotly/1.16.1/plotly.min.js")
> Pages.add_library("/libs/d3/4.2.1/d3.min.js")
> Pages.example_plotly()
```

Expand Down
37 changes: 23 additions & 14 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- julia_version: 1
- julia_version: nightly

platform:
- x86 # 32-bit
- x64 # 64-bit

# # Uncomment the following lines to allow failures on nightly julia
# # (tests will run but not make your overall status red)
# matrix:
# allow_failures:
# - julia_version: nightly

branches:
only:
Expand All @@ -15,19 +25,18 @@ notifications:
on_build_status_changed: false

install:
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
# Download most recent Julia Windows binary
- ps: (new-object net.webclient).DownloadFile(
$env:JULIA_URL,
"C:\projects\julia-binary.exe")
# Run installer silently, output to C:\projects\julia
- C:\projects\julia-binary.exe /S /D=C:\projects\julia
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1"))

build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo();
Pkg.clone(pwd(), \"Pages\"); Pkg.build(\"Pages\")"
- echo "%JL_BUILD_SCRIPT%"
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"

test_script:
- C:\projects\julia\bin\julia --check-bounds=yes -e "Pkg.test(\"Pages\")"
- echo "%JL_TEST_SCRIPT%"
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"

# # Uncomment to support code coverage upload. Should only be enabled for packages
# # which would have coverage gaps without running on Windows
# on_success:
# - echo "%JL_CODECOV_SCRIPT%"
# - C:\julia\bin\julia -e "%JL_CODECOV_SCRIPT%"
File renamed without changes.
15 changes: 15 additions & 0 deletions examples/blank.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Endpoint("/examples/blank") do request::HTTP.Request
read(joinpath(@__DIR__,"blank.html"),String)
end

function example_comms(nwin)
for i = 1:nwin
Pages.launch("http://localhost:$(Pages.port)/examples/blanks")
wait(Pages.conditions["connected"])
end

Pages.broadcast("/examples/blank","say","Hello everyone!")
for (sid,s) in Pages.connections["/examples/blank"]
Pages.message("/examples/comms",sid,"say","You are connection #$(sid).")
end
end
2 changes: 1 addition & 1 deletion examples/examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ul>
<li><a href="/examples/plot.ly">Plotly</a></li>
<li><a href="/examples/requests">Requests</a></li>
<li><a href="/examples/pages">Blank page</a></li>
<li><a href="/examples/blank">Blank</a></li>
</ul>

</body>
Expand Down
30 changes: 12 additions & 18 deletions examples/examples.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
Endpoint("/examples") do request::HTTP.Request
readstring(joinpath(dirname(@__FILE__),"examples.html"))
end

Endpoint("/examples/pages") do request::HTTP.Request
readstring(joinpath(dirname(@__FILE__),"pages.html"))
end

include("plotly.jl")
include("requests.jl")
# include("mwe.jl")

function examples()

@async Pages.start()
sleep(2.0)
Pages.launch("http://localhost:$(Pages.port)/examples")

end
Endpoint("/examples") do request::HTTP.Request
read(joinpath(@__DIR__,"examples.html"),String)
end

include(joinpath(@__DIR__,"plotly.jl"))
include(joinpath(@__DIR__,"requests.jl"))
include(joinpath(@__DIR__,"blank.jl"))
# include("mwe.jl")

@async Pages.start()
Pages.launch("http://localhost:$(Pages.port)/examples")
end
4 changes: 2 additions & 2 deletions examples/mwe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end

@async HTTP.listen(ip"127.0.0.1", 8081,;
ssl = true,
sslconfig = MbedTLS.SSLConfig(joinpath(dirname(@__FILE__),"cert.pem"), joinpath(dirname(@__FILE__),"key.pem"))) do http
sslconfig = MbedTLS.SSLConfig(joinpath(@__DIR__,"cert.pem"), joinpath(@__DIR__,"key.pem"))) do http
if HTTP.WebSockets.is_upgrade(http.message)

HTTP.WebSockets.upgrade(http) do client
Expand All @@ -31,7 +31,7 @@ end
end
else
HTTP.Servers.handle_request(http) do req::HTTP.Request
HTTP.Response(200,readstring(joinpath(dirname(@__FILE__),"mwe.html")))
HTTP.Response(200,read(joinpath(@__DIR__,"mwe.html"),String))
end
end
end
Expand Down
24 changes: 0 additions & 24 deletions examples/pages.jl

This file was deleted.

6 changes: 3 additions & 3 deletions examples/plotly.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using PlotlyBase

Endpoint("/libs/plotly/1.16.1/plotly.min.js") do request::HTTP.Request
readstring(joinpath(dirname(@__FILE__),"libs","plotly","v1.16.1","plotly.min.js"))
read(joinpath(@__DIR__,"libs","plotly","v1.16.1","plotly.min.js"),String)
end

Endpoint("/libs/d3/4.2.1/d3.min.js") do request::HTTP.Request
readstring(joinpath(dirname(@__FILE__),"libs","d3","v4.2.1","d3.min.js"))
read(joinpath(@__DIR__,"libs","d3","v4.2.1","d3.min.js"),String)
end

Endpoint("/examples/plot.ly") do request::HTTP.Request
readstring(joinpath(dirname(@__FILE__),"plotly.html"))
read(joinpath(@__DIR__,"plotly.html"),String)
end

function example_plotly()
Expand Down
28 changes: 16 additions & 12 deletions examples/requests.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
using JSON

Endpoint("/examples/requests") do request::HTTP.Request
readstring(joinpath(dirname(@__FILE__),"requests.html"))
read(joinpath(@__DIR__,"requests.html"),String)
end

Endpoint("/examples/requests/echo") do request::HTTP.Request
if request.method == "GET"
params = HTTP.queryparams(HTTP.URI(request.target).query)
println("Body: $(params)")
response = JSON.json(params)
elseif request.method == "POST"
data = String(request.body)
println("Parameters: $(data)")
response = JSON.json(Dict(:data => data))
end
response
Get("/examples/requests/echo") do request::HTTP.Request
params = HTTP.queryparams(HTTP.URI(request.target).query)
println("Body: $(params)")
response = JSON.json(params)
end

Post("/examples/requests/echo") do request::HTTP.Request
data = String(request.body)
println("Parameters: $(data)")
response = JSON.json(Dict(:data => data))
end

Get("/examples/variable/users/*") do request::HTTP.Request
name = replace(request.target,"/examples/variable/users/"=>"")
"Hi $(name)!"
end

# Endpoint("/api/math/rand") do request::HTTP.Request
Expand Down
36 changes: 19 additions & 17 deletions src/Pages.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
__precompile__()

module Pages

using HTTP, JSON
using HTTP, JSON, Sockets

import HTTP.WebSockets.WebSocket

export Endpoint, Callback, Plotly
export Endpoint, Get, Post, Callback

const router = Ref{HTTP.Router}()

mutable struct Endpoint
struct Endpoint
handler::Function
method::String
route::String
sessions::Dict{String,WebSocket}

function Endpoint(handler,route)
p = new(handler,route,Dict{String,WebSocket}())
!haskey(pages,route) || warn("Page $route already exists.")
pages[route] = p
finalizer(p, p -> delete!(pages, p.route))
p

function Endpoint(handler,method,route)
HTTP.register!(router[],method,route,HTTP.HandlerFunction(handler))
new(handler,method,route)
end

Endpoint(handler,route) = Endpoint(handler,"",route)
end
function Base.show(io::Base.IO,endpoint::Endpoint)
print(io,"Endpoint created at $(endpoint.route).")
end
const pages = Dict{String,Endpoint}() # url => page

Get(handler,route) = Endpoint(handler,"GET",route)
Post(handler,route) = Endpoint(handler,"POST",route)

include("callbacks.jl")
include("server.jl")
Expand All @@ -34,4 +32,8 @@ include("displays/plotly.jl")

include("../examples/examples.jl")

function __init__()
router[] = HTTP.Router()
end

end
Loading

0 comments on commit 0dda5a5

Please sign in to comment.