Skip to content

Embedding routes within a module via __init__() #73

@mkitti

Description

@mkitti

On the forums, I encountered some confusion about a @get route at the top level of a package module:
https://discourse.julialang.org/t/difference-between-include-and-using/87795

The problem with putting the routes at the top level of the module is that this is only invoked during precompilation. It is not invoked when the module is loaded.

For the routes to be loaded at module initialization, the routes should be declared from __init__ or some function called from __init__():

module OxygenExample
using Oxygen
using HTTP


function runserver()
    serve()
end

function __init__()
    @get "/greet" function(req::HTTP.Request)
        return "hello world!"
    end
    # routes may also be contained in src/main.jl
    include(joinpath(@__DIR__,"main.jl"))
end

end # module OxygenExample

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions