Genie.jl is the core of the Genie Framework with which you can build web apps and backends using Julia. It offers a built-in web server and a flexible system for creating web pages using HTML, JSON, Markdown, or Julia code.
Genie.jl makes your apps fast with built-in caching, keeps user data secure with encrypted cookies and sessions, and helps organize your app with a powerful router for easy navigation. It's a one-stop-shop for developers looking to build efficient and secure web applications with Julia, making the process straightforward and enjoyable.
Genie.jl you can create pages with dynamic content generated by Julia code. If you'd like to go further and add reactive UIs, check out the
Stippe.jl package which is built on top of
To install Genie, open a Julia REPL, enter Pkg mode with
] and type
pkg> add Genie
The simplest use case is to configure a routing function at the REPL and start the web server. That's all that's needed to run your code on the web page:
julia> using Genie julia> route("/hello") do "Hello World" end julia> up()
route function defines a mapping between a URL (
"/hello") and a Julia function (a handler) which will be
automatically invoked to send the response back to the client. In this case we're sending back the string "Hello World".
That's all! We have set up an app, a route, and started the web server. Open your favorite web browser and go to http://127.0.0.1:8000/hello to see the result.
Keep in mind that Julia JIT-compiles. A function is automatically compiled the first time it is invoked. The function, in this case, is our route handler that is responding to the request. This will make the first response slower as it also includes compilation time. But once the function is compiled, for all the subsequent requests, it will be super fast!
Genie can also be used in custom scripts, for example when building micro-services with Julia. Let's create a simple "Hello World" micro-service.
Start by creating a new file to host our code -- let's call it
Now, open it in the editor:
Add the following code:
using Genie, Genie.Renderer, Genie.Renderer.Html, Genie.Renderer.Json route("/hello.html") do html("Hello World") end route("/hello.json") do json("Hello World") end route("/hello.txt") do respond("Hello World", :text) end up(8001, async = false)
We begun by defining 2 routes and we used the
json rendering functions (available in the
Renderer.Json modules). These functions are responsible for outputting the data using the correct format and
document type (with the correct MIME), in our case HTML data for
hello.html, and JSON data for
route serves text responses. As Genie does not provide a specialized
text() method for sending
responses, we use the generic
respond function, indicating the desired MIME type. In our case
text/plain. Other available MIME types shortcuts are
and users can register their own mime types and response types as needed or can pass the full mime type as a string,
up function will launch the web server on port
8001. This time, very important, we instructed it to start the
server synchronously (that is, blocking the execution of the script), by passing the
async = false argument.
This way we make sure that our script stays running. Otherwise, at the end of the script, the Julia process would normally exit,
killing our server.
In order to launch the script, run
$ julia geniews.jl.
Genie readily makes available a rich set of features - you have already seen the rendering and the routing modules in action. But for instance, logging (to file and console) can also be easily triggered with one line of code, powerful caching can be enabled with a couple more lines, and so on.
The app already handles "404 Page Not Found" and "500 Internal Error" responses. If you try to access a URL which is not handled by the app, like say http://127.0.0.1:8001/not_here, you'll see Genie's default 404 page. The default error pages can be overwritten with custom ones.