servers

ToolipsServers are created by ServerTemplates. The main type of ToolipsServer is the WebServer, which is provided as a return from the ServerTemplate.start() function.

server templates

Toolips.ServerTemplateType

ServerTemplate

  • ip::String
  • port::Integer
  • routes::Vector{Route}
  • extensions::Dict
  • remove::Function
  • add::Function
  • start::Function -

The ServerTemplate is used to configure a server before running. These are usually made and started inside of a main server file.

example

st = ServerTemplate()

webserver = ServerTemplate.start()

field info

  • ip::String - IP the server should serve to.
  • port::Integer - Port to listen on.
  • routes::Vector{Route} - A vector of routes to provide to the server
  • extensions::Vector{ServerExtension} - A vector of extensions to load into

the server.

  • remove(::Int64)::Function - Removes routes by index.
  • remove(::String)::Function - Removes routes by name.
  • remove(::Symbol)::Function - Removes extension by Symbol representing

type, e.g. :Logger

  • add(::Route ...)::Function - Adds the routes to the server.
  • add(::ServerExtension ...)::Function - Adds the extensions to the server.
  • start()::Function - Starts the server.

constructors

  • ServerTemplate(ip::String = "127.0.0.1", port::Int64 = 8001, routes::Vector{Route} = Vector{Route}()); extensions::Vector{ServerExtension} = [Logger()] connection::Type)

toolips servers

The ServerTemplate.start() function returns a sub-type of ToolipsServer, usually a WebServer.

Toolips.ToolipsServerType

abstract type ToolipsServer

ToolipsServers are returned whenever the ServerTemplate.start() field is called. If you are running your server as a module, it should be noted that commonly a global start() method is used and returns this server, and dev is where this module is loaded, served, and revised.

Consistencies

  • routes::Dict - The server's route => function dictionary.
  • extensions::Dict - The server's currently loaded extensions.
  • server::Any - The server, whatever type it may be...

The WebServer type is similar to a Connection in that it can be routed, and holds the Connection extensions. This type is useful for when we want to control our server from a command-line interface in our Julia REPL.

using Toolips

myroute = route("/") do c::Connection
  myp = p("myp", text = "Hello world!")
  mydiv = divider("mydiv")
  push!(mydiv, myp)
  write!(c, mydiv)
end

st = ServerTemplate()
st.add(myroute)
ourwebserver = st.start()
Toolips.WebServerType

WebServer <: ToolipsServer

  • host::String
  • routes::Dict
  • extensions::Dict
  • server::Any -

A web-server is given as a return from a ServerTemplate whenever ServerTemplate.start() is ran. It can be rerouted with route! and indexed similarly to the Connection, with Symbols representing extensions and Strings representing routes.

example

st = ServerTemplate()
ws = st.start()
routes(ws)
...
extensions(ws)
...
route!(ws, "/") do c::Connection
    write!(c, "hello")
end

We can call the routes and extensions methods on a WebServer, just like a Connection

Toolips.routesMethod

Interface

routes(ws::WebServer) -> ::Dict{String, Function}


Returns the server's routes.

example

ws = MyProject.start()
routes(ws)
    "/" => home
    "404" => fourohfour
Toolips.extensionsMethod

Interface

extensions(ws::WebServer) -> ::Dict{Symbol, ServerExtension}


Returns the server's extensions.

example

ws = MyProject.start()
extensions(ws)
    :Logger => Logger(blah blah blah)

Similarly, we can index our WebServer with a Symbol, or use the route! method in the same manor as we would a Connection.

using Toolips

myroute = route("/") do c::Connection
  myp = p("myp", text = "Hello world!")
  mydiv = divider("mydiv")
  push!(mydiv, myp)
  write!(c, mydiv)
end

st = ServerTemplate()
st.add(myroute)
ourwebserver = st.start()

ourwebserver[:Logger].log("Hello!")
route!(ourwebserver, "/") do c::Connection
  write!(c, p("myp", text = "our new route"))
end
function newr(c::Connection)
  write!(c, "hello")
end
route!(ourwebserver, "/", newr)
Base.getindexMethod

Interface

getindex(ws::WebServer, s::Symbol) -> ::ServerExtension


Indexes the extensions in ws.

example

ws = MyProject.start()
ws[:Logger].log("hi")

server extensions

A [ServerExtension] is an abstract type that can be used to add new capabilities to a ToolipsServer. These extensions are provided in a Vector{ServerExtension} to the ServerTemplate. We can create this Vector by simply putting a list of extensions together.

using Toolips

extensions = [Logger(), Files()]

Toolips includes two extensions by default, the Files extension and the Logger extensions. This Vector is provided as a key-word argument to the ServerTemplate constructor.

using Toolips

extensions = [Logger(), Files()]

st = ServerTemplate(extensions = extensions)
st.start()