diff --git a/src/Kroki.jl b/src/Kroki.jl index 57b3dfb..e327a07 100644 --- a/src/Kroki.jl +++ b/src/Kroki.jl @@ -117,11 +117,16 @@ RenderError(::Diagram, exception::Exception) = exception """ Renders a [`Diagram`](@ref) through a Kroki service to the specified output format. + +A `KROKI_ENDPOINT` environment variable can be set, specifying the URI of a +specific instance of Kroki to use (e.g. when using a [privately hosted +instance](https://docs.kroki.io/kroki/setup/install/)). By default the +[publicly hosted service](https://kroki.io) is used. """ render(diagram::Diagram{T}, output_format::AbstractString) where T <: Val = try getfield( request("GET", join([ - "https://kroki.io", + get(ENV, "KROKI_ENDPOINT", "https://kroki.io"), lowercase("$(T.parameters[1])"), output_format, UriSafeBase64Payload(diagram) diff --git a/test/runtests.jl b/test/runtests.jl index f851ffd..baa3197 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -108,6 +108,30 @@ end InvalidOutputFormatError ) end + + @testset "`KROKI_ENDPOINT` environment variable" begin + # The instance of the Kroki service that is used for rendering can be + # controlled through the `KROKI_ENDPOINT` environment variable. The most + # straight-forward way for testing this is by pointing to an invalid + # endpoint and testing for a corresponding connection error + expected_diagram_type = :plantuml + expected_service_host = "https://localhost" + + withenv("KROKI_ENDPOINT" => expected_service_host) do + try + render(Diagram(expected_diagram_type, "A -> B: C"), "svg") + catch exception + buffer = IOBuffer() + showerror(buffer, exception) + rendered_buffer = String(take!(buffer)) + + @test occursin("ECONNREFUSED", rendered_buffer) + @test occursin( + "$(expected_service_host)/$(expected_diagram_type)", rendered_buffer + ) + end + end + end end end