Routes
You can add a new route to the server with AddRoute()
.
package main
import ( "embed" frz "github.com/razshare/frizzante" "main/lib/handlers")
//go:embed .dist/*/**var dist embed.FS
func main() { frz.NewServer(). WithDist(dist). AddRoute(frz.Route{Pattern: "GET /", Handler: handlers.GetHello}). Start()}
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) { c.SendMessage("Hello")}
Status
Section titled “Status”Use c.SendStatus()
to send the status of the response.
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) { c.SendStatus(404) // Sends status 404. c.SendMessage("Resource not found, sorry.") // Sends text.}
Header
Section titled “Header”Use c.ReceiveHeader()
to retrieve header fields and c.SendHeader()
to send them.
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) { ctype := c.ReceiveHeader("Accept") // Retrieves field "Accept". c.SendHeader("Content-Type", ctype) // Sends it back.}
Request handlers can define dynamic fields within the pattern using {}
syntax.
Path fields can then be retrieved with c.ReceivePath()
.
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) { name := c.ReceivePath("name") // Retrieves field "name". c.SendMessage("Hello " + name) // Sends text.}
GET /{name}
Use c.ReceiveQuery()
to retrieve query fields.
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) { name := c.ReceiveQuery("name") // Retrieves field "name". c.SendMessage("Hello " + name) // Sends text.}
Use c.ReceiveForm()
to retrieve and parse forms
when using POST
and PUT
http verbs.
package handlers
import frz "github.com/razshare/frizzante"
func PostHello(c *frz.Connection) { form := c.ReceiveForm() // Retrieves and parses the form. name := form.Get("name") // Retrieves field "name". c.SendMessage("Hello " + name) // Sends text.}
Use c.ReceiveJson()
to parse incoming content as json and c.SendJson()
to send data as json when using POST
and PUT
http verbs.
package handlers
import frz "github.com/razshare/frizzante"
type GreetingDetails struct { Name string `json:"name"`}
func PostHello(c *frz.Connection) { var value *GreetingDetails // Prepares empty value. c.ReceiveJson(value) // Parses the content into value. c.SendJson(value) // Sends it back.}
Cookies
Section titled “Cookies”Use c.ReceiveCookie()
to retrieve cookies and c.SendCookie()
to send them.
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) { cookie := c.ReceiveCookie("session-id") // Retrieves cookie "session-id". c.SendCookie("session-id", cookie) // Sends it back.}
Redirect
Section titled “Redirect”Use c.SendRedirect()
to redirect to a different location.
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) { c.SendRedirect("/login", 302)}
Navigate
Section titled “Navigate”Use c.SendNavigate()
to redirect to a different location with status 302
.
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) { c.SendNavigate("/login")}
Server Sent Events
Section titled “Server Sent Events”Use c.SendSseUpgrade()
to upgrade the connection to server sent events.
package handlers
import ( frz "github.com/razshare/frizzante" "time")
func GetHello(c *frz.Connection) { alive := c.IsAlive() // Track request status. ev := c.SendSseUpgrade() // Send sse upgrade. for *alive { // Loop until request gets cancelled. ev("channel-1") // Switch to event "channel-1". c.SendMessage("hello 1") // Send text. ev("channel-2") // Switch to event "channel-2". c.SendMessage("hello 2") // Send text. time.Sleep(time.Second) // Sleep for 1 second. }}
Web Sockets
Section titled “Web Sockets”Use c.SendWsUpgrade()
to upgrade the connection to web sockets.
package handlers
import ( frz "github.com/razshare/frizzante" "time")
func GetHello(c *frz.Connection) { alive := c.IsAlive() // Track request status. c.SendWsUpgrade() // Send ws upgrade. for *alive { // Loop until request gets cancelled. c.SendMessage("hello") // Send text. time.Sleep(time.Second) // Sleep for 1 second. }}
Order of Operations
Section titled “Order of Operations”Order of operations matters when sending data to the client.
Status codes and headers cannot be modified after sending out content.