go iris 连接 mysql 异步_Go Web Iris 开源框架的学习与应用

package main

import (

"fmt"

"github.com/go-xorm/xorm"

"github.com/kataras/iris/v12"

_ "github.com/go-sql-driver/mysql"

"github.com/kataras/iris/v12/middleware/logger"

"github.com/kataras/iris/v12/middleware/recover"

"log"

)

const DriverName = "mysql"

// "数据库账号:数据库密码@tcp(数据库Ip:端口)/数据库名称?parseTime=true"const MasterDataSourceName = "root:@tcp(127.0.0.1:3306)/otott_slave?charset=utf8"

var engine *xorm.Engine

func execute () {

sql := "INSERT INTO `admin`(`id`, `username`, `password`, `true_name`, `email`, `mobile`, `remark`, `create_admin_id`, `last_login_ip`, `last_login_time`, `login_count`, `last_logout_time`, `create_time`, `update_time`, `status`) VALUES (29, 'sz_admin', '$2y$13$nHT706UX/3qDO2FunpzcquY0YxLDw3X9buoXoVTITA3dLXT1bzFxq', '深圳admin', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2019-12-20 14:48:49', '2019-12-20 14:48:49', 1);"

affected, err := engine.Exec(sql)

if err != nil {

log.Fatal("execute error", err)

} else {

id, _ := affected.LastInsertId()

rows, _ := affected.RowsAffected()

fmt.Println("execute id=", id, ",rows=", rows)

}

}

// 连接到数据库func newEngin() *xorm.Engine {

engine, err := xorm.NewEngine(DriverName, MasterDataSourceName)

if err != nil {

log.Fatal(newEngin, err)

return nil

}

// Debug模式,打印全部SQL语句,帮助对比engine.ShowSQL(true)

return engine

}

type Person struct {

Username string `json:"username"`

Pwd string `json:"pwd"`

}

func main() {

app := iris.New()

app.Logger().SetLevel("debug")

// Optionally, add two builtin handlers// that can recover from any http-relative panics// and log the requests to the terminal.app.Use(recover.New())

app.Use(logger.New())

engine = newEngin()

execute() // 测试插入一条语句

// Method: GET// Resource: http://localhost:8080app.Handle("GET", "/", func(ctx iris.Context) {

ctx.HTML("

Go Web Iris, Welcome

")

})

// same as app.Handle("GET", "/ping", [...])// Method: GET// Resource: http://localhost:8080/pingapp.Get("/ping", func(ctx iris.Context) {

path := ctx.Path

app.Logger().Info(path)

username := ctx.URLParam("username")

app.Logger().Info(username)

password := ctx.URLParam("pwd")

app.Logger().Info(password)

//ctx.WriteString(string(path))ctx.HTML("

" + username + "," + password + "

")

})

// Method: GET// Resource: http://localhost:8080/helloapp.Get("/hello", func(ctx iris.Context) {

ctx.JSON(iris.Map{"message": "Hello Iris!"})

})

app.Post("/login", func(ctx iris.Context) {

path := ctx.Path

app.Logger().Info(path)

username := ctx.PostValue("username")

app.Logger().Info(username)

password := ctx.PostValue("pwd")

app.Logger().Info(password)

//ctx.WriteString(string(path))ctx.HTML("

" + username + "," + password + "

")

})

app.Post("/postjson", func(ctx iris.Context) {

path := ctx.Path

app.Logger().Info(path)

var person Person

if err := ctx.ReadJSON(&person); err != nil {

panic(err.Error())

}

//ctx.WriteString(string(path))ctx.Writef("Received", person)

})

// http://localhost:8080// http://localhost:8080/ping// http://localhost:8080/helloapp.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值