Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (71 sloc) 1.81 KB
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
type Person struct {
Id int `json:"id" form:"id"`
FirstName string `json:"first_name" form:"first_name"`
LastName string `json:"last_name" form:"last_name"`
}
func main() {
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/test?parseTime=true")
defer db.Close()
if err != nil {
log.Fatalln(err)
}
db.SetMaxIdleConns(20)
db.SetMaxOpenConns(20)
if err := db.Ping(); err != nil {
log.Fatalln(err)
}
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "It works")
})
router.GET("/insert", func(c *gin.Context) {
firstName := c.Request.FormValue("first_name")
lastName := c.Request.FormValue("last_name")
rs, err := db.Exec("INSERT INTO person(first_name, last_name) VALUES (?, ?)", firstName, lastName)
if err != nil {
log.Fatalln(err)
}
id, err := rs.LastInsertId()
if err != nil {
log.Fatalln(err)
}
fmt.Println("insert person Id {}", id)
msg := fmt.Sprintf("insert successful %d", id)
c.JSON(http.StatusOK, gin.H{"msg": msg})
})
router.GET("/query", func(c *gin.Context) {
rows, err := db.Query("SELECT id, first_name, last_name FROM person")
defer rows.Close()
if err != nil {
log.Fatalln(err)
}
persons := make([]Person, 0)
for rows.Next() {
var person Person
rows.Scan(&person.Id, &person.FirstName, &person.LastName)
persons = append(persons, person)
}
if err = rows.Err(); err != nil {
log.Fatalln(err)
}
c.JSON(http.StatusOK, gin.H{
"persons": persons,
})
})
router.GET("/user/:name/*action", func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
message := name + " is " + action
c.String(http.StatusOK, message)
})
router.Run(":8080")
}