Julia Community 🟣

Cover image for Oxygen.jl: A breath of fresh air for programming web apps in Julia
Nathan Ortega
Nathan Ortega

Posted on

Oxygen.jl: A breath of fresh air for programming web apps in Julia

I'm excited to announce Oxygen.jl, a micro-framework built on top of the HTTP.jl library that's designed to simplify building web applications in Julia.

using Oxygen
using HTTP

@get "/greet" function(req::HTTP.Request)
    return "hello world!"
end

# both path parameters are automatically converted to Float64's 
@get "/multiply/{a}/{b}" function(req, a::Float64, b::Float64)
    return a * b
end

# all objects are automatically deserialized into JSON
@get "/getdata" function()
    return Dict("message" => "hello!")
end

# mount all nested files inside the "content" folder under "/static"
@staticfiles "content" "static"

# start the web server
serve()
Enter fullscreen mode Exit fullscreen mode

Features

  • Straightforward routing (@get, @post, @put, @patch, @delete and @route macros)
  • Out-of-the-box JSON serialization & deserialization
  • Optional type definition support for Path parameters
  • Helper functions to parse & transform the body of requests & responses
  • Hosting static files
  • Built-in multithreading support

Why build another web framework?

That's a great question, why create another package if Julia already has full stack frameworks like Genie.jl that can do just about anything?

My quick answer: most people don't need all features that come with these heavyweight packages. About a month ago, I was one of those people looking to quickly spin up a web server to prototype some ideas.

I wanted something lightweight and straightforward, and I didn't want to spend a lot of time learning a new syntax. Ideally, I wanted something similar to Flask or FastApi but in Julia.

So, I set out to create this package and design a no-nonsense api that would hopefully feel familiar to JS and Python programmers and drive greater adoption in these communities.

Feature Requests

If you have any ideas or feature requests, please feel free to open an issue on the github page!

Top comments (13)

Collapse
 
ifihan profile image
Ifihanagbara Olusheye • Edited

This is nice. The first thing that came to mind was this is Flask but for Julia and I'm happy I saw it in the post. Great work 😄

I'll try it out and if I find have any questions, I will ask

Collapse
 
cjdoris profile image
Christopher Rowley

Looks cool, I too have wanted "FastAPI for Julia".

Collapse
 
logankilpatrick profile image
Logan Kilpatrick

Same here, Jeremey Howard (creator of FastAI) has said many times that having things like FastAPI is critical for Julia's long term success so I am excited to see this!

Collapse
 
melonedo profile image
melonedo • Edited

I am so exited to see that Julia finally have a web server that does not heavily rely on global states! It feels much more "Julian" than existing frameworks.

By the way, I am very interested in how you implement the @route macro, because it does not seem obvious to me how to manipulate a function by its signature. In python it is done with a special annotation system, how do you do that in Julia?

Collapse
 
ndortega profile image
Nathan Ortega

That's a great question! And one that was difficult for me to figure out as well. It involved a lot of googling and reading into the Base library documentation

Essentially, there's a low-level api just for methods that's pretty good at extracting useful information. I was able to extract the number of function arguments and their types, but was unable to get the name of each variable.

I found a snippet in the julia forums that did exactly what I needed and modified it a bit to return strings instead of symbols.

(get parameter names)
github.com/ndortega/Oxygen.jl/blob...

(get parameter types & count)
github.com/ndortega/Oxygen.jl/blob...

(low-level api)
docs.julialang.org/en/v1/base/base...

Collapse
 
emmanuel_r8 profile image
Emmanuel_R8

Great work! Looks almost as easy as the R Plumber framework. Almost, because Plumber automatically generates the Swagger description.

I have no idea about the amount of work that would be involved, but does an integration with the Genie/Stipple framework make any sense?

Collapse
 
emmy profile image
emmy

cool

Collapse
 
abhimanyuaryan profile image
abhimanyuaryan

so nice Nathan congrats on making an awesome package :)

Collapse
 
imresamu profile image
ImreSamu

Please add a benchmark :-)

to -> github.com/TechEmpower/FrameworkBe...

Collapse
 
edwinlock profile image
Edwin Lock

Very nice. Do you happen to know whether there's a templating engine (like Jinja for flask) that can be used in combination with Oxygen?

Collapse
 
ndortega profile image
Nathan Ortega

There's a package called Mustache.jl which is a generic templating engine. They should be compatible, but I haven't tested this yet. Let me know if you run into any issues.

github.com/jverzani/Mustache.jl

Collapse
 
ericforgy profile image
Eric Forgy

Neat 👍

Your motivation was the same as mine when I wrote Pages.jl. Have you seen it?

Collapse
 
ndortega profile image
Nathan Ortega

Up until now, I have not! But that's because the prototype I was working on a month ago was supposed to be a simple REST api with no front-end. I had no plans to add websockets to communicate with the browser