Skip to content

Commit

Permalink
v0.31.5
Browse files Browse the repository at this point in the history
  • Loading branch information
essenciary committed Jul 1, 2020
1 parent 9d835e0 commit 715d3a4
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.31.5 - 2020-07-01

* fixed issue with emtpy SECRET_TOKEN
* Session improvements
* more tests
* Markdown views fixes
* fixed `Genie.serve` for serving static websites

## v0.31.4 - 2020-07-01

* fixed issue with cookie encryption/decryption
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.31.4"
version = "0.31.5"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Expand Down
3 changes: 0 additions & 3 deletions TODO.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Configuration
References the current Genie version number.
"""
const GENIE_VERSION = v"0.31.4"
const GENIE_VERSION = v"0.31.5"

import Logging
import Genie
Expand Down
11 changes: 10 additions & 1 deletion src/Encryption.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ Generates a pair of key32 and iv16 with salt for encryption/decryption
"""
function encryption_sauce() :: Tuple{Vector{UInt8},Vector{UInt8}}
if ! isdefined(Genie, :SECRET_TOKEN)
@warn "Encryption error"
@error "Encryption error: Genie.SECRET_TOKEN not defined"

if ! Genie.Configuration.isprod()
@warn "Generating temporary secret token"
Core.eval(Genie, :(const SECRET_TOKEN = $(Genie.Generator.secret_token())))
else
error("Can't encrypt - please make sure you run a Genie project and SECRET_TOKEN is defined in config/secrets.jl")
end
elseif length(Genie.SECRET_TOKEN) < 64
@error "Encryption error: Invalid Genie.SECRET_TOKEN"

if ! Genie.Configuration.isprod()
@warn "Regenerating temporary secret token"
Core.eval(Genie, :(SECRET_TOKEN = $(Genie.Generator.secret_token())))
else
error("Can't encrypt - please make sure you run a Genie project and SECRET_TOKEN is defined in config/secrets.jl")
end
end

passwd = Genie.SECRET_TOKEN[1:32]
Expand Down
5 changes: 4 additions & 1 deletion src/Genie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ julia> Genie.serve("public", 8888, async = false, verbose = true)
```
"""
function serve(path::String = Genie.config.server_document_root, params...; kwparams...)
cd(path)
path = "."

Router.route("/") do
Router.serve_static_file("index.html", root = path)
Router.serve_static_file(path, root = path)
end
Router.route(".*") do
Router.serve_static_file(Router.@params(:REQUEST).target, root = path)
Expand Down
13 changes: 8 additions & 5 deletions src/Router.jl
Original file line number Diff line number Diff line change
Expand Up @@ -999,16 +999,19 @@ function serve_static_file(resource::String; root = Genie.config.server_document
f = file_path(resource_path, root = root)

if isfile(f)
HTTP.Response(200, file_headers(f), body = read(f, String))
return HTTP.Response(200, file_headers(f), body = read(f, String))
elseif isdir(f)
isfile(joinpath(f, "index.html")) && return serve_static_file(joinpath(f, "index.html"), root = root)
isfile(joinpath(f, "index.htm")) && return serve_static_file(joinpath(f, "index.htm"), root = root)
else
bundled_path = joinpath(@__DIR__, "..", "files", "static", resource[2:end])
if isfile(bundled_path)
HTTP.Response(200, file_headers(bundled_path), body = read(bundled_path, String))
else
@error "404 Not Found $f"
error(resource, response_mime(), Val(404))
return HTTP.Response(200, file_headers(bundled_path), body = read(bundled_path, String))
end
end

@error "404 Not Found $f"
error(resource, response_mime(), Val(404))
end


Expand Down
2 changes: 1 addition & 1 deletion src/Sessions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ end
Checks wheter or not `key` exists on the `Session` `s`.
"""
function isset(s::Union{Session,Nothing}, key::Symbol) :: Bool
s != nothing && haskey(s.data, key)
s !== nothing && haskey(s.data, key)
end


Expand Down
18 changes: 13 additions & 5 deletions src/renderers/Html.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const SUPPORTED_HTML_OUTPUT_FILE_FORMATS = TEMPLATE_EXT
const HTMLString = String
const HTMLParser = Gumbo

const MD_SEPARATOR_START = "---\n"
const MD_SEPARATOR_END = "---\n"
const MD_SEPARATOR_START = "---"
const MD_SEPARATOR_END = "---"

const NBSP_REPLACEMENT = ("&nbsp;"=>"!!nbsp;;")

Expand Down Expand Up @@ -213,12 +213,20 @@ end
Converts the mardown `md` to HTML view code.
"""
function eval_markdown(md::String; context::Module = @__MODULE__) :: String
if startswith(md, MD_SEPARATOR_START)
if startswith(md, string(MD_SEPARATOR_START, "\n")) ||
startswith(md, string(MD_SEPARATOR_START, "\r")) ||
startswith(md, string(MD_SEPARATOR_START, "\r\n"))
close_sep_pos = findfirst(MD_SEPARATOR_END, md[length(MD_SEPARATOR_START)+1:end])
metadata = md[length(MD_SEPARATOR_START)+1:close_sep_pos[end]] |> YAML.load

for (k,v) in metadata
task_local_storage(:__vars)[Symbol(k)] = v
isa(metadata, Dict) || (@warn "\nFound Markdown YAML metadata but it did not result in a `Dict` \nPlease check your markdown metadata \n$metadata")

try
for (k,v) in metadata
task_local_storage(:__vars)[Symbol(k)] = v
end
catch ex
@error ex
end

md = replace(md[close_sep_pos[end]+length(MD_SEPARATOR_END)+1:end], "\"\"\""=>"\\\"\\\"\\\"")
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
26 changes: 26 additions & 0 deletions test/tests_Sessions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@testset "Sessions functionality" begin

@testset "Assets paths" begin
using Genie, Genie.Sessions
using Genie.Router
using HTTP

Sessions.init()

route("/home") do
sess = Sessions.session(Genie.Router.@params)
Sessions.set!(sess, :visit_count, Sessions.get(sess, :visit_count, 0)+1)

"$(Sessions.get(sess, :visit_count))"
end

Genie.up()

# TODO: extend to use the cookie and increment the count
response = HTTP.get("http://$(Genie.config.server_host):$(Genie.config.server_port)/home")
@test response.body |> String == "1"

Genie.down()
end;

end;
61 changes: 57 additions & 4 deletions test/tests_cache.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
@testset "Caching" begin
# @testset "Empty string" begin
# using Genie, Genie.Cache
using Genie, Genie.Cache

# @test String(r.body) == "<html><head></head><body></body></html>"
# end;
function f()
rand(1:1_000)
end

Genie.config.cache_duration = 0 # no caching

r0 = f()

r1 = withcache(:x) do
f()
end

@test r0 != r1 # because cache_duration == 0 so no caching

r2 = withcache(:x) do
f()
end

@test r1 != r2 # because cache_duration == 0 so no caching

Genie.config.cache_duration = 5 # cache for 5s

r1 = withcache(:x) do
f()
end

r2 = withcache(:x) do
f()
end

@test r1 == r2

r3 = withcache(:x, condition = false) do # disable caching cause ! condition
f()
end

@test r1 == r2 != r3

r4 = withcache(:x, 0) do # disable caching with 0 duration
f()
end

@test r1 == r2 != r3 != r4

r5 = withcache(:x) do # regular cache should still work as under 5s passed
f()
end

@test r1 == r2 == r5

sleep(6)

r6 = withcache(:x) do # regular cache should not work as over 5s passed
f()
end

@test r1 == r2 == r5 != r6
end;
7 changes: 7 additions & 0 deletions test/tests_markdown_rendering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@
"<html><head></head><body><div><h1>Layout header</h1><section><h1>There are 7</h1><p>-> 1 -> 1 -> 2 -> 3 -> 5 -> 8 -> 13</p></section><footer><h4>Layout footer</h4></footer></div></body></html>"
end

@safetestset "Markdown rendering with embedded variables" begin
using Genie, Genie.Renderer
using Genie.Renderer.Html

@test Html.html(filepath("views/view-vars.jl.md")).body |> String == "<html><head></head><body><h1>There are 7</h1><p>-> 1 -> 1 -> 2 -> 3 -> 5 -> 8 -> 13</p></body></html>"
end;

end;
12 changes: 12 additions & 0 deletions test/views/view-vars.jl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
numbers: [1, 1, 2, 3, 5, 8, 13]
---

# There are $(length(numbers))

$(
@foreach(numbers) do number
" -> $number
"
end
)

2 comments on commit 715d3a4

@essenciary
Copy link
Member 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/17296

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 v0.31.5 -m "<description of version>" 715d3a422458d1af3b08c482d979d684fac4e6d9
git push origin v0.31.5

Please sign in to comment.