Skip to content

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()
}
lib/handlers/hello.go
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) {
c.SendMessage("Hello")
}

Use c.SendStatus() to send the status of the response.

lib/handlers/hello.go
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.
}

Use c.ReceiveHeader() to retrieve header fields and c.SendHeader() to send them.

lib/handlers/hello.go
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().

lib/handlers/hello.go
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.
}

Use c.ReceiveQuery() to retrieve query fields.

lib/handlers/hello.go
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.

lib/handlers/hello.go
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.

lib/handlers/hello.go
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.
}

Use c.ReceiveCookie() to retrieve cookies and c.SendCookie() to send them.

lib/handlers/hello.go
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.
}

Use c.SendRedirect() to redirect to a different location.

lib/handlers/hello.go
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) {
c.SendRedirect("/login", 302)
}

Use c.SendNavigate() to redirect to a different location with status 302.

lib/handlers/hello.go
package handlers
import frz "github.com/razshare/frizzante"
func GetHello(c *frz.Connection) {
c.SendNavigate("/login")
}

Use c.SendSseUpgrade() to upgrade the connection to server sent events.

lib/handlers/hello.go
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.
}
}

Use c.SendWsUpgrade() to upgrade the connection to web sockets.

lib/handlers/hello.go
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 matters when sending data to the client.

Status codes and headers cannot be modified after sending out content.