cango

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: Apache-2.0 Imports: 22 Imported by: 4

README

简介

TODO
a) 动态tag

cango web 开发框架 通过tag进行URI自发现,不需要显式的定义路由;同时支持对于请求参数自动构造,减少编码和转化,更高效简洁的进行开发。

安装

cango需要Go1.18及以上,同时本教程需要依赖go mod。
添加cango包

go get -u github.com/JessonChan/cango

为了更好更快的了解cango,建议通过以下命令安装cango-cli工具

GO111MODULE=off go get -u  github.com/JessonChan/cango-cli
GO111MODULE=off go install github.com/JessonChan/cango-cli

编程代码片段

在编辑器中添加代码片段,可以方便、高效的生成代码。

vscode

添加的方法是在设置中选择配置代码片段

	"Gen Cango Func": {
		"prefix": ".cf",
		"body": [
			"func (p *$1) $2(ps struct {",
			"\tcango.URI `value:\"/$3\"`",
			"}) interface{} {",
			"\treturn nil",
			"}"
		],
		"description": "Log output to console"
	},
	"Gen Cango Post": {
		"prefix": ".post",
		"body": [
			"func (p *$1) $2(ps struct {",
			"\tcango.URI `value:\"/$3\"`",
			"\tcango.PostMethod",
			"}) interface{} {",
			"\treturn nil",
			"}"
		],
		"description": "cango Post method"
	},
	"Gen Cango Struct": {
		"prefix": ".ctrl",
		"body": [
			"type $1 struct{",
			"\tcango.URI `value:\"$2\"`",
			"}\n",
			"var _ = cango.RegisterURI(&$1{})",
		],
		"description": "生成cango的struct"
	},
	"Gen Cango Filter": {
		"prefix": ".filter",
		"body": [
			"type $1 struct{",
			"\tcango.Filter `value:\"$2\"`",
			"}\n",
			"var _ = cango.RegisterFilter(&$1{})\n",
			"func (p *$1) PreHandle(request *cango.WebRequest) interface{} {",
			"\treturn true",
			"}",
			"func (p *$1) PostHandle(request *cango.WebRequest) interface{} {",
			"\treturn true",
			"}",
		],
		"description": "生成cango的struct"
	},
Goland

Goland的中添加 Live Template

func (p *$NAME$) $FUNC$(ps struct {
	cango.URI `value:"/$URL$"`
	cango.PostMethod
}) interface{} {
	return nil
}

Hello, World!

开箱,看看cango最简单的样子。在终端进入自己熟悉的目录(比如/tmp)执行以下命令:

cango-cli start
cd start

本命令完成的工作:创建一个文件,route.go,写入下面的代码(也可以手动输入以下代码)。

package main

import "github.com/JessonChan/cango"

func main() {
	cango.
		NewCan().
		RouteFunc(func(cango.URI) interface{} {
			return cango.Content{String: "Hello,World!"}
		}).
		Run()
}
go run route_func.go

打开 http://127.0.0.1:8080链接,就会看到Hello,World! 。 接下来,通过修改过上面的代码来展示如何通过tag来定义路由。

package main

import "github.com/JessonChan/cango"

func main() {
	cango.
		NewCan().
		RouteFunc(func(ps struct {
			cango.URI `value:"/;/hello"`
		}) interface{} {
			return cango.Content{String: "Hello,World!"}
		}).
		Run()
}

这段代码在start文件夹的route_func.go中,通过对func入参ps来定义两个等效的路由//hello, 意味着,我们既可以通过http://127.0.0.1:8080链接来访问,也可以通过 http://127.0.0.1:8080/hello链接来访问。

上述两段的代码初步展示cango的使用。在项目中,更为推荐的写法是将函数定义在特定的struct上,创建新的文件 route_struct.go,并写入代码如下(start目录下的route_struct.go)

package main

import "github.com/JessonChan/cango"

type Ctrl struct {
	cango.URI `value:"/hello"`
}

// 路由定义是方法的 receiver 中定义的
// 这个方法对应的 /hello
func (c *Ctrl) Hello(cango.URI) interface{} {
	return cango.Content{String: "Hello,World!"}
}

// 路由定义为 receiver中的URI tag和 参数列表中的 URI和tag的相加,
// 这个示例为 /hello/world.html
func (c *Ctrl) World(ps struct {
	cango.URI `value:"/world.html"`
}) interface{} {
	return cango.Content{String: "Hello,Cango!"}
}

func main() {
	cango.
		NewCan().
		Route(&Ctrl{}).
		Run()
}

go run route_struct.go

打开 http://127.0.0.1:8080/hello链接,就会看到Hello,World!;打开 http://127.0.0.1:8080/hello/world.html链接,就会看到Hello,Cango!

路由简介

cango中,路由都定义在控制器结构体和控制器方法参数上。在Hello, World!这一节中已经对此进行初步的说明,可以知道,只需要定义将cango.URI做为成员变量引入结构体(控制器或者控制器方法入参), 就会自动写入路由,路由规则是由tag中的value值来定义的。 需要特殊说明的是value值可以使用;来定义多个同时生效的平行规则,也可以为空。 定义的函数入参为cango.URI类型,出参为interface{},当前版本必须要返回值的, 返回值做为请求的返回依据。

// RouteFunc 方法路由,可以传入多个方法
can.RouteFunc(...func (cango.URI,...cango.Constructor)interface{})
// RouteFuncWithPrefix 带有前缀的方法路由,可以传入多个方法(便于版本、分组等管理)
can.RouteFuncWithPrefix(prefix, ...func(cango.URI, ...cango.Constructor)interface{})
// 路由结构体上所有的方法
can.Route(cango.URI)
// 路由结构体上所有的方法,并使用前缀
can.RouteWithPrefix(cango.URI)
// 在定义struct的时候引入,也这是非常推荐的方法
var _ = cango.RegisterURI(cango.URI)
// RegisterURIWithPrefix 在定义struct的时候引入,同时使用prefix做为路由前缀,也这是非常推荐的方法
var _ = cango.RegisterURIWithPrefix(prefix string, uri URI) 
  • can 是cango.NewCan()的实例
  • cango.URI是用来保存路由及请求相关数据的。
  • prefix是定义在路由上的前缀,使用prefix参数后,路由地址为prefix+URI-Tag-Value
  • interface{} 映射请求的返回值,当前版本支持的类型如下
// 返回模板和数据
cango.ModelView
// 文件类型,会调用http.ServeFile,一般用不到
cango.StaticFile
// 重定向
cango.Redirect
cango.RedirectWithCode
// 上面的例子中使用,会直接返回Content.String
cango.Content
cango.ContentWithCode

如果返回值不是以上类型,当前版本的处理逻辑是返回JSON。

下面的代码为我们展示本小节的所有内容。 在终端执行

cango-cli app

本命令完成以下工作:创建app.go和view和static两个文件夹,在view上创建index.html模板,在static创建index.css文件。形式如下:

.
├── app.go
├── static
│   └── index.css
└── view
    └── index.html

app.go中写入

package main

import (
	"github.com/JessonChan/cango"
)

func Index(ps struct {
	cango.URI `value:"/index.html"`
}) interface{} {
	return cango.ModelView{
		Tpl: "index.html",
		Model: map[string]string{
			"Title":   "cango",
			"Content": "hello,cango",
		},
	}
}

func main() {
	cango.
		NewCan().
		RouteFunc(func(ps struct {
			cango.URI `value:"/;/hello"`
		}) interface{} {
			return cango.Content{String: "Hello,World!"}
		}).
		// 注册两个handle,一个是有函数名,一个没有
		RouteFunc(Index, func(ps struct {
			cango.URI `value:"/goto"`
		}) interface{} {
			return cango.Redirect{Url: "/index.html"}
		}).
		RouteFuncWithPrefix("/v2", Index).
		Run()
}

同时在index.html中写入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{.Title}}</title>
    <link rel="stylesheet" href="/static/index.css">
</head>
<body>
<span class="blue">{{.Content}}</span>
</body>
</html>

在css中写入

.blue {
    color: blue;
}

在终端执行以下命令

go run app.go

在浏览器打开 http://127.0.0.1:8080/index.html链接http://127.0.0.1:8080/v2/index.html链接http://127.0.0.1:8080/goto链接

请求方法

在以上两个例子中都只有GET方法,如果相要定义其它访方法只要在请求中加入对应的方法即可。

type GetMethod httpMethod
type HeadMethod httpMethod
type PostMethod httpMethod
type PutMethod httpMethod
type PatchMethod httpMethod
type DeleteMethod httpMethod
type OptionsMethod httpMethod
type TraceMethod httpMethod

还以上面的例子说明。在app.go中加入新的方法,更新后如下

package main

import (
	"github.com/JessonChan/cango"
)

func Index(ps struct {
	cango.URI `value:"/index.html"`
}) interface{} {
	return cango.ModelView{
		Tpl: "index.html",
		Model: map[string]string{
			"Title":   "cango",
			"Content": "hello,cango",
		},
	}
}

// 演示POST方法
func Ping(ps struct {
	cango.URI `value:"/ping.json"`
	cango.PostMethod
}) interface{} {
	return map[string]string{
		"Pong": "ok",
	}
}

func main() {
	cango.
		NewCan().
		RouteFunc(func(ps struct {
			cango.URI `value:"/;/hello"`
		}) interface{} {
			return cango.Content{String: "Hello,World!"}
		}).
		// 注册两个handle,一个是有函数名,一个没有
		RouteFunc(Index, func(ps struct {
			cango.URI `value:"/goto"`
		}) interface{} {
			return cango.Redirect{Url: "/index.html"}
		}).
		RouteFuncWithPrefix("/v2", Index).
		RouteFunc(Ping).
		Run()
}

重新执行 go run app.go,在终端执行

curl  -d ""  http://127.0.0.1:8080/ping.json

可以看到

{"Pong":"ok"}
路由变量

当前版本支持的路由变量定义方式是使用{},将上面例子中Ping函数进行修改如下

func Ping(ps struct {
cango.URI `value:"/ping/{year}/{car_age}/{Color}.json"`
cango.PostMethod
Year   int
CarAge int
Color  string
}) interface{} {
return map[string]interface{}{
"Pong":  "ok",
"Year":  ps.Year,
"Age":  ps.CarAge,
"Color": ps.Color,
}
}

重新执行 go run app.go,在终端执行

curl  -d ""  http://127.0.0.1:8080/ping/2020/15/white.json

可以看到

{"Age":1,"Color":"white","Pong":"ok","Year":2020}

也就是定义在路由中的变量

请求参数

获取请求中的变量和路由变量的定义方式相同,只需要在入参加入相应的变量名称。

Header变量

获取header中的值,只需要结构体成员使用header这个tag名就可以,如

struct{
    Name string `header:"~"`
}

需要指出的是,在header后使用~这个符号表示,对应的header名称是自动从成员变量名读取。

获取cookie中的值,只需要结构体成员使用cookie这个tag名就可以,如

struct{
    Name string `cookie:"~"`
}

需要指出的是,在cookie后使用~这个符号表示,对应的cookie名称是自动从成员变量名读取。

Session变量

需要在配置文件中开启session,方法为在cango.ini中添加cookie_session_key=my_session_key,加入后会 自动开启session,当前使用的session是gorilla/session包。 获取session中的值,只需要结构体成员使用session这个tag名就可以,如

struct{
    Name string `session:"~"`
}

后使用~这个符号表示,对应的session名称是自动从成员变量名读取

通配符路由

当前版本支持在路由中最多包含一个 * 通配符的路径,并且通配符路由不能再包含路径变量,定义方法如下

cango.URI `value:"/goto/*"`
过滤器

当前版本支持前置过滤器和后置过滤器,目前不支持对执行结果的修改。定义如下

type VisitFilter struct {
cango.Filter `value:"/static/*.css;/static/*.js"`
}
func (v *VisitFilter) PreHandle(req *cango.WebRequest) interface{} {
return true
}
func (v *VisitFilter) PostHandle(req *cango.WebRequest) interface{} {
return true
}

如上所写,使用tag定义filter路径是最推荐的。
上面的代码完成会在执行controller的函数前先执行PreHandle,再执行函数,最后执行PostHandle。它的作用范围是所有在static目录下的的cssjs静态文件。
但是你也可以根据自己的需要,只注册某些cango.URI,如下所示。

can.Filter(f cango.Filter, uris ...cango.URI)
cango.RegisterFilter(cango.Filter)

上在的接口支持对某些接口单独的Filter。

配置文件

当前版本的cango支持配置参数从文件读取,配置文件位置必须是程序运行目录下的conf/cango.ini文件,支持的配置参数如下表
| 参数名 |参数类型 |参数说明 | | ---- |---- |---- | | host/Host |string |监听的主机 | | port/Port |int |监听的端口 | | tpl_dir/tplDir/TplDir |string |模板文件文件夹,相对程序运行路径 | | static_dir/staticDir/StaticDir |string |静态文件文件夹,相对程序运行路径 | | tpl_suffix/tplSuffix/TplSuffix |[]string |模板文件后续名,默认为 .tpl 和 .html | | debug_tpl/debugTpl/DebugTpl |bool |是否调试页面,true 表示每次都重新加载模板 | | canlog_path/canlogPath/CanlogPath |string |日志文件位置,绝对路径 | | cookie_session_key/cookieSessionKey/CookieSessionKey |string |gorilla cookie store 的key | | cookie_session_secure/cookieSessionSecure/CookieSessionSecure |string |gorilla cookie store 加密使用的key |

和其它框架结合(Gin为例)

以下为如何和gin框架结合

package main

import "github.com/gin-gonic/gin"
import "github.com/JessonChan/cango"

func main() {

	can := cango.
		NewCan().
		RouteFunc(func(ps struct {
			cango.URI `value:"/;/hello"`
		}) interface{} {
			return map[string]string{"message": "hello,world"}
		})
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	can.FallbackHandler(r)
	can.Run()
}

如下,引入了cango,可以很好的兼容原来的gin项目。

package main

import (
	"github.com/JessonChan/cango"
	"github.com/gin-gonic/gin"
)

type Api struct {
	cango.URI `value:"/api"`
}

// 通过 http://127.0.0.1:8080/api/msg/bob.json 来访问
func (a *Api) Get(ps struct {
	cango.URI `value:"/msg/{name}.json"`
	Name      string
}) any {
	return map[string]string{"msg": "Very Good! " + ps.Name}
}

func main() {

	r := gin.Default()
	cango.NewCan().Route(&Api{}).GinRoute(r)
	r.Run()
}

更多例子

为了更好的理解和使用cango,cango-cli中还包含canshort_urldemo三个示例,请自己执行查看。 另外,可以查看can_blog这个简单的博客项目。

Documentation

Overview

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Env

func Env(key string) string

Env 从配置文件中取配置项为key的值

func Envs

func Envs(i interface{}, mode ...string)

Envs 对传入的对象进行赋值 i 须为指针类型

func InitLogger

func InitLogger(rw io.Writer)

InitLogger 用来初始化cango的日志writer

func RegisterFilter

func RegisterFilter(filter Filter, values ...string) bool

func RegisterURI

func RegisterURI(uri URI, cangoName ...string) bool

RegisterURI todo with can app Name ??? RegisterURI 在定义struct的时候引入,也这是非常推荐的方法

func RegisterURIWithPrefix

func RegisterURIWithPrefix(prefix string, uri URI, cangoName ...string) bool

RegisterURIWithPrefix 在定义struct的时候引入,同时使用prefix做为路由前缀,也这是非常推荐的方法

func SessionGet

func SessionGet(r *http.Request, key string, value interface{})

func SessionPut

func SessionPut(r *http.Request, rw http.ResponseWriter, key string, value interface{}, opts ...*sessions.Options)

func SetError

func SetError(code int, fn func(w http.ResponseWriter, r *http.Request))

SetError can define http status code with specific method

func SetGorillaSessionStore

func SetGorillaSessionStore(store sessions.Store)

func Version

func Version() string

Version returns the cango tree's version string. It is either the commit hash and date at the time of the build or, when possible, a release tag like "v1.0.0-rc1".

func WriteJSON added in v0.0.2

func WriteJSON(w http.ResponseWriter, obj interface{}) error

WriteJSON marshals the given interface object and writes it with custom ContentType.

Types

type Addr

type Addr struct {
	Host string
	Port int
}

func (Addr) String

func (addr Addr) String() string

type Can

type Can struct {
	// contains filtered or unexported fields
}

func NewCan

func NewCan(name ...string) *Can

NewCan 生成服务对象,name 为生成的对象名,可以为空 一般,只有我们在工程中需要注册多个web服务时才需要设置

func (*Can) FallbackHandler

func (can *Can) FallbackHandler(handler http.Handler) *Can

func (*Can) Filter

func (can *Can) Filter(f Filter, uris ...URI) *Can

func (*Can) GinRoute

func (can *Can) GinRoute(eg *gin.Engine)

GinRoute convert gin to cango

func (*Can) Mode

func (can *Can) Mode(runMode string) *Can

func (*Can) RegTplFunc

func (can *Can) RegTplFunc(name string, fn interface{}) *Can

RegTplFunc 用name注册fn函数,方便在渲染模板时使用

func (*Can) Route

func (can *Can) Route(uris ...URI) *Can

Route todo route by controller and method Name??? Route 路由结构体上所有的可导出方法

func (*Can) RouteFunc

func (can *Can) RouteFunc(fns ...interface{}) *Can

RouteFunc 方法路由,可以传入多个方法

func (*Can) RouteFuncWithPrefix

func (can *Can) RouteFuncWithPrefix(prefix string, fns ...interface{}) *Can

RouteFuncWithPrefix 带有前缀的方法路由,可以传入多个方法(便于版本、分组等管理)

func (*Can) RouteWithPrefix

func (can *Can) RouteWithPrefix(prefix string, uris ...URI) *Can

RouteWithPrefix todo route with suffix and simplify Route路由结构体上所有的可导出方法,并使用路由前缀

func (*Can) Run

func (can *Can) Run(as ...interface{}) error

func (*Can) ServeHTTP

func (can *Can) ServeHTTP(rw http.ResponseWriter, r *http.Request)

func (*Can) SetJsonWriter

func (can *Can) SetJsonWriter(rth responseTypeHandler) *Can

SetJsonWriter will set a json writer

func (*Can) Shutdown

func (p *Can) Shutdown() *Can

func (*Can) ToGins

func (can *Can) ToGins() []*GinHandler

type Caster

type Caster func(string) reflect.Value

type Constructor

type Constructor interface {
	Construct(request *WebRequest)
}

Constructor is the interface that wraps the Construct method.

Construct 可以从*http.Request中进行初始化变量

type Content

type Content struct {
	String string
}

func (Content) WithCode

func (c Content) WithCode(code int) *ContentWithCode

type ContentWithCode

type ContentWithCode struct {
	String string
	Code   int
}

type DeleteMethod

type DeleteMethod httpMethod

Common HTTP methods.

type DoNothing

type DoNothing struct {
}

type Filter

type Filter interface {
	// PreHandle is used to perform operations before sending the request to the controller.
	// This method should return true to continue the request serve.
	// If this method returns false,the request will stop.
	// If this method returns cango-return type(Redirect/ModelView...),the request will response with the type
	PreHandle(request *WebRequest) interface{}
	// PostHandle is used to perform operations before sending the response to the client.
	// This method should return true.
	PostHandle(request *WebRequest) interface{}
}

todo 为什么filter 不使用和URI一样的方式进行注册

type FilterType

type FilterType reflect.Type

type GetMethod

type GetMethod httpMethod

Common HTTP methods.

type GinHandler

type GinHandler struct {
	Url         string
	HttpMethods map[string]bool
	Handle      func(ctx *gin.Context)
}

type HeadMethod

type HeadMethod httpMethod

Common HTTP methods.

type IniConfig

type IniConfig struct {
	// contains filtered or unexported fields
}

func NewIniConfig

func NewIniConfig(filePath string) *IniConfig

func (*IniConfig) Env

func (ic *IniConfig) Env(key string) string

Env 从配置文件中取配置项为key的值

func (*IniConfig) Envs

func (ic *IniConfig) Envs(i interface{})

Envs 对传入的对象进行赋值 i 须为指针类型

type Invoker

type Invoker struct {
	*reflect.Method
	// contains filtered or unexported fields
}

Invoker 实际执行请求的函数 kind用来表示是通过struct来注册的还是只是通过函数来注册的 0 invokeBySelf --- 通过函数 1 invokeByReceiver --- 通过struct

type JSON added in v0.0.2

type JSON struct {
	Data interface{}
}

func (JSON) Render added in v0.0.2

func (r JSON) Render(w http.ResponseWriter) (err error)

Render (JSON) writes data with custom ContentType.

func (JSON) WriteContentType added in v0.0.2

func (r JSON) WriteContentType(w http.ResponseWriter)

WriteContentType (JSON) writes JSON ContentType.

type ModelView

type ModelView struct {
	Tpl   string
	Model interface{}
}

todo 可以根据路由的uri/方法名等自动查找tpl

type OptionsMethod

type OptionsMethod httpMethod

Common HTTP methods.

type Opts

type Opts struct {
	// 监听的主机
	Host string
	// 监听的端口
	Port int
	// 模板文件文件夹,相对程序运行路径
	TplDir string
	// 静态文件文件夹,相对程序运行路径
	StaticDir string
	// 模板文件后续名,默认为 .tpl 和 .html
	TplSuffix []string
	// 是否调试页面,true 表示每次都重新加载模板
	DebugTpl bool
	// 日志文件位置,绝对路径
	CanlogPath string
	// gorilla cookie store 的key
	CookieSessionKey string
	// gorilla cookie store 加密使用的key
	CookieSessionSecure string
	// TLS 文件
	CertFile, KeyFile string
}

Opts 程序启动的配置参数

type PatchMethod

type PatchMethod httpMethod

Common HTTP methods.

type PostMethod

type PostMethod httpMethod

Common HTTP methods.

type PutMethod

type PutMethod httpMethod

Common HTTP methods.

type Redirect

type Redirect struct {
	Url string
}

func (Redirect) WithCode

func (r Redirect) WithCode(code int) *RedirectWithCode

type RedirectWithCode

type RedirectWithCode struct {
	Url  string
	Code int
}

type StaticFile

type StaticFile struct {
	Path string
}

type TraceMethod

type TraceMethod httpMethod

Common HTTP methods.

type URI

type URI interface {
	Request() *WebRequest
}

type WebRequest

type WebRequest struct {
	http.ResponseWriter
	*http.Request
}

func (*WebRequest) IsDelete

func (wr *WebRequest) IsDelete() bool

IsDelete check the request method is http.MethodDelete

func (*WebRequest) IsGet

func (wr *WebRequest) IsGet() bool

IsGet check the request method is http.MethodGet

func (*WebRequest) IsHead

func (wr *WebRequest) IsHead() bool

IsHead check the request method is http.MethodHead

func (*WebRequest) IsOptions

func (wr *WebRequest) IsOptions() bool

IsOptions check the request method is http.MethodOptions

func (*WebRequest) IsPatch

func (wr *WebRequest) IsPatch() bool

IsPatch check the request method is http.MethodPatch

func (*WebRequest) IsPost

func (wr *WebRequest) IsPost() bool

IsPost check the request method is http.MethodPost

func (*WebRequest) IsPut

func (wr *WebRequest) IsPut() bool

IsPut check the request method is http.MethodPut

func (*WebRequest) IsTrace

func (wr *WebRequest) IsTrace() bool

IsTrace check the request method is http.MethodTrace

func (*WebRequest) SessionGet

func (wr *WebRequest) SessionGet(key string, value interface{})

func (*WebRequest) SessionPut

func (wr *WebRequest) SessionPut(key string, value interface{}, opts ...*sessions.Options)

func (*WebRequest) SetCookie

func (wr *WebRequest) SetCookie(ck *http.Cookie)

SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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