Skip to content

Commit

Permalink
v0.22.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian committed Dec 24, 2019
1 parent 4fa0288 commit 139516c
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 189 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.22.9 - 2019-12-24

* enhanced support for WebChannels and websockets operations

## v0.22.8 - 2019-12-05

* more tests
Expand Down
12 changes: 6 additions & 6 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ version = "0.21.0"

[[JuliaInterpreter]]
deps = ["CodeTracking", "InteractiveUtils", "Random", "UUIDs"]
git-tree-sha1 = "0bf5e88aa07b9ffe36dca3215642d5d1ea2901cc"
git-tree-sha1 = "cf43000752f15c92bef4a080966810de886f3560"
uuid = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"
version = "0.7.5"
version = "0.7.6"

[[LibGit2]]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
Expand Down Expand Up @@ -153,9 +153,9 @@ uuid = "739be429-bea8-5141-9913-cc70e7f3736d"
version = "0.7.0"

[[Millboard]]
git-tree-sha1 = "c6d4a61d1b74ba35b1efe5cb025bf852145521e1"
git-tree-sha1 = "19b2fe6b9cdef06291f66692335ec25fd2e1f037"
uuid = "39ec1447-df44-5f4c-beaa-866f30b4d3b2"
version = "0.2.1"
version = "0.2.2"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
Expand Down Expand Up @@ -207,9 +207,9 @@ version = "0.2.0"

[[Revise]]
deps = ["CodeTracking", "Distributed", "FileWatching", "JuliaInterpreter", "LibGit2", "LoweredCodeUtils", "OrderedCollections", "Pkg", "REPL", "UUIDs", "Unicode"]
git-tree-sha1 = "855751b1fc9337d8cbe1e6d80ab6aa948a004a7e"
git-tree-sha1 = "2ecbd19f31a934b035bfc38036d5f7ac575d0878"
uuid = "295af30f-e4ad-537b-8983-00126c2a3abe"
version = "2.3.2"
version = "2.5.0"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Genie"
uuid = "c43c736e-a2d1-11e8-161f-af95117fbd1e"
authors = ["Adrian Salceanu <[email protected]>"]
version = "0.22.8"
version = "0.22.9"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Expand Down
13 changes: 1 addition & 12 deletions files/new_app/assets/js/application.js
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
require("../css/application.css");

const WEBSOCKETS_SERVER = false;

if ( WEBSOCKETS_SERVER ) {
require("./channels.js");
WebChannels.load_channels();

WebChannels.messageHandlers.push(function(event){
console.log(event.data);
});
}
require("../css/application.css");
70 changes: 0 additions & 70 deletions files/new_app/assets/js/channels.js

This file was deleted.

34 changes: 32 additions & 2 deletions files/new_app/public/js/app/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ window.WebChannels.load_channels = function() {

// A message maps to a channel route so that channel + message = /action/controller
// The payload is the data made exposed in the Channel Controller
function sendMessageTo(channel, message, payload) {
function sendMessageTo(channel, message, payload = {}) {
if (socket.readyState === 1) {
socket.send(JSON.stringify({
'channel': channel,
Expand All @@ -74,9 +74,39 @@ WebChannels.load_channels();
WebChannels.messageHandlers.push(function(event) {
console.log(event.data);
});

WebChannels.messageHandlers.push(function(event){
try {
if (event.data.startsWith('{') && event.data.endsWith('}')) {
window.parse_payload(JSON.parse(event.data));
} else {
window.parse_payload(event.data);
}
} catch (ex) {
console.log(ex);
}
});

WebChannels.errorHandlers.push(function(event) {
console.log(event.data);
});

WebChannels.closeHandlers.push(function(event) {
console.log("Server closed WebSocket connection");
});
});

function parse_payload(json_data) {
console.log("Overwrite window.parse_payload to handle messages from the server")
console.log(json_data);
};

// subscribe
function subscribe() {
if (document.readyState === "complete" || document.readyState === "interactive") {
WebChannels.sendMessageTo("__", "subscribe");
console.log("Subscription ready");
} else {
console.log("Queuing subscription");
setTimeout(subscribe, 1000);
}
};
71 changes: 7 additions & 64 deletions src/AppServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ Handles Http server related functionality, manages requests and responses and th
"""
module AppServer

import Revise, HTTP, HTTP.IOExtras, HTTP.Sockets, Millboard, MbedTLS, URIParser, Sockets, Distributed, Logging
import Genie, Genie.Configuration, Genie.Sessions, Genie.Flax, Genie.Router, Genie.WebChannels, Genie.Exceptions
import Revise
import HTTP, HTTP.IOExtras, HTTP.Sockets
import Millboard, MbedTLS, URIParser, Sockets, Distributed, Logging
import Genie, Genie.Configuration, Genie.Sessions, Genie.Flax, Genie.Router, Genie.WebChannels, Genie.Exceptions, Genie.Headers

mutable struct ServersCollection
webserver::Union{Task,Nothing}
Expand Down Expand Up @@ -72,88 +74,29 @@ function startup(port::Int = Genie.config.server_port, host::String = Genie.conf
end


"""
set_headers!(req::HTTP.Request, res::HTTP.Response, app_response::HTTP.Response) :: HTTP.Response
Configures the response headers.
"""
function set_headers!(req::HTTP.Request, res::HTTP.Response, app_response::HTTP.Response) :: HTTP.Response
if req.method == Genie.Router.OPTIONS || req.method == Genie.Router.GET

request_origin = get(Dict(req.headers), "Origin", "")

allowed_origin_dict = Dict("Access-Control-Allow-Origin" =>
in(request_origin, Genie.config.cors_allowed_origins)
? request_origin
: strip(Genie.config.cors_headers["Access-Control-Allow-Origin"])
)

app_response.headers = [d for d in merge(Genie.config.cors_headers, allowed_origin_dict, Dict(res.headers), Dict(app_response.headers))]
end

app_response.headers = [d for d in merge(Dict(res.headers), Dict(app_response.headers))]

app_response
end


"""
sign_response!(res::HTTP.Response) :: HTTP.Response
Adds a signature header to the response using the value in `Genie.config.server_signature`.
If `Genie.config.server_signature` is empty, the header is not added.
"""
function sign_response!(res::HTTP.Response) :: HTTP.Response
headers = Dict(res.headers)
isempty(Genie.config.server_signature) || (headers["Server"] = Genie.config.server_signature)

res.headers = [k for k in headers]
res
end


"""
handle_request(req::HTTP.Request, res::HTTP.Response, ip::IPv4 = IPv4(Genie.config.server_host)) :: HTTP.Response
Http server handler function - invoked when the server gets a request.
"""
function handle_request(req::HTTP.Request, res::HTTP.Response, ip::Sockets.IPv4 = Sockets.IPv4(Genie.config.server_host)) :: HTTP.Response
isempty(Genie.config.server_signature) && sign_response!(res)
isempty(Genie.config.server_signature) && Headers.sign_response!(res)

try
req = normalize_headers(req)
req = Headers.normalize_headers(req)
catch ex
@error ex
end

try
set_headers!(req, res, Genie.Router.route_request(req, res, ip))
Headers.set_headers!(req, res, Genie.Router.route_request(req, res, ip))
catch ex
@error ex
rethrow(ex)
end
end


function normalize_headers(req::HTTP.Request) :: HTTP.Request
headers = Dict(req.headers)
normalized_headers = Dict{String,String}()

for (k,v) in headers
normalized_headers[normalize_header_key(string(k))] = string(v)
end

req.headers = [k for k in normalized_headers]

req
end


function normalize_header_key(key::String) :: String
join(map(x -> uppercasefirst(lowercase(x)), split(key, '-')), '-')
end


"""
setup_http_handler(req::HTTP.Request, res::HTTP.Response = HTTP.Response()) :: HTTP.Response
Expand Down
44 changes: 43 additions & 1 deletion src/Assets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Helper functions for working with frontend assets (including JS, CSS, etc files)
"""
module Assets

import Genie, Genie.Configuration
import Revise
import Genie, Genie.Configuration, Genie.Router, Genie.WebChannels

export include_asset, css_asset, js_asset

Expand Down Expand Up @@ -49,4 +50,45 @@ function js_asset(file_name::String; fingerprinted::Bool = Genie.config.assets_f
include_asset(:js, file_name, fingerprinted = fingerprinted)
end


"""
embeded(path::String) :: String
Reads and outputs the file at `path` within Genie's root package dir
"""
function embedded(path::String) :: String
read(joinpath(@__DIR__, "..", path) |> normpath, String)
end


"""
channels() :: String
Outputs the channels.js file included with the Genie package
"""
function channels() :: String
embedded(joinpath("files", "new_app", "public", "js", "app", "channels.js"))
end


function channels_script() :: String
"""
<script>
$(channels())
</script>
"""
end


function channels_support() :: String
Router.route("/__/channels.js", channels)

Router.channel("/__/subscribe") do
WebChannels.subscribe(Genie.Requests.wsclient(), "__")
"OK"
end

"<script src=\"/__/channels.js\"></script>"
end

end
2 changes: 1 addition & 1 deletion src/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Core genie configuration / settings functionality.
"""
module Configuration

const GENIE_VERSION = v"0.22.8"
const GENIE_VERSION = v"0.22.9"

import Logging
import Genie
Expand Down
3 changes: 2 additions & 1 deletion src/Genie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ include("Sessions.jl")
include("Input.jl")
include("Flax.jl")
include("Renderer.jl")
include("Assets.jl")
include("Router.jl")
include("WebChannels.jl")
include("Headers.jl")
include("Assets.jl")
include("AppServer.jl")
include("Commands.jl")
include("Cache.jl")
Expand Down
Loading

2 comments on commit 139516c

@essenciary
Copy link
Member

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/7120

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 Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.22.9 -m "<description of version>" 139516c8f3116628d2ea2e525c4f1e19902992ed
git push origin v0.22.9

Please sign in to comment.