## Introduction
The Server API allows you to interact and send messages between the client and the server, allowing you to do all sorts of things from forms and login prompts to chatrooms. The Server and Client API both take advantage of Firewolf's fast and encrypted networking system.
## Queries
Queries are the core of sending data from the client to the server. Think of it like AJAX for Firewolf. Queries work like appending variables to the URLs, just like what you see in PHP. Example: `mysite.com/query?cat=meow&dog=woof` The Server API automatically decodes the variables on the handler call (See below).
## Setup
To use the Server API, first, create a file in your server domain directory named "server_api". Inside there you stick your functions and code for the Server API, that's it!
## Functions
`server.domain` - Returns the server's domain. Ex: `print(server.domain) -- mysite.com`
`server.log(text)` - Logs the text to the server console
`server.handleRequest(page, func)` - Runs the function provided when matching pages are requested. See below for more information.
`server.applyTemplate(vars, templatePath)` - Applies "vars" to the template file located at the "templatePath", and returns it. If file does not exist, returns false.
`server.runCoroutine(func)` - Runs the function as a coroutine along with the server. Can be called multiple times, and will be stacked.
## Handling Requests
`server.handleRequest(page, func)`
### page
"page" is a string that specifies the pages requested to run the function "func". The page must be the page right after the domain root.
Examples:
```lua
server.handleRequest("cat", myFunction)
```
Will match:
```
mysite.com/cat
mysite.com/cat/meow/dog/
mysite.com/cat?dog=woof
mysite.com/cat/meow?dog=woof
```
```lua
server.handleRequest("/", myFunction)
```
Will match every request
```lua
server.handleRequest("not-found", myFunction)
```
Will match every "page not found" request
If you need to filter the results, you can make your function filter it out. The Firewolf server will only respond with content if your function returns a string. See the next section for more information.
### func
The Firewolf server will run the function provided with 2 arguments, the raw page request, and the query variables in a dictionary table, if any. If the function returns anything, the request will be overridden from what would have happened if there was no server_api, and would respond with the data the function returned. If the function returns nil, Firewolf server will continue and respond with whatever it would normally respond with.
You can also change the header of the response by returning a second variable. At this moment, the header can only be changed to "fwml"
Examples:
```lua
local myFunction = function(page, vars)
-- If you need to remove the query stuff from the page requested
pageWithoutQuery = page:match("^[^%?]+")
...
return "print('hello')"
or
return "[br]hello", "fwml"
or
return nil -- Not gonna do an API response
end
server.handleRequest("cat", myFunction)
````
A request of `mysite.com/cat` will call `myFunction("cat", nil)`
A request of `mysite.com/cat?dog=woof` will call `myFunction("cat?dog=woof", {["dog"]="woof"})`
A request of `mysite.com/cat/meow?dog=woof` will call `myFunction("cat/meow?dog=woof", {["dog"]="woof"})`
## Templates
`server.applyTemplate(vars, templatePath)` - Returns applied template, or false if file does not exist.
Templates are files stored on the server, which can be anywhere on the server computer, to be read and have portions of it replaced as a canned response where only certain parts need to change with the use of specifying a dictionary table. What happens is that the template file is read, and everything in the vars table's key's variable key is replaced with the variable in the table. The variable key is `##varname##`. Here's an example:
```lua
-- Let's say this is stored under /templates/example
print("{{hello}}")
```
which then can be used in the Server API like
```lua
local resp = server.applyTemplate({["hello"] = "bonjour"}, "/templates/example")
print(resp) -- Prints out: print("bonjour")
```
These can be more complex with different variables, such like:
```
-- Stored in /templates/profile-example
print("Your name is: {{name}}")
print("Your birthday is on: {{birthday}}")
print("Your pet: {{pet}}")
```
which applied looks like:
```lua
local myFunction = function()
local profile = {}
profile.name = "1lann"
profile.birthday = "January 28th"
profile.pet = "None"
return server.applyTemplate(profile, "/templates/profile-example")
end
... attach handler, etc.
```
The above is one example which can be applied to `firewolf.redirect(url .. firewolf.encode(vars))` to redirect people with variables sent to the server, which the server can use to construct a page with.
## Examples
### Custom page not found
Stored in /templates/not-found (Meaning it's in the computer's root directory). In this example, it is a FWML document.
```
[br][br][=]
The page "{{page}}" could not be found![br]
Please make sure you typed in the URL correctly[br]
[br][br]
[c lightBlue]
[newlink {{domain}}]Click here to return to the homepage[endlink]
```
The "server_api" file
```lua
server.handleRequest("not-found", function(page)
cleanPage = page:match("^[^%?]+")
local vars = {page = cleanPage, domain = server.domain}
return server.applyTemplate(vars, "/templates/not-found"), "fwml"
end)
```