yap

package module
v0.7.6 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 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

How to use in Go+

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
Router and Parameters

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

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"
YAP Template

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"
YAP Test Framework

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.

Documentation

Index

Constants

View Source
const (
	GopPackage = true
)

Variables

This section is empty.

Functions

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
}

func (*App) DELETE added in v0.5.0

func (r *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 (r *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 (r *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 (r *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 (r *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 (r *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 (r *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 (r *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)
}

AppType represents an abstract of YAP applications.

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) 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

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 (r *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 (r *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 (r *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 (r *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 (r *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 (r *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 (r *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 (r *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) 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 Template

type Template struct {
	*template.Template
}

Template is the representation of a parsed template. The *parse.Tree field is exported only for use by html/template and should be treated as unexported by all other clients.

func NewTemplate

func NewTemplate(name string) Template

NewTemplate allocates a new, undefined template with the given name.

func ParseFSFile

func ParseFSFile(f fs.FS, file string) (t Template, err error)

func (Template) Parse

func (t Template) Parse(text string) (ret Template, err error)

Jump to

Keyboard shortcuts

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