在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:faygo开源软件地址:https://gitee.com/henrylee2cn/faygo开源软件介绍:FaygoFaygo 是一款快速、简洁的Go Web框架,可用极少的代码开发出高性能的Web应用程序(尤其是API接口)。只需定义 struct Handler,Faygo 就能自动绑定、验证请求参数并生成在线API文档。 最新版本版本号v1.2.0 安装要求Go Version ≥ 1.8 快速使用
go get -u -v github.com/henrylee2cn/faygo
go get -u -v github.com/henrylee2cn/fay fay command [arguments]The commands are: new 创建、编译和运行(监控文件变化)一个新的faygo项目 run 编译和运行(监控文件变化)任意一个已存在的golang项目fay new appname [apptpl] appname 指定新faygo项目的创建目录 apptpl 指定一个faygo项目模板(可选)fay run [appname] appname 指定待运行的golang项目路径(可选) 框架特性
简单示例package mainimport ( // "mime/multipart" "time" "github.com/henrylee2cn/faygo")type Index struct { Id int `param:"<in:path> <required> <desc:ID> <range: 0:10>"` Title string `param:"<in:query> <nonzero>"` Paragraph []string `param:"<in:query> <name:p> <len: 1:10> <regexp: ^[\\w]*$>"` Cookie string `param:"<in:cookie> <name:faygoID>"` // Picture *multipart.FileHeader `param:"<in:formData> <name:pic> <maxmb:30>"`}func (i *Index) Serve(ctx *faygo.Context) error { if ctx.CookieParam("faygoID") == "" { ctx.SetCookie("faygoID", time.Now().String()) } return ctx.JSON(200, i)}func main() { app := faygo.New("myapp", "0.1") // Register the route in a chain style app.GET("/index/:id", new(Index)) // Register the route in a tree style // app.Route( // app.NewGET("/index/:id", new(Index)), // ) // Start the service faygo.Run()}/*http GET: http://localhost:8080/index/1?title=test&p=abc&p=xyzresponse: { "Id": 1, "Title": "test", "Paragraph": [ "abc", "xyz" ], "Cookie": "2016-11-13 01:14:40.9038005 +0800 CST" }*/ 操作和中间件操作和中间件是相同的,都是实现了Handler接口!
// 不含API文档描述func Page() faygo.HandlerFunc { return func(ctx *faygo.Context) error { return ctx.String(200, "faygo") }}// 含API文档描述var Page2 = faygo.WrapDoc(Page(), "测试页2的注意事项", "文本")
// Param操作通过Tag绑定并验证请求参数type Param struct { Id int `param:"<in:path> <required> <desc:ID> <range: 0:10>"` Title string `param:"<in:query>"`}// Serve实现Handler接口func (p *Param) Serve(ctx *faygo.Context) error { return ctx.JSON(200, faygo.Map{ "Struct Params": p, "Additional Param": ctx.PathParam("additional"), }, true)}// Doc实现API文档接口(可选)func (p *Param) Doc() faygo.Doc { return faygo.Doc{ // 向API文档声明接口注意事项 Note: "param desc", // 向API文档声明响应内容格式 Return: faygo.JSONMsg{ Code: 1, Info: "success", }, // 向API文档增加额外的请求参数声明(可选) Params: []faygo.ParamInfo{ { Name: "additional", In: "path", Model: "a", Desc: "defined by the `Doc()` method", }, }, }} 过滤函数过滤函数必须是HandlerFunc类型! func Root2Index(ctx *faygo.Context) error { // 不允许直接访问`/index` if ctx.Path() == "/index" { ctx.Stop() return nil } if ctx.Path() == "/" { ctx.ModifyPath("/index") } return nil} 路由注册
// 新建应用服务,参数:名称、版本var app1 = faygo.New("myapp1", "1.0")// 路由app1.Filter(Root2Index). Route( app1.NewNamedGET("测试页1", "/page", Page()), app1.NewNamedGET("测试页2", "/page2", Page2), app1.NewGroup("home", app1.NewNamedGET("test param", "/param", &Param{ // 为绑定的参数设定API文档中缺省值(可选) Id: 1, Title: "test param", }), ), )
// 新建应用服务,参数:名称、版本var app2 = faygo.New("myapp2", "1.0")// 路由app2.Filter(Root2Index)app2.NamedGET("test page", "/page", Page())app2.NamedGET("test page2", "/page2", Page2)app2.Group("home"){ app2.NamedGET("test param", "/param", &Param{ // 为绑定的参数设定API文档中缺省值(可选) Id: 1, Title: "test param", })} 平滑关闭与重启
kill [pid]
kill -USR2 [pid] 配置文件说明
net_types = http|https # 多种网络类型列表,支持 http | https | unix_http | unix_https | letsencrypt | unix_letsencryptaddrs = 0.0.0.0:80|0.0.0.0:443 # 多个监听地址列表tls_certfile = # TLS证书文件路径tls_keyfile = # TLS密钥文件路径letsencrypt_dir = # Let's Encrypt TLS证书缓存目录unix_filemode = 0666 # UNIX listener的文件权限,要求使用八进制http_redirect_https = false # 从 'http://hostname:port1' 重定向到 'https://hostname:port2'read_timeout = 0s # 读取请求数据超时;ns|µs|ms|s|m|hwrite_timeout = 0s # 写入响应数据超时;ns|µs|ms|s|m|hmultipart_maxmemory_mb = 32 # 接收上传文件时允许使用的最大内存slow_response_threshold= 0s # 当响应时长 > slow_response_threshold时, 日志级别调整为 'WARNING';0 表示不限;ns|µs|ms|s|m|hprint_body = false # 以JSON格式打印表单请求的body,其它类型请求原样打印body[router] # 路由配置区redirect_trailing_slash = true # 当前请求的URL含`/`后缀如`/foo/`且相应路由不存在时,如存在`/foo`,则自动跳转至`/foo`redirect_fixed_path = true # 自动修复URL,如`/FOO` `/..//Foo`均被跳转至`/foo`(依赖redirect_trailing_slash=true)handle_method_not_allowed = true # 若开启,当前请求方法不存在时返回405,否则返回404handle_options = true # 若开启,自动应答OPTIONS类请求,可在Faygo中设置默认Handlerno_default_params = false # 若开启,不使用handler参数初始值作为请求参数默认值default_upload = true # 自动注册默认静态路由: /upload/*filepathdefault_static = true # 自动注册默认静态路由: /static/*filepath[xsrf] # XSRF跨站请求伪造过滤配置区enable = false # 是否开启key = faygoxsrf # 加密keyexpire_second = 3600 # xsrf防伪token有效时长[session] # Session配置区(详情参考beego session模块)enable = false # 是否开启provider = memory # 数据存储方式name = faygosessionID # 客户端存储cookie的名字provider_config = # 配置信息,根据不同的引擎设置不同的配置信息cookie_life_second = 0 # 客户端存储的cookie的时间,默认值是0,即浏览器生命周期gc_life_second = 300 # 触发GC的时间max_life_second = 3600 # 会话的最大生命周期auto_setcookie = true # 是否自动设置关于session的cookie值,一般默认truedomain = # 可以访问此cookie的域名enable_sid_in_header = false # 是否将session ID写入Headername_in_header = Faygosessionid # 将session ID写入Header时的头名称enable_sid_in_urlquery = false # 是否将session ID写入url的query部分[apidoc] # API文档enable = true # 是否启用path = /apidoc # 访问的URL路径nolimit = false # 是否不限访问IPreal_ip = false # 使用真实客户端的IP进行过滤whitelist = 192.*|202.122.246.170 # 表示允许带有`192.`前缀或等于`202.122.246.170`的IP访问desc = # 项目描述email = # 联系人邮箱terms_url = # 服务条款URLlicense = # 协议类型license_url = # 协议内容URL
[cache] # 文件内存缓存配置区enable = false # 是否开启size_mb = 32 # 允许缓存使用的最大内存(单位MB),为0时系统自动设置为512KBexpire_second = 60 # 缓存最大时长[gzip] # gzip压缩配置区enable = false # 是否开启min_length = 20 # 进行压缩的最小内容长度compress_level = 1 # 非文件类响应Body的压缩水平(0-9),注意文件压缩始终为最优压缩比(9)methods = GET # 允许压缩的请求方法,为空时默认为GET[log] # 日志配置区console_enable = true # 是否启用控制台日志console_level = debug # 控制台日志打印水平:critical | error | warning | notice | info | debugfile_enable = true # 是否启用文件日志file_level = debug # 文件日志打印水平:critical | error | warning | notice | info | debugasync_len = 0 # 0表示同步打印,大于0表示异步缓存长度 Handler结构体字段标签说明
NOTES:
Handler结构体字段类型说明
扩展包
已知案例
注:按拼音字母排序 企业用户开源协议Faygo 项目采用商业应用友好的 Apache2.0 协议发布 |
2022-08-15
2022-08-17
2023-10-27
2022-09-23
2022-08-13
请发表评论