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="/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="/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://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://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.
yap's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.