Home

Awesome

yap - Yet Another Go/Go+ HTTP Web Framework

Build Status Go Report Card GitHub release Coverage Status GoDoc Language

This repo contains three Go+ classfiles. They are yap (a HTTP Web Framework), yaptest (a HTTP Test Framework) and ydb (a Go+ 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@latest

For more details, see YAP Framework Manual.

How to use in Go+

First let us initialize a hello project:

gop mod init hello

Then we have it reference a classfile called yap as the HTTP Web Framework:

gop get github.com/goplus/yap@latest

Create a file named get.yap with the following content:

html `<html><body>Hello, YAP!</body></html>`

Execute the following commands:

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: HTTP Web Framework

This classfile has the file suffix .yap.

Router and Parameters

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"

Static files

Static files server demo (main.yap):

static "/foo", FS("public")
static "/"

run ":8080"

yaptest: HTTP Test Framework

yaptest is a web server testing framework. 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 - Go+ HTTP Test Framework.

ydb: Database Framework

This classfile has the file suffix _ydb.gox.

TODO