yap

package module
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2024 License: Apache-2.0 Imports: 17 Imported by: 10

README

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

Documentation

Index

Constants

View Source
const (
	GopPackage = true
)

Variables

This section is empty.

Functions

func Gopt_AppV2_Main added in v0.8.0

func Gopt_AppV2_Main(app AppType, handlers ...iHandler)

Gopt_AppV2_Main is required by Go+ compiler as the entry of a YAP project.

func Gopt_App_Main added in v0.5.0

func Gopt_App_Main(app AppType)

Gopt_App_Main is required by Go+ compiler as the entry of a YAP project.

func SubFS added in v0.7.0

func SubFS(fsys fs.FS, dir string) (ret fs.FS)

SubFS returns a sub filesystem by specified a dir.

Types

type App added in v0.5.0

type App struct {
	Engine
}

App is project class of YAP classfile (old version).

func (*App) DELETE added in v0.5.0

func (p *App) DELETE(path string, handle func(ctx *Context))

DELETE is a shortcut for router.Route(http.MethodDelete, path, handle)

func (*App) Delete added in v0.5.0

func (p *App) Delete(path string, handle func(ctx *Context))

Delete is a shortcut for router.Route(http.MethodDelete, path, handle)

func (*App) GET added in v0.5.0

func (p *App) GET(path string, handle func(ctx *Context))

GET is a shortcut for router.Route(http.MethodGet, path, handle)

func (*App) Get added in v0.5.0

func (p *App) Get(path string, handle func(ctx *Context))

Get is a shortcut for router.Route(http.MethodGet, path, handle)

func (*App) HEAD added in v0.5.0

func (p *App) HEAD(path string, handle func(ctx *Context))

HEAD is a shortcut for router.Route(http.MethodHead, path, handle)

func (*App) Head added in v0.5.0

func (p *App) Head(path string, handle func(ctx *Context))

Head is a shortcut for router.Route(http.MethodHead, path, handle)

func (*App) OPTIONS added in v0.5.0

func (p *App) OPTIONS(path string, handle func(ctx *Context))

OPTIONS is a shortcut for router.Route(http.MethodOptions, path, handle)

func (*App) Options added in v0.5.0

func (p *App) Options(path string, handle func(ctx *Context))

Options is a shortcut for router.Route(http.MethodOptions, path, handle)

func (*App) PATCH added in v0.5.0

func (p *App) PATCH(path string, handle func(ctx *Context))

PATCH is a shortcut for router.Route(http.MethodPatch, path, handle)

func (*App) POST added in v0.5.0

func (p *App) POST(path string, handle func(ctx *Context))

POST is a shortcut for router.Route(http.MethodPost, path, handle)

func (*App) PUT added in v0.5.0

func (p *App) PUT(path string, handle func(ctx *Context))

PUT is a shortcut for router.Route(http.MethodPut, path, handle)

func (*App) Patch added in v0.5.0

func (p *App) Patch(path string, handle func(ctx *Context))

Patch is a shortcut for router.Route(http.MethodPatch, path, handle)

func (*App) Post added in v0.5.0

func (p *App) Post(path string, handle func(ctx *Context))

Post is a shortcut for router.Route(http.MethodPost, path, handle)

func (*App) Put added in v0.5.0

func (p *App) Put(path string, handle func(ctx *Context))

Put is a shortcut for router.Route(http.MethodPut, path, handle)

func (*App) Route added in v0.5.0

func (p *App) Route(method, path string, handle func(ctx *Context))

Route registers a new request handle with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*App) Static__0 added in v0.7.1

func (p *App) Static__0(pattern string, dir ...fs.FS)

Static serves static files from a dir (default is "$YapFS/static").

func (*App) Static__1 added in v0.7.1

func (p *App) Static__1(pattern string, ctx context.Context, url string) (closer fsx.Closer, err error)

Static serves static files from a http file system scheme (url). See https://pkg.go.dev/github.com/qiniu/x/http/fsx for more information.

func (*App) Static__2 added in v0.7.2

func (p *App) Static__2(pattern string, fs http.FileSystem, allowRedirect ...bool)

Static serves static files from a http file system.

type AppType added in v0.7.3

type AppType interface {
	InitYap(fs ...fs.FS)
	SetLAS(listenAndServe func(addr string, handler http.Handler) error)
	Route(method, path string, handle func(ctx *Context))
	Handle(pattern string, f func(ctx *Context))
	Run(addr string, mws ...func(h http.Handler) http.Handler) error
}

AppType represents an abstract of YAP applications.

type AppV2 added in v0.8.0

type AppV2 struct {
	App
}

AppV2 is project class of YAP classfile (v2).

func (*AppV2) DELETE added in v0.8.0

func (p *AppV2) DELETE(path string, handle func(ctx *Context))

DELETE is a shortcut for router.Route(http.MethodDelete, path, handle)

func (*AppV2) GET added in v0.8.0

func (p *AppV2) GET(path string, handle func(ctx *Context))

GET is a shortcut for router.Route(http.MethodGet, path, handle)

func (*AppV2) HEAD added in v0.8.0

func (p *AppV2) HEAD(path string, handle func(ctx *Context))

HEAD is a shortcut for router.Route(http.MethodHead, path, handle)

func (*AppV2) OPTIONS added in v0.8.0

func (p *AppV2) OPTIONS(path string, handle func(ctx *Context))

OPTIONS is a shortcut for router.Route(http.MethodOptions, path, handle)

func (*AppV2) PATCH added in v0.8.0

func (p *AppV2) PATCH(path string, handle func(ctx *Context))

PATCH is a shortcut for router.Route(http.MethodPatch, path, handle)

func (*AppV2) POST added in v0.8.0

func (p *AppV2) POST(path string, handle func(ctx *Context))

POST is a shortcut for router.Route(http.MethodPost, path, handle)

func (*AppV2) PUT added in v0.8.0

func (p *AppV2) PUT(path string, handle func(ctx *Context))

PUT is a shortcut for router.Route(http.MethodPut, path, handle)

func (*AppV2) Route added in v0.8.0

func (p *AppV2) Route(method, path string, handle func(ctx *Context))

Route registers a new request handle with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

type Context

type Context struct {
	*http.Request
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*Context) Accept

func (p *Context) Accept(mime ...string) string

Accept header specifies: Accept: <MIME_type>/<MIME_subtype> Accept: <MIME_type>/* Accept: */* Multiple types, weighted with the quality value syntax: Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8 FIXME: 1. quality value not supported, 2. don't need parse all, just find the first match with a spliter iterator

func (*Context) Binary__0 added in v0.5.0

func (p *Context) Binary__0(code int, mime string, data []byte)

func (*Context) Binary__1 added in v0.5.0

func (p *Context) Binary__1(code int, data []byte)

func (*Context) Binary__2 added in v0.5.0

func (p *Context) Binary__2(data []byte)

func (*Context) Binary__3 added in v0.5.0

func (p *Context) Binary__3(code int, data string)

func (*Context) Binary__4 added in v0.5.0

func (p *Context) Binary__4(data string)

func (*Context) DATA

func (p *Context) DATA(code int, mime string, data []byte)

func (*Context) Gop_Env added in v0.8.0

func (p *Context) Gop_Env(name string) string

Gop_Env returns the value associated with the name. If the name exists in URL query, it returns the first value for the name.

func (*Context) Html__0 added in v0.5.0

func (p *Context) Html__0(code int, text string)

func (*Context) Html__1 added in v0.5.0

func (p *Context) Html__1(text string)

func (*Context) Html__2 added in v0.5.0

func (p *Context) Html__2(code int, text []byte)

func (*Context) Html__3 added in v0.5.0

func (p *Context) Html__3(text []byte)

func (*Context) JSON

func (p *Context) JSON(code int, data interface{})

func (*Context) Json__0 added in v0.5.0

func (p *Context) Json__0(code int, data interface{})

func (*Context) Json__1 added in v0.5.0

func (p *Context) Json__1(data interface{})

func (*Context) Param added in v0.2.0

func (p *Context) Param(name string) string

Param returns the value associated with the name. If the name exists in URL query, it returns the first value for the name.

func (*Context) ParamInt added in v0.2.0

func (p *Context) ParamInt(name string, defval int) int

func (*Context) PrettyJSON

func (p *Context) PrettyJSON(code int, data interface{})

func (*Context) PrettyJson__0 added in v0.5.0

func (p *Context) PrettyJson__0(code int, data interface{})

func (*Context) PrettyJson__1 added in v0.5.0

func (p *Context) PrettyJson__1(data interface{})

func (*Context) Redirect added in v0.7.0

func (p *Context) Redirect(url string, code ...int)

Redirect replies to the request with a redirect to url, which may be a path relative to the request path.

func (*Context) TEXT

func (p *Context) TEXT(code int, mime string, text string)

func (*Context) Text__0 added in v0.5.0

func (p *Context) Text__0(code int, mime string, text string)

func (*Context) Text__1 added in v0.5.0

func (p *Context) Text__1(code int, text string)

func (*Context) Text__2 added in v0.5.0

func (p *Context) Text__2(text string)

func (*Context) Text__3 added in v0.5.0

func (p *Context) Text__3(code int, text []byte)

func (*Context) Text__4 added in v0.5.0

func (p *Context) Text__4(text []byte)

func (*Context) YAP

func (p *Context) YAP(code int, yapFile string, data interface{})

func (*Context) Yap__0 added in v0.5.0

func (p *Context) Yap__0(code int, yapFile string, data interface{})

func (*Context) Yap__1 added in v0.5.0

func (p *Context) Yap__1(yapFile string, data interface{})

type Engine

type Engine struct {
	Mux *http.ServeMux
	// contains filtered or unexported fields
}

func New

func New(fs ...fs.FS) *Engine

New creates a YAP engine.

func (*Engine) DELETE added in v0.2.0

func (p *Engine) DELETE(path string, handle func(ctx *Context))

DELETE is a shortcut for router.Route(http.MethodDelete, path, handle)

func (*Engine) FS added in v0.7.0

func (p *Engine) FS(dir string) (ret fs.FS)

FS returns a $YapFS sub filesystem by specified a dir.

func (*Engine) GET added in v0.2.0

func (p *Engine) GET(path string, handle func(ctx *Context))

GET is a shortcut for router.Route(http.MethodGet, path, handle)

func (*Engine) HEAD added in v0.2.0

func (p *Engine) HEAD(path string, handle func(ctx *Context))

HEAD is a shortcut for router.Route(http.MethodHead, path, handle)

func (*Engine) Handle

func (p *Engine) Handle(pattern string, f func(ctx *Context))

Handle registers the handler function for the given pattern.

func (*Engine) Handler added in v0.7.3

func (p *Engine) Handler(mws ...func(h http.Handler) http.Handler) http.Handler

Handler returns the main entry that responds to HTTP requests.

func (*Engine) InitYap added in v0.7.3

func (p *Engine) InitYap(fs ...fs.FS)

InitYap initialize a YAP application.

func (*Engine) NewContext

func (p *Engine) NewContext(w http.ResponseWriter, r *http.Request) *Context

func (*Engine) OPTIONS added in v0.2.0

func (p *Engine) OPTIONS(path string, handle func(ctx *Context))

OPTIONS is a shortcut for router.Route(http.MethodOptions, path, handle)

func (*Engine) PATCH added in v0.2.0

func (p *Engine) PATCH(path string, handle func(ctx *Context))

PATCH is a shortcut for router.Route(http.MethodPatch, path, handle)

func (*Engine) POST added in v0.2.0

func (p *Engine) POST(path string, handle func(ctx *Context))

POST is a shortcut for router.Route(http.MethodPost, path, handle)

func (*Engine) PUT added in v0.2.0

func (p *Engine) PUT(path string, handle func(ctx *Context))

PUT is a shortcut for router.Route(http.MethodPut, path, handle)

func (*Engine) Route added in v0.2.0

func (p *Engine) Route(method, path string, handle func(ctx *Context))

Route registers a new request handle with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*Engine) Run

func (p *Engine) Run(addr string, mws ...func(h http.Handler) http.Handler) error

Run listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

func (*Engine) ServeHTTP added in v0.2.0

func (p *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP makes the router implement the http.Handler interface.

func (*Engine) SetDelims added in v0.8.1

func (p *Engine) SetDelims(left, right string)

SetDelims sets the action delimiters to the specified strings. Nested template definitions will inherit the settings. An empty delimiter stands for the corresponding default: {{ or }}.

func (*Engine) SetLAS added in v0.7.3

func (p *Engine) SetLAS(listenAndServe func(addr string, handler http.Handler) error)

SetLAS sets listenAndServe func to listens on the TCP network address addr and to handle requests on incoming connections.

func (*Engine) Static added in v0.7.0

func (p *Engine) Static(pattern string, dir ...fs.FS)

Static serves static files from a dir (default is "$YapFS/static").

func (*Engine) StaticHttp added in v0.7.1

func (p *Engine) StaticHttp(pattern string, fsys http.FileSystem, allowRedirect ...bool)

StaticHttp serves static files from fsys (http.FileSystem).

type H

type H map[string]interface{}

type Handler added in v0.8.0

type Handler struct {
	Context
}

Handler is worker class of YAP classfile (v2).

func (*Handler) Main added in v0.8.0

func (p *Handler) Main(ctx *Context)

Main is required by Go+ compiler as the entry of a YAP HTTP handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL