• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

016-GoIrisRestful测试

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1:data/data.go

package data

import(
    "fmt"
    "database/sql"
    _"github.com/lib/pq"
)

const(
    host = "192.168.72.128"
    port = 5432
    user = "test"
    password = "test"
    dbname = "testdb"
)

var Db *sql.DB

func init(){
    var err error

    pgInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", 
        host, port, user, password, dbname)
    
    Db, err = sql.Open("postgres", pgInfo)

    if err != nil{
        panic(err)
    }
}

 

2:result/result.go

package result


type Result struct{
    No    int             `json:"no"`
    Msg    string         `json:"msg"`
    Obj interface{} `json:"obj,omitempty"`
}

 

3:user/user_data.go

package user

import(
    ."iristest/data"
)

type User struct{
    ID        int     `json:"id"`
    Name    string    `json:"name"`
    Age        int        `json:"age"`
}

func (user *User) Insert() (err error){
    sql := "insert into t_user(name,age) values($1,$2)"
    _, err = Db.Exec(sql, user.Name, user.Age)
    return
}

func Delete(id int) (err error){
    sql := "delete from t_user where id=$1"
    _,err = Db.Exec(sql, id)
    return
}

func (user *User) Update()(err error){
    sql := "update t_user set name=$1,age=$2 where id=$3"
    _,err = Db.Exec(sql, user.Name, user.Age, user.ID)
    return
}

func Select(id int)(user User, err error){
    sql := "select id,name,age from t_user where id=$1"
    user = User{}
    err = Db.QueryRow(sql, id).Scan(&user.ID, &user.Name, &user.Age)
    return
}

func SelectList()(users []User, err error){
    sql := "select id,name,age from t_user"
    users = []User{}
    rows,err := Db.Query(sql)
    if err != nil{
        panic(err.Error)
    }
    defer rows.Close()

    for rows.Next(){
        rows.Columns()
        var user User
        err = rows.Scan(&user.ID, &user.Name, &user.Age)
        if err != nil{
            panic(err.Error)
        }

        users = append(users, user)
    }

    return
}

 

4:user/user_web.go

package user

import(
    "github.com/kataras/iris"    
    "strconv"
    ."iristest/result"
)

func CreateUser(ctx iris.Context){
    var user User
    ctx.ReadJSON(&user)

    err := user.Insert()

    result := Result{}

    if err != nil{
        result.No = 0
        result.Msg = err.Error()
    }else{
        result.No = 1
        result.Msg = "成功保存用户信息"
    }

    ctx.JSON(result)
}

func GetUserById(ctx iris.Context){
    id,_ := strconv.Atoi(ctx.Params().Get("id"))
    user, err := Select(id)

    result := Result{}

    if err != nil{
        result.No = 0
        result.Msg = err.Error()
    }else{
        result.No = 1
        result.Msg = "成功获取用户信息"
        result.Obj = user
    }

    ctx.JSON(result)
}

func GetUserList(ctx iris.Context){
    users, err := SelectList()

    result := Result{}

    if err != nil{
        result.No = 0
        result.Msg = err.Error()
    }else{
        result.No = 1
        result.Msg = "成功获取用户列表信息"
        result.Obj = users
    }

    ctx.JSON(result)
}

func DeleteUserById(ctx iris.Context){
    id,_ := strconv.Atoi(ctx.Params().Get("id"))
    err := Delete(id)

    result := Result{}

    if err != nil{
        result.No = 0
        result.Msg = err.Error()
    }else{
        result.No = 1
        result.Msg = "成功删除用户信息"
    }

    ctx.JSON(result)
}

5:main.go

package main

import(
    "github.com/kataras/iris"
    "github.com/kataras/iris/middleware/logger"
    "github.com/kataras/iris/middleware/recover"
    ."iristest/user"
)

func main(){
    app := iris.New()
    app.Logger().SetLevel("debug")
    app.Use(recover.New())
    app.Use(logger.New())

    app.RegisterView(iris.HTML("./public", ".html"))
    app.Get("/", func(ctx iris.Context){
        ctx.View("index.html")
    })//users
    usersRoutes := app.Party("/users", func(ctx iris.Context){
        ctx.Next()
    })
    {
        usersRoutes.Post("/", CreateUser)
        usersRoutes.Delete("/{id:int}", DeleteUserById)
        usersRoutes.Get("/{id:int}", GetUserById)
        usersRoutes.Get("/", GetUserList)
    }

    app.Run(iris.Addr(":8080"),iris.WithCharset("UTF-8"),iris.WithoutServerError(iris.ErrServerClosed))
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go测试--进阶发布时间:2022-07-10
下一篇:
go package包的使用发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap