Skip to content

Instantly share code, notes, and snippets.

@armeo
Forked from EtienneR/main.go
Created February 18, 2016 16:57
Show Gist options
  • Save armeo/40e1166aecd0289628dd to your computer and use it in GitHub Desktop.
Save armeo/40e1166aecd0289628dd to your computer and use it in GitHub Desktop.

Revisions

  1. @EtienneR EtienneR revised this gist Feb 14, 2016. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ import (
    "gopkg.in/gorp.v1"
    "log"
    "strconv"

    "github.com/gin-gonic/gin"
    _ "github.com/go-sql-driver/mysql"
    )
    @@ -54,6 +54,8 @@ func main() {
    v1.POST("/users", PostUser)
    v1.PUT("/users/:id", UpdateUser)
    v1.DELETE("/users/:id", DeleteUser)
    v1.OPTIONS("/users", OptionsUser) // POST
    v1.OPTIONS("/users/:id", OptionsUser) // PUT, DELETE
    }

    r.Run(":8080")
    @@ -116,7 +118,7 @@ func PostUser(c *gin.Context) {
    }

    } else {
    c.JSON(400, gin.H{"error": "fields are empty"})
    c.JSON(400, gin.H{"error": "Fields are empty"})
    }

    // curl -i -X POST -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Queen\" }" http://localhost:8080/api/v1/users
    @@ -179,4 +181,11 @@ func DeleteUser(c *gin.Context) {
    }

    // curl -i -X DELETE http://localhost:8080/api/v1/users/1
    }
    }

    func OptionsUser(c *gin.Context) {
    c.Writer.Header().Add("Access-Control-Allow-Origin", "*")
    c.Writer.Header().Set("Access-Control-Allow-Methods", "DELETE,POST, PUT")
    c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type")
    c.Next()
    }
  2. @EtienneR EtienneR revised this gist Feb 14, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -35,9 +35,18 @@ func checkErr(err error, msg string) {
    }
    }

    func Cors() gin.HandlerFunc {
    return func(c *gin.Context) {
    c.Writer.Header().Add("Access-Control-Allow-Origin", "*")
    c.Next()
    }
    }

    func main() {
    r := gin.Default()

    r.Use(Cors())

    v1 := r.Group("api/v1")
    {
    v1.GET("/users", GetUsers)
  3. @EtienneR EtienneR revised this gist Jan 12, 2016. No changes.
  4. @EtienneR EtienneR revised this gist Jan 12, 2016. No changes.
  5. @EtienneR EtienneR revised this gist Jan 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.go
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ func main() {
    }

    func GetUsers(c *gin.Context) {
    var users []User
    var users []User
    _, err := dbmap.Select(&users, "SELECT * FROM user")

    if err == nil {
  6. @EtienneR EtienneR revised this gist Jan 12, 2016. 1 changed file with 0 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -38,11 +38,6 @@ func checkErr(err error, msg string) {
    func main() {
    r := gin.Default()

    r.Static("/js", "./js")
    r.LoadHTMLGlob("views/*")

    r.GET("/", indexHandler)

    v1 := r.Group("api/v1")
    {
    v1.GET("/users", GetUsers)
    @@ -55,10 +50,6 @@ func main() {
    r.Run(":8080")
    }

    func indexHandler(c *gin.Context) {
    c.HTML(200, "index.html", nil)
    }

    func GetUsers(c *gin.Context) {
    var users []User
    _, err := dbmap.Select(&users, "SELECT * FROM user")
  7. @EtienneR EtienneR revised this gist Jan 12, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,6 @@ func main() {

    r.GET("/", indexHandler)

    // Déclaration des routes
    v1 := r.Group("api/v1")
    {
    v1.GET("/users", GetUsers)
    @@ -53,7 +52,6 @@ func main() {
    v1.DELETE("/users/:id", DeleteUser)
    }

    // Déclartion du port
    r.Run(":8080")
    }

  8. @EtienneR EtienneR created this gist Jan 12, 2016.
    184 changes: 184 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,184 @@
    package main

    import (
    "database/sql"
    "gopkg.in/gorp.v1"
    "log"
    "strconv"

    "github.com/gin-gonic/gin"
    _ "github.com/go-sql-driver/mysql"
    )

    type User struct {
    Id int64 `db:"id" json:"id"`
    Firstname string `db:"firstname" json:"firstname"`
    Lastname string `db:"lastname" json:"lastname"`
    }

    var dbmap = initDb()

    func initDb() *gorp.DbMap {
    db, err := sql.Open("mysql", "root:@/myapi")
    checkErr(err, "sql.Open failed")
    dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
    dbmap.AddTableWithName(User{}, "User").SetKeys(true, "Id")
    err = dbmap.CreateTablesIfNotExists()
    checkErr(err, "Create tables failed")

    return dbmap
    }

    func checkErr(err error, msg string) {
    if err != nil {
    log.Fatalln(msg, err)
    }
    }

    func main() {
    r := gin.Default()

    r.Static("/js", "./js")
    r.LoadHTMLGlob("views/*")

    r.GET("/", indexHandler)

    // Déclaration des routes
    v1 := r.Group("api/v1")
    {
    v1.GET("/users", GetUsers)
    v1.GET("/users/:id", GetUser)
    v1.POST("/users", PostUser)
    v1.PUT("/users/:id", UpdateUser)
    v1.DELETE("/users/:id", DeleteUser)
    }

    // Déclartion du port
    r.Run(":8080")
    }

    func indexHandler(c *gin.Context) {
    c.HTML(200, "index.html", nil)
    }

    func GetUsers(c *gin.Context) {
    var users []User
    _, err := dbmap.Select(&users, "SELECT * FROM user")

    if err == nil {
    c.JSON(200, users)
    } else {
    c.JSON(404, gin.H{"error": "no user(s) into the table"})
    }

    // curl -i http://localhost:8080/api/v1/users
    }

    func GetUser(c *gin.Context) {
    id := c.Params.ByName("id")
    var user User
    err := dbmap.SelectOne(&user, "SELECT * FROM user WHERE id=? LIMIT 1", id)

    if err == nil {
    user_id, _ := strconv.ParseInt(id, 0, 64)

    content := &User{
    Id: user_id,
    Firstname: user.Firstname,
    Lastname: user.Lastname,
    }
    c.JSON(200, content)
    } else {
    c.JSON(404, gin.H{"error": "user not found"})
    }

    // curl -i http://localhost:8080/api/v1/users/1
    }

    func PostUser(c *gin.Context) {
    var user User
    c.Bind(&user)

    log.Println(user)

    if user.Firstname != "" && user.Lastname != "" {

    if insert, _ := dbmap.Exec(`INSERT INTO user (firstname, lastname) VALUES (?, ?)`, user.Firstname, user.Lastname); insert != nil {
    user_id, err := insert.LastInsertId()
    if err == nil {
    content := &User{
    Id: user_id,
    Firstname: user.Firstname,
    Lastname: user.Lastname,
    }
    c.JSON(201, content)
    } else {
    checkErr(err, "Insert failed")
    }
    }

    } else {
    c.JSON(400, gin.H{"error": "fields are empty"})
    }

    // curl -i -X POST -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Queen\" }" http://localhost:8080/api/v1/users
    }

    func UpdateUser(c *gin.Context) {
    id := c.Params.ByName("id")
    var user User
    err := dbmap.SelectOne(&user, "SELECT * FROM user WHERE id=?", id)

    if err == nil {
    var json User
    c.Bind(&json)

    user_id, _ := strconv.ParseInt(id, 0, 64)

    user := User{
    Id: user_id,
    Firstname: json.Firstname,
    Lastname: json.Lastname,
    }

    if user.Firstname != "" && user.Lastname != "" {
    _, err = dbmap.Update(&user)

    if err == nil {
    c.JSON(200, user)
    } else {
    checkErr(err, "Updated failed")
    }

    } else {
    c.JSON(400, gin.H{"error": "fields are empty"})
    }

    } else {
    c.JSON(404, gin.H{"error": "user not found"})
    }

    // curl -i -X PUT -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Merlyn\" }" http://localhost:8080/api/v1/users/1
    }

    func DeleteUser(c *gin.Context) {
    id := c.Params.ByName("id")

    var user User
    err := dbmap.SelectOne(&user, "SELECT * FROM user WHERE id=?", id)

    if err == nil {
    _, err = dbmap.Delete(&user)

    if err == nil {
    c.JSON(200, gin.H{"id #" + id: "deleted"})
    } else {
    checkErr(err, "Delete failed")
    }

    } else {
    c.JSON(404, gin.H{"error": "user not found"})
    }

    // curl -i -X DELETE http://localhost:8080/api/v1/users/1
    }