This repo contains two Go+ classfiles: yap
(a HTTP Web Framework) and yaptest
(a HTTP Test Framework).
The classfile yap
has the file suffix _yap.gox
. And the classfile yaptest
has the file suffix _ytest.gox
.
Before using yap
or yaptest
, you need to add github.com/goplus/yap
to go.mod
by using go get
:
go get github.com/goplus/yap@latest
Then find require github.com/goplus/yap
statement in go.mod
and add //gop:class
at the end of the line:
require github.com/goplus/yap v0.7.2 //gop:class
demo in Go (hello.go):
import "github.com/goplus/yap"
y := yap.New()
y.GET("/p/:id", func(ctx *yap.Context) {
ctx.JSON(200, yap.H{
"id": ctx.Param("id"),
})
})
y.Handle("/", func(ctx *yap.Context) {
ctx.TEXT(200, "text/html", `<html><body>Hello, <a href="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/p/123">Yap</a>!</body></html>`)
})
y.Run(":8080")
demo in Go+ classfile (hello_yap.gox):
get "/p/:id", ctx => {
ctx.json {
"id": ctx.param("id"),
}
}
handle "/", ctx => {
ctx.html `<html><body>Hello, <a href="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/p/123">Yap</a>!</body></html>`
}
run ":8080"
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(":8888")
Static files server demo in Go+ classfile (staticfile_yap.gox):
static "/foo", FS("public")
static "/"
run ":8888"
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://goplus.org"), false // false means not allow to redirect
run ":8888"
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(":8080")
demo in Go+ classfile (blog_yap.gox, article_yap.html):
get "/p/:id", ctx => {
ctx.yap "article", {
"id": ctx.param("id"),
}
}
run ":8080"
Suppose we have a web server named foo
(demo/foo/foo_yap.gox):
get "/p/:id", ctx => {
ctx.json {
"id": ctx.param("id"),
}
}
run ":8080"
Then we create a yaptest file (demo/foo/foo_ytest.gox):
mock "foo.com", new(foo)
run "test get /p/$id", => {
id := "123"
get "http:https://foo.com/p/${id}"
ret 200
json {
"id": id,
}
}
The directive mock
creates the foo
server by mockhttp. Then we call the directive run
to run a subtest.
You can change the directive mock
to testServer
(see demo/foo/bar_ytest.gox), and keep everything else unchanged:
testServer "foo.com", new(foo)
run "test get /p/$id", => {
id := "123"
get "http:https://foo.com/p/${id}"
ret 200
json {
"id": id,
}
}
The directive testServer
creates the foo
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 - Go+ HTTP Test Framework.