Skip to content

Examples

The examples come in two sets with one source each:

  • Short programs for the documentation live in examples/ of this repository as .xl files. The pages of this site include them, so the code shown is the code in the repository.
  • Complete programs live in examples/<name>/ (main.xl plus a README). They build as ordinary programs with xl build examples/<name>/main.xl -o <name>.

Check an example with xl check examples/<name>.xl; the site build does not compile them.

Each example below is one of the routing examples of the Gin documentation, written with std/http. The server is the Engine of std/http/engine: routes are registered with the method calls, each handler takes a *Context, and Engine.Run(port) serves until the process ends.

// Hello world: one route, served on port 8080 (Gin's quickstart shape).
from "std" import Engine, Context
@entry fn main() -> i32 {
local router: Engine = Engine()
router.GET("/hello", fn(c: *Context) {
c.String(200, "Hello, World!")
})
return router.Run(8080)
}

:name matches one segment; *action matches the rest of the path, including its leading slash.

// Path parameters: ":name" matches one segment, "*action" the rest of the path. curl http://127.0.0.1:8080/user/john answers
// "Hello john"; curl http://127.0.0.1:8080/user/john/send answers "john is /send".
from "std" import Engine, Context, String
@entry fn main() -> i32 {
local router: Engine = Engine()
router.GET("/user/:name", fn(c: *Context) {
local body: String = String()
body.append("Hello ")
body.append(c.Param("name"))
c.String(200, body.asStr())
}).GET("/user/:name/*action", fn(c: *Context) {
local body: String = String()
body.append(c.Param("name"))
body.append(" is ")
body.append(c.Param("action"))
c.String(200, body.asStr())
})
return router.Run(8080)
}

Query(name) returns the value of a query parameter, and DefaultQuery(name, fallback) returns the fallback when the parameter is absent.

// Query string parameters. curl "http://127.0.0.1:8080/welcome?firstname=Jane&lastname=Doe" answers "Hello Jane Doe"; a missing
// parameter takes its default ("Guest" and "Doe" here).
from "std" import Engine, Context, String
@entry fn main() -> i32 {
local router: Engine = Engine()
router.GET("/welcome", fn(c: *Context) {
local body: String = String()
body.append("Hello ")
body.append(c.DefaultQuery("firstname", "Guest"))
body.append(" ")
body.append(c.DefaultQuery("lastname", "Doe"))
c.String(200, body.asStr())
})
return router.Run(8080)
}

Group(prefix) returns a *RouterGroup that copies the middleware in effect at that point; Use adds middleware to the routes registered after it, and Next runs the rest of the chain.

// Route groups with nested prefixes and middleware. The group's middleware runs before its own handlers only.
// GET /api/v1/users/ -> 401 without the X-Token header, else the list
// GET /api/v1/users/:id -> 401 without the header, else the user
// GET /public -> no middleware
from "std" import Engine, Context, RouterGroup
fn authRequired(c: *Context) {
if c.GetHeader("X-Token").len == 0 {
c.AbortWithStatus(401)
return
}
c.Next()
}
@entry fn main() -> i32 {
local router: Engine = Engine()
local v1: *RouterGroup = router.Group("/api").Group("/v1")
local users: *RouterGroup = v1.Group("/users")
users.Use(authRequired)
users.GET("/", fn(c: *Context) {
c.String(200, "list users")
})
users.GET("/:id", fn(c: *Context) {
c.String(200, "get user")
})
router.GET("/public", fn(c: *Context) {
c.String(200, "public")
})
return router.Run(8080)
}
// Redirects. GET /old goes to https://example.com/ with 301; POST /submit answers 307 so the client repeats the POST at /result.
from "std" import Engine, Context
@entry fn main() -> i32 {
local router: Engine = Engine()
router.GET("/old", fn(c: *Context) {
c.Redirect(301, "https://example.com/")
})
router.POST("/submit", fn(c: *Context) {
c.Redirect(307, "/result")
})
router.GET("/result", fn(c: *Context) {
c.String(200, "result page")
})
return router.Run(8080)
}
// A JSON response. curl http://127.0.0.1:8080/message answers {"message":"hey","status":200} as application/json.
from "std" import Engine, Context
@entry fn main() -> i32 {
local router: Engine = Engine()
router.GET("/message", fn(c: *Context) {
c.JSON(200, "{\"message\":\"hey\",\"status\":200}")
})
return router.Run(8080)
}
Program What it shows
examples/blog-api JSON REST API, route groups, bearer-token (HS256) authentication, an in-memory store
examples/https-server TLS 1.2 and 1.3 server with a certificate and key from files, JSON responses
examples/certificate-inspect PEM certificate fields, chain validation to a root, revocation in a CRL
examples/jwt-tool HS256 sign and verify; RS256, ES256 and EdDSA verification with JWK keys; RFC 7638 thumbprints
examples/archive-tool gzip and zlib compression and decompression of files
examples/dns-lookup name resolution with the system resolver and an RFC 1035 query built as bytes