Faygo一款最适合开发API的 Go Web 框架

图片 7

目前有很多web框架(beego、faygo等)支持结构体参数验证;为了解决项目管理上的问题引入一个框架且框架中参数验证规则扩展性不是很好,个人感觉不是很优雅;于是自己写了一个结构体参数验证工具(struct_util.go)

平滑关闭与重启

  • 平滑关闭

kill [pid]
  • 平滑重启

kill -USR2 [pid]
3、解读:

当一个点(x, y, z)经过3D变换后变为另外一个点(x1, y1,
z1)的过程可以表示为:

图片 1

(1、)常用方法与定义:

/* The identity transform: [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]. */
// 单位矩阵
CA_EXTERN const CATransform3D CATransform3DIdentity

// 判断矩阵是否为3D单位矩阵
CA_EXTERN bool CATransform3DIsIdentity (CATransform3D t)

// 判断两个3D矩阵是否一样
CA_EXTERN bool CATransform3DEqualToTransform (CATransform3D a, CATransform3D b)

/* Returns a transform that translates by '(tx, ty, tz)':
 * t' =  [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1]. */
// 平移
CA_EXTERN CATransform3D CATransform3DMakeTranslation (CGFloat tx, CGFloat ty, CGFloat tz)

/* Returns a transform that scales by `(sx, sy, sz)':
 * t' = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0; 0 0 0 1]. */
// 缩放
CA_EXTERN CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy, CGFloat sz)

/* Returns a transform that rotates by 'angle' radians about the vector
 * '(x, y, z)'. If the vector has length zero the identity transform is returned. */
// 旋转
CA_EXTERN CATransform3D CATransform3DMakeRotation (CGFloat angle, CGFloat x, CGFloat y, CGFloat z)

#prama -----------以上的矩阵变换均是针对默认单位矩阵做出的变换!!!
#prama -----------即单独使用别管你调用了那个,调用了几次,最后结果仅仅是针对单位矩阵做了最后一步的变换!!!

/* Translate 't' by '(tx, ty, tz)' and return the result: t' = translate(tx, ty, tz) * t. */
// 在某一3D变换的基础上平移
CA_EXTERN CATransform3D CATransform3DTranslate (CATransform3D t, CGFloat tx, CGFloat ty, CGFloat tz)

/* Scale 't' by '(sx, sy, sz)' and return the result: t' = scale(sx, sy, sz) * t. */
// 在某一3D变换的基础上缩放
CA_EXTERN CATransform3D CATransform3DScale (CATransform3D t, CGFloat sx, CGFloat sy, CGFloat sz)

/* Rotate 't' by 'angle' radians about the vector '(x, y, z)' and return the result.
 * If the vector has zero length the behavior is undefined: t' = rotation(angle, x, y, z) * t. */
// 在某一3D变换的基础上旋转
CA_EXTERN CATransform3D CATransform3DRotate (CATransform3D t, CGFloat angle, CGFloat x, CGFloat y, CGFloat z)

/* Concatenate 'b' to 'a' and return the result: t' = a * b.*/
// 组合两个3D变换
CA_EXTERN CATransform3D CATransform3DConcat (CATransform3D a, CATransform3D b)

#prama -----------以上涉及矩阵变换的矩阵可以将多重效果组合使用!

/* Invert 't' and return the result. Returns the original matrix if 't' has no inverse. */
// 对某一3D变换结果取反
CA_EXTERN CATransform3D CATransform3DInvert (CATransform3D t)

/* Return a transform with the same effect as affine transform 'm'. */
// 实现与仿射变换函数效果相同的3D变换,即仿射变换转换成3D变换
CA_EXTERN CATransform3D CATransform3DMakeAffineTransform (CGAffineTransform m)

/* Returns true if 't' can be represented exactly by an affine transform. */
// 判断该3D变换是否可以通过仿射变换精确地表示。因为所有仿射变换都可以用3D变换来表示,但是反之则不行。
CA_EXTERN bool CATransform3DIsAffine (CATransform3D t)

/* Returns the affine transform represented by 't'. If 't' can not be
 * represented exactly by an affine transform the returned value is undefined. */
// 返回由't'表示的仿射变换。 如果't'不能通过仿射变换精确地表示,则返回的值是未定义的
CA_EXTERN CGAffineTransform CATransform3DGetAffineTransform (CATransform3D t)

@interface NSValue (CATransform3DAdditions)
// NSValue与CATransform3D相互转换的接口
+ (NSValue *)valueWithCATransform3D:(CATransform3D)t;
@property(readonly) CATransform3D CATransform3DValue;

@end

示例一、

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _blueLayer = [CALayer layer];
    [_blueLayer setBackgroundColor:[UIColor blueColor].CGColor];
    [_blueLayer setFrame:CGRectMake(50, 100, 100, 100)];
    [self.view.layer addSublayer:_blueLayer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (once) {
        [UIView animateWithDuration:2.0 animations:^{
            _blueLayer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5);
        }];
        once = NO;
    }else{
        [UIView animateWithDuration:2.0 animations:^{
            _blueLayer.transform = CATransform3DMakeTranslation(100, 0, 0);
        }];
        once = YES;
    }
}

图片 2

示例一效果图

示例二、

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _blueLayer = [CALayer layer];
    [_blueLayer setBackgroundColor:[UIColor blueColor].CGColor];
    [_blueLayer setContents:(id)[[UIImage imageNamed:@"lovesick"] CGImage]];
    [_blueLayer setFrame:CGRectMake(50, 100, 200, 350)];
    [self.view.layer addSublayer:_blueLayer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:10.0 animations:^{
        CGAffineTransform affineTransform_1 = CGAffineTransformMakeTranslation(200, 0);
        CGAffineTransform affineTransform_2 = CGAffineTransformMakeScale(0.5, 0.5);
        CGAffineTransform affineTransform_all = CGAffineTransformConcat(affineTransform_1, affineTransform_2);
        CATransform3D transform3D_1 = CATransform3DMakeAffineTransform(affineTransform_all);
        CATransform3D transform3D_2 = CATransform3DRotate(transform3D_1, M_PI/2, 0, 0.5, 0.5);
        _blueLayer.transform = transform3D_2;
    }];
}

图片 3

示例二效果图

(2、)其他方法

  • 除了上述系统的函数外还可以直接修改结构体参数来实现3D变换。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:10.0 animations:^{
        CATransform3D transform3D = CATransform3DIdentity;
        transform3D.m41 = 200;
        _blueLayer.transform = transform3D;
    }];
}

图片 4

直接修改参数示例

普通转换的矩阵配置影响参数:

图片 5

在这里特别需要注意的是参数m34,
这一参数主要涉及到设置透视效果,但是必须要有旋转角度才能显示出来,否则无效果。

 feedback := &mysql_model.OfbankFeedback{} err := json.Unmarshal(body, feedback) feedback.FStatus=1 feedback.CreateTime=time.Now() if err != nil { resp := apiservice.GenerateResponse(0, "请求参数有误", "") rw.Write([]byte return } if feedback.Phone=="" { resp := apiservice.GenerateResponse(0, "参数phone不能为空", "") rw.Write([]byte return } if feedback.Contacts=="" { resp := apiservice.GenerateResponse(0, "参数contacts不能为空", "") rw.Write([]byte return } if feedback.FType==0 { resp := apiservice.GenerateResponse(0, "参数fType不能为空", "") rw.Write([]byte return } if feedback.Img==""&&feedback.Content=="" { resp := apiservice.GenerateResponse(0, "参数img或content不能同时为空", "") rw.Write([]byte return }

简单示例

package main

import (
    // "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=xyz
response:
    {
        "Id": 1,
        "Title": "test",
        "Paragraph": [
            "abc",
            "xyz"
        ],
        "Cookie": "2016-11-13 01:14:40.9038005 +0800 CST"
    }
*/

示例库

CoreGraphics框架中的CATransform3D类可用于设定CALayer及其子类的transform属性,控制layer的缩放、旋转和平移等操作。

代码示例:
/** * Created with IntelliJ IDEA. * Description: * User: yangzhao * Date: 2018-07-17 * Time: 11:08 */package utilsimport ( "reflect" "errors" "strings" "strconv")//自定义验证规则const ( NOT_EMPTY = "NotEmpty" //字符串不能为空 INT_MAX = "int-max" //int最大值 INT_MIN = "int-min" //int最小值 TYPE = "type" //类型 STR_MAX_LENGTH = "str-max-len" //字符串最大长度 STR_MIN_LENGTH = "str-min-len" //字符串最小长度 STR_LENGTH = "str-len" //字符串长度 RANGE = "range" //元素必须在合适的范围内 例:1-100)//对外暴露结构体验证函数func StructValidate(bean interface{}) error { fields := reflect.ValueOf.Elem() for i := 0; i < fields.NumField(); i++ { field := fields.Type().Field valid := field.Tag.Get if valid == "" { continue } value := fields.FieldByName(field.Name) err := fieldValidate(field.Name, valid, value) if err != nil { return err } } return nil}//属性验证func fieldValidate(fieldName, valid string, value reflect.Value) error { valids := strings.Split(valid, " ") for _, valid := range valids { if strings.Index(valid, TYPE) != -1 { v := value.Type split := strings.Split(valid, "=") t := split[1] if v != t { return errors.New(fieldName + " type must is " + t) } } if strings.Index(valid, NOT_EMPTY) != -1 { str := value.String() if str == "" { return errors.New(fieldName + " value not empty") } } if strings.Index(valid, INT_MIN) != -1 { v := value.Int() split := strings.Split(valid, "=") rule, err := strconv.Atoi if err != nil { return errors.New(fieldName + ":验证规则有误") } if int < rule { return errors.New(fieldName + " value must >= " + strconv.Itoa } } if strings.Index(valid, INT_MAX) != -1 { v := value.Int() split := strings.Split(valid, "=") rule, err := strconv.Atoi if err != nil { return errors.New(fieldName + ":验证规则有误") } if int > rule { return errors.New(fieldName + " value must <= " + strconv.Itoa } } //字符串特殊处理 if value.Type == "string" { if strings.Index(valid, STR_LENGTH) != -1 { v := value.String() split := strings.Split(valid, "=") lenStr := split[1] length, err := strconv.Atoi if err != nil { return errors.New(fieldName + " " + STR_LENGTH + " rule is error") } if len != length { return errors.New(fieldName + " str length must be " + lenStr) } } if strings.Index(valid, STR_MAX_LENGTH) != -1 { v := value.String() split := strings.Split(valid, "=") lenStr := split[1] length, err := strconv.Atoi if err != nil { return errors.New(fieldName + " " + STR_LENGTH + " rule is error") } if len > length { return errors.New(fieldName + " str length <= " + lenStr) } } if strings.Index(valid, STR_MIN_LENGTH) != -1 { v := value.String() split := strings.Split(valid, "=") lenStr := split[1] length, err := strconv.Atoi if err != nil { return errors.New(fieldName + " " + STR_LENGTH + " rule is error") } if len < length { return errors.New(fieldName + " str length >= " + lenStr) } } } } return nil}

框架特性

  • 一个 struct Handler 搞定多件事:

  • 定义 Handler/Middleware
  • 绑定与验证请求参数
  • 生成 Swagger2.0 API 在线文档
  • 数据库 ORM 映射

  • Handler与Middleware完全相同,都是实现Handler接口(funcstruct类型),共同构成路由操作链,只是概念层面的说法不同
  • 支持多种网络类型:
网络类型 配置net_types
HTTP http
HTTPS/HTTP2(TLS) https
HTTPS/HTTP2(Let’s Encrypt TLS) letsencrypt
HTTPS/HTTP2(Let’s Encrypt TLS on UNIX socket) unix_letsencrypt
HTTP(UNIX socket) unix_http
HTTPS/HTTP2(TLS on UNIX socket) unix_https
  • 支持单服务单监听、单服务多监听、多服务多监听等,多个服务的配置信息相互独立
  • 基于 httprouter
    开发高性能路由,支持链式与树形两种注册风格,支持灵活的静态文件路由(如DirFS、RenderFS、MarkdownFS等)
  • 支持平滑关闭、平滑升级,提供fay工具进行新建项目、热编译、元编程
  • 采用最强大的 pongo2 作为HTML渲染引擎
  • 提供近似LRU的文件缓存功能,主要用途是静态文件缓存
  • 跨平台的彩色日志系统,且同时支持console和file两种输出形式(可以同时使用)
  • 提供Session管理功能
  • 支持Gzip全局配置
  • 提供XSRF跨站请求伪造安全过滤
  • 大多数功能尽量使用简洁的ini进行配置来避免不必要的重新编译,并且这些配置文件支持自动补填默认值
  • 提供 gormxormsqlxdirectSQLWebsocketini
    http client 等很多常用扩展包

图片 6

faygo struct handler 多重用途合一

2、原理:

一个任意的3D变换都能表示为 乘以一个4 x 4矩阵
,即经历一个4×4的矩阵的变换。

demo示例:

图片 7image.png

以上属于原创文章,转载请注明作者@怪咖QQ:208275451

快速使用

  • 方式一 源码下载

go get -u -v github.com/henrylee2cn/faygo
  • 方式二 部署工具 (Go to
    fay)

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项目路径(可选)
1、定义:
struct CATransform3D
{
  CGFloat m11, m12, m13, m14;
  CGFloat m21, m22, m23, m24;
  CGFloat m31, m32, m33, m34;
  CGFloat m41, m42, m43, m44;
};
typedef struct CATransform3D CATransform3D;
You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图