This repo contains three XGo classfiles: yap (a HTTP Web Framework), yaptest (a HTTP Test Framework) and ydb (a XGo Database Framework).
The classfile yap has the file suffix .yap. The classfile yaptest has the file suffix _ytest.gox. And the classfile ydb has the file suffix _ydb.gox.
Before using yap, yaptest or ydb, you need to add github.com/goplus/yap to go.mod:
gop get github.com/goplus/yap@latestdemo in Go (hello.go):
import "github.com/goplus/yap"
func main() {
y := yap.New()
y.GET("/", func(ctx *yap.Context) {
ctx.TEXT(200, "text/html", `<html><body>Hello, YAP!</body></html>`)
})
y.GET("/p/:id", func(ctx *yap.Context) {
ctx.JSON(200, yap.H{
"id": ctx.Param("id"),
})
})
y.Run("localhost:8080")
}demo in XGo classfile v1 (main.yap):
get "/", ctx => {
ctx.html `<html><body>Hello, YAP!</body></html>`
}
get "/p/:id", ctx => {
ctx.json {
"id": ctx.param("id"),
}
}
run "localhost:8080"demo in XGo classfile v2 (get.yap, get_p_#id.yap):
html `<html><body>Hello, YAP!</body></html>`json {
"id": ${id},
}Static files server demo in Go:
y := yap.New(os.DirFS("."))
y.Static("/foo", y.FS("public"))
y.Static("/") // means: y.Static("/", y.FS("static"))
y.Run(":8080")Static files server demo in XGo classfile (main.yap):
static "/foo", FS("public")
static "/"
run ":8080"Static files server also can use a http.FileSystem instead of fs.FS object (See yapserve for details):
import "github.com/qiniu/x/http/fs"
static "/", fs.http("https://xgo.dev"), false // false means not allow to redirect
run ":8080"demo in Go (blog.go, article_yap.html):
import (
"os"
"github.com/goplus/yap"
)
y := yap.New(os.DirFS("."))
y.GET("/p/:id", func(ctx *yap.Context) {
ctx.YAP(200, "article", yap.H{
"id": ctx.Param("id"),
})
})
y.Run(":8888")demo in XGo classfile v1 (main.yap, article_yap.html):
get "/p/:id", ctx => {
ctx.yap "article", {
"id": ctx.param("id"),
}
}
run ":8888"demo in XGo classfile v2 (get_p_#id.yap, article_yap.html):
yap "article", {
"id": ${id},
}This classfile has the file suffix _ytest.gox.
Suppose we have a web server (foo/get_p_#id.yap):
json {
"id": ${id},
}Then we create a yaptest file (foo/foo_ytest.gox):
mock "foo.com", new(AppV2) // name of any YAP v2 web server is `AppV2`
id := "123"
get "http://foo.com/p/${id}"
ret 200
json {
"id": id,
}The directive mock creates the web server by mockhttp. Then we write test code directly.
You can change the directive mock to testServer (see foo/bar_ytest.gox), and keep everything else unchanged:
testServer "foo.com", new(AppV2)
id := "123"
get "http://foo.com/p/${id}"
ret 200
json {
"id": id,
}The directive testServer creates the web server by net/http/httptest and obtained a random port as the service address. Then it calls the directive host to map the random service address to foo.com. This makes all other code no need to changed.
For more details, see yaptest - XGo HTTP Test Framework.