XU XUv0.8.8
EN

文档 / 教程

13 · HTTP Context / mux

请求收成一个 http.Context:

fn hello(c http.Context) {
    c.text(200, "hello")
}
http.listen_and_serve_handler("127.0.0.1:18081", hello)

在一个 app(c) 里按 c.path() 分支即可。

源码

@main

#net/http

// 请求上下文:收成一个 Context。

fn app(c http.Context) {
    p := c.path()
    if p == "/" || p == "/index.html" {
        page := "<!doctype html><html><head><meta charset=\"utf-8\"><title>Xu</title></head>" +
            "<body><h1>Context</h1>" +
            "<p><a href=\"/hello\">/hello</a> · <a href=\"/health\">/health</a></p>" +
            "<p>POST /echo</p></body></html>"
        c.html(200, page) or (err) { << }
        <<
    }
    if p == "/hello" {
        c.text(200, "hello, xu\n") or (err) { << }
        <<
    }
    if p == "/health" {
        c.text(200, "ok\n") or (err) { << }
        <<
    }
    if p == "/echo" {
        m := c.method()
        if m != "POST" && m != "PUT" {
            c.text(405, "method not allowed\n") or (err) { << }
            <<
        }
        b := c.body()
        c.text(200, b) or (err) { << }
        <<
    }
    miss := "not found: " + p + "\n"
    c.text(404, miss) or (err) { << }
}

fn main() int {
    println("http://127.0.0.1:18081")
    http.listen_and_serve_handler("127.0.0.1:18081", app) or (err) {
        println(err.msg())
        << 1
    }
    << 0
}

在独立的 examples 仓库根执行;源码位于 examples/。