Core
requests
Toolips has some bindings that pre-parse responses fro you, these are both post and get requests.
Base.get — FunctionInterface
get() -> ::Dict
Quick binding for an HTTP GET request.
example
Toolips.post — FunctionInterface
post() ->
Quick binding for an HTTP POST request.
example
connections
Toolips.Connection — TypeConnection
- routes::Dict
- http::HTTP.Stream
- extensions::Dict
The connection type is passed into route functions and pages as an argument. This is both for functions, as well as Servable.f() methods. This constructor should not be called directly. Instead, it is called by the server and passed through the function pipeline. Indexing a Connection will return the extension named with that symbol.
example
# v The Connection
home = route("/") do c::Connection
c[Logger].log("We can index extensions.")
c.routes["/"] = c::Connection -> write!(c, "rerouting!")
httpstream = c.http
write!(c, "Hello world!")
myheading::Component = h("myheading", 1, text = "Whoa!")
write!(c, myheading)
endField Info
- routes::Dict - A dictionary of routes where the keys
are the routed URL and the values are the functions to those keys.
- http::HTTP.Stream - The stream for this current peer's connection.
- extensions::Dict - A dictionary of extensions to load with the
name to reference as keys and the extension as the pair.
Constructors
- Connection
Connections are served as an argument to incoming routes. Functions are written anticipating a connection return. Here we will write a new route using the route(::Function, ::String) method.
Route{Function}("/", Main.EvalBlockSandbox.var"#1#2"())We also use the write!() method on our Connection. We can use this on the types ::Any, ::Vector{Servable}, and ::Servable.
Toolips.write! — MethodInterface
write!(::Connection, ::Any) -> _
Attempts to write any type to the Connection's stream.
example
Toolips.write! — MethodInterface
write!(::Connection, ::String) -> _
Writes the String into the Connection as HTML.
example
Toolips.write! — MethodToolips.write! — MethodInterface
write!(c::Connection, s::Vector{Servable}) -> _
Writes, in order of element, each Servable inside of a Vector of Servables.
example
Toolips.write! — MethodInterface
write!(::Connection, ::Servable) -> _
Writes a Servable's return to a Connection's stream.
example
Or push any data response into a body and startread the body.
Base.push! — MethodMissing docstring for startread!(::Connection, ::Any). Check Documenter's build log for details.
The connection type can be indexed with Symbols, Strings, and Types. Symbols and Types will index the extensions. Strings will index the routes. The same goes for setting the indexes.
Base.getindex — MethodMissing docstring for getindex(::Connection, ::Type). Check Documenter's build log for details.
Base.getindex — MethodWe also use the Connection in order to get arguments, download files, and pretty much anything else pertaining to a person's connection.
Toolips.getarg — FunctionInterface
getargs(::Connection, ::Symbol) -> ::Dict
Returns the requested arguments from the target.
example
Interface
getarg(::Connection, ::Symbol, ::Type) -> ::Vector
This method is the same as getargs(::HTTP.Stream, ::Symbol), however types are parsed as type T(). Note that "Cannot convert..." errors are possible with this method.
example
Toolips.getargs — FunctionInterface
getargs(::Connection) -> ::Dict
The getargs method returns arguments from the HTTP header (GET requests.) Returns a full dictionary of these values.
example
Toolips.getip — FunctionToolips.postarg — FunctionInterface
postarg(::Connection, ::String) -> ::Any
Get a body argument of a POST response by name.
example
Missing docstring for getpost. Check Documenter's build log for details.
Toolips.postargs — FunctionInterface
postargs(::Connection, ::Symbol, ::Type) -> ::Dict
Get arguments from the request body.
example
Missing docstring for download!. Check Documenter's build log for details.
Toolips.navigate! — FunctionInterface
navigate!(::Connection, ::String) -> _
Routes a connected stream to a given URL.
example
We can also check if an extension is present by type.
Missing docstring for has_extension(::Connection, ::Type). Check Documenter's build log for details.
routing
When routing, many methods involve the Connection type we just spoke of. In toolips, routes are handled by the Route type.
Toolips.Route — TypeRoute{T}
- path::String
- page::T
A route is added to a ServerTemplate using either its constructor, or the ServerTemplate.add(::Route) method. Each route calls either a particular servable or function; the type of which denoted by T. 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!")
endfields
- path::String
The path, e.g. "/" at which to direct to the given component.
- page::T (::Function || T <: Component)
The servable to serve at this given route.
constructors
- Route(path::String, f::Function) where
- Route(path::String, s::Servable)
The Route's constructors are not typically called directly, instead it is probably better to use these methods. Using route! as opposed to route! will modify the routes of a Connection or ToolipsServer
Toolips.route — FunctionInterface
route(::Function, ::String) -> ::Route
Creates a route from the Function.
example
Interface
route(::String, ::Servable) -> ::Route
Creates a route from a Servable.
example
Toolips.route! — FunctionInterface
route!(::Connection, ::Route) -> _
Modifies the routes on the Connection.
example
Interface
route!(::Function, ::Connection, ::String) -> _
Routes a given String to the Function.
example
Toolips.unroute! — FunctionInterface
unroute!(::Connection, ::String) -> _
Removes the route with the key equivalent to the String.
example
servers
ToolipsServers are created by ServerTemplates. Here is a look at how to make a ServerTemplate:
Toolips.ServerTemplate — TypeServerTemplate
- 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. –––––––––
Field Info
- ip::String
- port::Integer
- routes::Vector{Route}
- extensions::Dict
- remove::Function
- add::Function
- start::Function
Constructors
ServerTemplate(ip::String, port::Int64, routes::Dict; extensions::Dict)
The ServerTemplate.start() function returns a sub-type of ToolipsServer.
Toolips.ToolipsServer — Typeabstract 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...
Toolips.WebServer — TypeWe can also call some methods on a WebServer in order to change our routes
server extensions
All server extensions have the following consistencies:
Toolips.ServerExtension — Typeabstract type ServerExtension
Server extensions are loaded into the server on startup, and can have a few different abilities according to their type field's value. There are three types to be aware of.
Consistencies
There are also a few default extensions included with toolips. These can be used by passing them in a Symbol-labeled dictionary as the extensions key-word argument on a ServerTemplate These are Logger and Files.
Toolips.Logger — TypeLogger
out::String levels::Dict log::Function –––––––––
Field Info
- out::String
Rgw output file for the logger to write to.
- log::Function
A Logger logs information with different levels. Holds the function log(), connected to the function _log(). Methods:
- log(::Int64, ::String)
- log(::String)
- log(::HTTP.Stream, ::String)
Writes to HTML console, and also logs at level 1 with logger.
- levels::Dict
Constructors
Logger(levels::Dict{level_count::Int64 => crayon::Crayons.Crayon}; out::String = pwd() * "logs/log.txt") Logger(; out::String = pwd() * "/logs/log.txt")
Toolips.Files — TypeFiles
type::Symbol directory::String f::Function –––––––––
- type::Symbol - The type of extension. There are three different selections
you can choose from. :connection :routing :func. A :connection extension will be provided in Connection.extensions. A :routing function is passed a Dict of routes as an argument. The last is a function argument, which is just a specific function to run from the top-end to the server.
- directory::String - The directory to route the files from.
- f::Function - The function f() called with a Connection.
constructors
Files(dir::String)