routing

Toolips Routes are structures that contain both a Function and a String. The String represents the URL from root that we wish to serve the Function at. The Function takes a single positional argument. The argument can be any sort of AbstractConnection, unless specified otherwise by your ServerTemplate, this will be of type Connection.

Toolips.RouteType

Route

  • path::String
  • page::Function -

A route is added to a ServerTemplate using either its constructor, or the ServerTemplate.add(::Route) method. Each route calls a function. The Route type is commonly constructed using the do syntax with the route(::Function, ::String) method.

example

# Constructors
route = Route("/", p(text = "hello"))

function example(c::Connection)
    write!(c, "hello")
end

route = Route("/", example)

# method
route = route("/") do c
    write!(c, "Hello world!")
    write!(c, p(text = "hello"))
    # we can also use extensions!
    c[:logger].log("hello world!")
end

field info

  • path::String - The path to route to the function, e.g. "/".
  • page::Function - The function to route the path to.

constructors

  • Route(path::String, f::Function)

You can create a new route by either writing a function, or using the route method. When using the latter approach, writing a function, it is important to remember that you will still need to call the route method after. Route functions and strings can be provided in any order to the route method.

using Toolips

function myroutef(c::Connection)
  write!(c, "hello!")
end

myroute = route("/", myroutef)
otherroute = route("/otherroute") do c::Connection
  write!(c, "goodbye!")
end
Toolips.routeFunction

Interface

route(f::Function, r::String) -> ::Route


Creates a route from the Function. The function should take a Connection or AbstractConnection as a single positional argument.

example

route("/") do c::Connection

end

Interface

route(r::String, f::Function) -> ::Route


Creates a route from the Function. The function should take a Connection or AbstractConnection as a single positional argument.

example

function example(c::Connection)
    write!(c, h("myh", 1, text = "hello!"))
end
r = route("/", example)

composing routes

Routes can be composed into a Vector{Route} using the routes method. The ServerTemplate type, which utilizes our routes, will only take a Vector{Route}, so it is important that we compose routes using either the routes method, or by calling the Vector{Route} constructor.

using Toolips

function myroutef(c::Connection)
  write!(c, "hello!")
end

myroute = route("/", myroutef)
otherroute = route("/otherroute") do c::Connection
  write!(c, "goodbye!")
end

myroutes = Vector{Route}(myroute, otherroute)
myroutes = routes(myroute, otherroute)

serving routes

Routes are served by the ServerTemplate type. We can either provide routes to the constructor, or add them individually with ServerTemplate.add.

using Toolips

function myroutef(c::Connection)
  write!(c, "hello!")
end

myroute = route("/", myroutef)
otherroute = route("/otherroute") do c::Connection
  write!(c, "goodbye!")
end

myroutes = routes(myroute)

st = ServerTemplate("127.0.0.1", 8000, myroutes)
st.add(otherroute)

We can then begin the serving of these routes with ServerTemplate.start:

st.start()

Routes can also be removed using the unroute! method.

st = ServerTemplate("127.0.0.1", 8000, myroutes)
st.add(otherroute)
st.remove("/otherroute")

changing routes

Routes can also be modified via the command-line interface while the WebServer is running. This can be done both inside of a route on a Connection, as well as inside of a Julia REPL on a WebServer

using Toolips

function myroutef(c::Connection)
  # Change the route via setindex!:
  c["/"] = otherroute
  # Change the route via route!:
  route!(c, "/") do c::Connection
    write!(c, "goodbye!")
  end
  route!(c, otherroute)
end

function otherroute(c::Connection)
  write!(c, "goodbye!")
end

myroute = route("/", myroutef)
myroutes = routes(myroute)

st = ServerTemplate("127.0.0.1", 8000, myroutes)

We can also remove a route using the unroute! method:

function myroutef(c::Connection)
  # Change the route via setindex!:
  c["/"] = otherroute
  # Change the route via route!:
  route!(c, "/") do c::Connection
    write!(c, "goodbye!")
  end
end

The same technique can also be applied to a WebServer.

using Toolips

st = ServerTemplate("127.0.0.1", 8000, myroutes)
webserver = st.start()

route!(webserver, "/") do c::Connection
  write!(c, "I am a rerouted route!")
end