Basics
Before doing anything you need to start a server.
Create a new server with servers.New()
, then followup with servers.Start()
in order to start a server.
package main
import "main/lib/core/servers"
var server = servers.New() // Creates server.
func main() { defer servers.Start(server) // Starts server.}
Routes
Section titled “Routes”Each server exposes a slice of Routes which you can freely modify.
You can add a new route by appending to or overwriting server.Routes
.
package main
import ( "main/lib/core/servers" "main/lib/routes/welcome")
var server = servers.New() // Creates server.
func main() { defer servers.Start(server) // Starts server. server.Routes = []routes.Route{ // Overwrites routes. {Pattern: "GET /", Handler: welcome.View}, // Adds route. }}
Where welcome.View
is a function pointer.
package welcome
import "main/lib/core/clients"
func View(client *clients.Client) {}
Path Fields
Section titled “Path Fields”Route patterns can define dynamic path fields using {}
syntax.
routes.Route{Pattern: "GET /{name}", Handler: welcome.View}
Path fields can then be retrieved with receive.Path()
.
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive")
func View(client *clients.Client) { _ = receive.Path(client, "name") // Retrieves field "name".}
Messages
Section titled “Messages”Use receive.Message()
to retrieve messages sent by the client.
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive")
func View(client *clients.Client) { _ = receive.Message(client) // Retrieves message.}
Use send.Message()
to send a message to the client.
package welcome
import ( "main/lib/core/clients" "main/lib/core/send")
func View(client *clients.Client) { send.Message(client, "Hello.") // Sends message.}
Headers
Section titled “Headers”Use receive.Header()
to retrieve header fields sent by the client.
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive")
func View(client *clients.Client) { _ = receive.Header(client, "Accept") // Retrieves field "Accept".}
Use send.Header()
to send header fields to the client.
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive" "main/lib/core/send")
func View(client *clients.Client) { accept := receive.Header(client, "Accept") // Retrieves field "Accept". send.Header(client, "Content-Type", accept) // Sends it back.}
Status
Section titled “Status”Use send.Status()
to send the status of the response to the client.
package welcome
import ( "main/lib/core/clients" "main/lib/core/send")
func View(client *clients.Client) { send.Status(client, 404) // Sends status 404. send.Message(client, "Not found.") // Sends message.}
Order of Operations
Section titled “Order of Operations”Order of operations matters when sending data to the client.
For example, sending the status code with send.Status()
after you’ve already sent content
with send.Message()
is not allowed.
package welcome
import ( "main/lib/core/clients" "main/lib/core/send")
func View(client *clients.Client) { send.Message(client, "Hello.") // Sends message. (Succeeds). send.Status(client, 404) // Sends status (Fails).}
send.Status(client, 404)
will fail and the client will receive status 200
instead of 404
.
HTTP/1.1 200 OKDate: Sun, 25 May 2025 02:00:37 GMTContent-Length: 6Content-Type: text/plain; charset=utf-8
Hello.
The failure is logged to the server’s error logger.
Assuming you’re using the default error logger, you’ll see an error of sorts in your console
listening for requests at http://127.0.0.1:8080status is locked
status is locked
, meaning the status code has already been sent to the client and there’s nothing you can do about it.
Queries
Section titled “Queries”Use receive.Query()
to retrieve query fields.
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive" "main/lib/core/send")
func View(client *clients.Client) { name := receive.Query(client, "name") // Retrieves field "name". send.Message(client, "Hello " + name) // Sends message.}
Use receive.Form()
to parse incoming content as multipart form or url encoded form when using POST
and GET
http verbs.
routes.Route{Pattern: "POST /", Handler: welcome.View}// orroutes.Route{Pattern: "GET /", Handler: welcome.View}
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive" "main/lib/core/send")
type Form struct { // Defines a struct in which to Name string `form:"name"` // store the form content.}
func View(client *clients.Client) { var form Form receive.Form(client, &form) // Retrieves form. send.Message(client, "Hello " + form.Name) // Sends message.}
Use receive.Json()
to parse incoming content as json when using POST
and PUT
http verbs and send.Json()
to send json content.
routes.Route{Pattern: "POST /", Handler: welcome.View}// orroutes.Route{Pattern: "PUT /", Handler: welcome.View}
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive" "main/lib/core/send")
type GreetingDetails struct { // Defines a struct in which to Name string `json:"name"` // store the json content.}
func View(client *clients.Client) { var details GreetingDetails // Creates a zero value. receive.Json(client, &details) // Unmarshals the content into details. send.Json(client, details) // Sends content back as json.}
Cookies
Section titled “Cookies”Use receive.Cookie()
to retrieve cookies and send.Cookie()
to send them.
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive" "main/lib/core/send")
func View(client *clients.Client) { nickname := receive.Cookie(client, "nickname") // Retrieves cookie. send.Cookie(client, "nickname", nickname) // Sends it back.}
Session Id
Section titled “Session Id”Use receive.SessionId()
to retrieve the client’s session id.
package welcome
import ( "main/lib/core/clients" "main/lib/core/receive")
func View(client *clients.Client) { _ = receive.SessionId(client) // Retrieves session id.}
Redirect
Section titled “Redirect”Use send.Redirect()
to redirect to a different location.
package welcome
import ( "main/lib/core/clients" "main/lib/core/send")
func View(client *clients.Client) { send.Redirect(client, "/login", 307) // Redirects to /login.}
Navigate
Section titled “Navigate”Use send.Navigate()
to redirect to a different location with status 302
.
package welcome
import ( "main/lib/core/clients" "main/lib/core/send")
func View(client *clients.Client) { send.Navigate(client, "/login") // Redirects to /login with status 302.}