v0.8.0
features:
- yap: classfile v2 (#94)
- yap: ${name} - operator Gop_Env (#98 #100)
- ytest for YAP classfile v2 (#101 #102)
- yap doc (#89 #95 #96 #99)
- qiniu/x/stringutil (#90 #91)
changes:
- mod: github.com/golang-jwt/jwt/v5 v5.2.1
- mod: github.com/qiniu/x v1.13.9
Router and Parameters
Create a file named get.yap with the following content:
html `<html><body>Hello, YAP!</body></html>`
Execute the following commands:
gop mod init hello
gop get github.com/goplus/yap@latest
gop mod tidy
gop run .
A simplest web program is running now. At this time, if you visit http://localhost:8080, you will get:
Hello, YAP!
YAP uses filenames to define routes. get.yap
's route is get "/"
(GET homepage), and get_p_#id.yap
's route is get "/p/:id"
(In fact, the filename can also be get_p_:id.yap
, but it is not recommended because :
is not allowed to exist in filenames under Windows).
Let's create a file named get_p_#id.yap with the following content:
json {
"id": ${id},
}
Execute gop run .
and visit http://localhost:8080/p/123, you will get:
{"id": "123"}
YAP Template
In most cases, we don't use the html
directive to generate html pages, but use the yap
template engine. See get_p_#id.yap:
yap "article", {
"id": ${id},
}
It means finding a template called article
to render. See yap/article_yap.html:
<html>
<head><meta charset="utf-8"/></head>
<body>Article {{.id}}</body>
</html>
Run at specified address
By default the YAP server runs on localhost:8080
, but you can change it in main.yap file:
run ":8888"