golang mysql web开发_一套简洁、优雅的Golang Web开发框架

tmaic 是一套简洁、优雅的Golang Web开发框架(GoLang Web Framework)。支持mysql,mssql等多类型数据库,它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络应用,而且每行代码都可以简洁、富于表达力。

数据模型

以下是例子

package modelsimport (        "github.com/pangxianfei/framework/helpers/m"        "github.com/pangxianfei/framework/helpers/ptr"        "github.com/pangxianfei/framework/helpers/zone"        "github.com/pangxianfei/framework/model")type User struct {    model.BaseModel    ID        *uint      `gorm:"column:user_id;primary_key;auto_increment"`    Name      *string    `gorm:"column:user_name;type:varchar(100)"`    Email     *string    `gorm:"column:user_email;type:varchar(100);unique_index;not null"`    Password  *string    `gorm:"column:user_password;type:varchar(100);not null"`    CreatedAt *zone.Time `gorm:"column:user_created_at"`    UpdatedAt zone.Time  `gorm:"column:user_updated_at"`    DeletedAt *zone.Time `gorm:"column:user_deleted_at"`}func (user *User) TableName() string {    return user.SetTableName("user")}func (user *User) SetNameAttribute(value interface{}) {    user.Name = user.Email}func (user *User) GetUpdatedAtAttribute(value interface{}) interface{} {    return user.UpdatedAt //查询后这里可以其他处理,如格式化处理,如果是时间time 可以格式成 2020-10-01 18:00:20}

生成公钥文件

tmaic.CreateRsaKey()

系统函数

加密解密函数

公钥加密

s,_:= tmaic.Encryption("Golang使用RSA进行公钥加密私钥解密,私钥加密公钥解密的实现")

私钥解密

dd,_ := tmaic.Decrypt(s)

私钥加密

s,_:= tmaic.PrivateEncryption("Golang使用RSA进行公钥加密私钥解密,私钥加密公钥解密的实现")

公钥解密

dd,_ := tmaic.PublicDecrypt(s)

校验密码是否正确

user.Password:用户密码(已加密)

requestData.Password:要校验的密码(明文)

if !crypt.BcryptCheck(user.Password, requestData.Password) error{        return "返回提示信息"}

注意事项:如果要使用 NSQ 队列的,不用将包注释

导入包:

"github.com/pangxianfei/framework/queue"

"tmaic/app/events"

"tmaic/app/jobs"

"tmaic/app/listeners"

以下去掉注释即可:

//queue.Initialize()

//jobs.Initialize()

//events.Initialize()

//listeners.Initialize()

NSQ安装请参考:https://nsq.io/overview/quick_start.html

nsq启动

1.nsqlookupd

2.nsqd --lookupd-tcp-address=127.0.0.1:4160

3.nsqadmin --lookupd-http-address=127.0.0.1:4161

执行以上命令后:http://127.0.0.1:4171 可查看监控台

队列demo (test)

*** 详情见工程项目下

入列(我们常说的写入 topic,生产者),以下写入topic例子

test := events.Test{}        testparam := &pbs.Test{                Id: uint32(userId),        }        test.SetParam(testparam)        if errs := hub.Emit(&test); errs != nil {                log.Info("user test", tmaic.Output{"event": test, "errors": errs})        }

出列(启动一个观察者)

func init() {        hub.Register(&Test{})}type Test struct {        user models.User        hub.Listen}func (user *Test) Name() hub.ListenerName {        return "add-test" //监听器名称 后面我们会用到}func (user *Test) Subscribe() (eventPtrList []hub.Eventer) {        log.Debug("Subscribe-test")        return []hub.Eventer{                &events.Test{},        }}func (user *Test) Construct(paramPtr proto.Message) error {        /** 第一执行这里          业务代码        */        log.Debug("Construct-test")        return nil}func (user *Test) Handle() error {        /**第二执行这里         最终实现业务逻辑 :比如发邮件、消息推送、短信通知等 这些业务通常封装在 service 层,这里只是建议        */                // 更新        t := users.UserService.Get(cast.ToInt64(user.ID))        t.Email = user.Email //test 其实没有这个字段 根据需要自行组织        t.Name = user.Name   //test 其实没有这个字段 根据需要自行组织        _ = users.UserService.Update(t)                log.Debug("Handle-test")        return nil}

实现消费,在根目录与main.go同级,

go run artisan.go queue:listen test-add //test-add 是Name() 返回的 配合supervisor进程守护

类型转化助手

ToInt64

cast.ToInt64()

ToInt32

cast.ToInt32()

ToFloat32

cast.ToFloat32()

更多方法 查看cast包

打印

tmaic.Dump(mugs)

mugs := map[string]interface{} {        "password2" : map[string]string {        "password3" : "The name cannot be empty",        },    }        //效果    {        "password2": {            "password3": "The name cannot be empty"        }    }

性能测试

虚拟机环境下:

a3318c941136e31422f9e0be7c9e8802.gif

一套简洁、优雅的Golang Web开发框架-1.jpg (22.6 KB, 下载次数: 0)

5 天前 上传

压力测试

ab -c 1000 -n 5000 -k http://127.0.0.1/all

a3318c941136e31422f9e0be7c9e8802.gif

一套简洁、优雅的Golang Web开发框架-2.jpg (51.72 KB, 下载次数: 0)

5 天前 上传

安装教程

git clone git@gitee.com:pangxianfei/tmaic.gitgo mod init // 初始化go mod 包管理go mod tidy // 加载依赖包go mod vendor // 将依赖包拷贝到项目目录中去go run main.go

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值