These are chat archives for gin-gonic/gin

30th
Apr 2017
kebeeyong
@kebeeyong
Apr 30 2017 16:09
Hi everyone

I can't seem to findout how to use go gin-gonic to perform CRUD or GET, PUT and POST with MongoDB

Below is my code, please Kindly assist me

package main

import (
//"encoding/json"
"fmt"
"log"
"net/http"
"os"

// "goji.io"
// "goji.io/pat"
"gopkg.in/gin-gonic/gin.v1"
"gopkg.in/mgo.v2"
// "gopkg.in/mgo.v2/bson"
// "github.com/rs/cors"

)

func ErrorWithJSON(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, "{message: %q}", message)
}

func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(json)
}

type Poll struct {
ID string json:"id,omitempty"
Firstname string json:"firstname,omitempty"
Lastname string json:"lastname,omitempty"
Poll string json:"poll,omitempty"
// Address *Address json:"address,omitempty"
}

func main() {
port := os.Getenv("PORT")

if port == "" {
log.Fatal("$PORT must be set")
}

session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()

session.SetMode(mgo.Monotonic, true)
ensureIndex(session)

router := gin.Default()

router.GET("/polls", func(c * gin.Context) {
//c.HTML(http.StatusOK, "polls", nil)
//var polls []Poll
//err := c.Find(bson.M{}).All(&polls)

//  **Help Needed Here**

})

router.Run(":" + port)
}

func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()

c := session.DB("store").C("polls")

index := mgo.Index{
Key: []string{"id"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}]

Thomas Boerger
@tboerger
Apr 30 2017 16:14
if you properly format the code somebody can help you
kebeeyong
@kebeeyong
Apr 30 2017 16:14
ok

Below is my code, please Kindly assist me

package main

import (
//"encoding/json"
"fmt"
"log"
"net/http"
"os"

// "goji.io"
// "goji.io/pat"
"gopkg.in/gin-gonic/gin.v1"
"gopkg.in/mgo.v2"
// "gopkg.in/mgo.v2/bson"
// "github.com/rs/cors"

)

func ErrorWithJSON(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, "{message: %q}", message)
}

func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(json)
}

type Poll struct {
ID string json:"id,omitempty"
Firstname string json:"firstname,omitempty"
Lastname string json:"lastname,omitempty"
Poll string json:"poll,omitempty"
// Address *Address json:"address,omitempty"
}

func main() {
port := os.Getenv("PORT")

if port == "" {
log.Fatal("$PORT must be set")
}

session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()

session.SetMode(mgo.Monotonic, true)
ensureIndex(session)

router := gin.Default()

router.GET("/polls", func(c * gin.Context) {
//c.HTML(http.StatusOK, "polls", nil)
//var polls []Poll
//err := c.Find(bson.M{}).All(&polls)

//  **Help Needed Here**

})

router.Run(":" + port)
}

func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()

c := session.DB("store").C("polls")

index := mgo.Index{
Key: []string{"id"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}]

Thomas Boerger
@tboerger
Apr 30 2017 16:15
if you are not familar with markdown code formatting use a gist or something like that
kebeeyong
@kebeeyong
Apr 30 2017 16:15

Below is my code, please Kindly assist me

package main

import (
//"encoding/json"
"fmt"
"log"
"net/http"
"os"

// "goji.io"
// "goji.io/pat"
"gopkg.in/gin-gonic/gin.v1"
"gopkg.in/mgo.v2"
// "gopkg.in/mgo.v2/bson"
// "github.com/rs/cors"

)

func ErrorWithJSON(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, "{message: %q}", message)
}

func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(json)
}

type Poll struct {
ID string json:"id,omitempty"
Firstname string json:"firstname,omitempty"
Lastname string json:"lastname,omitempty"
Poll string json:"poll,omitempty"
// Address *Address json:"address,omitempty"
}

func main() {
port := os.Getenv("PORT")

if port == "" {
log.Fatal("$PORT must be set")
}

session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()

session.SetMode(mgo.Monotonic, true)
ensureIndex(session)

router := gin.Default()

router.GET("/polls", func(c * gin.Context) {
//c.HTML(http.StatusOK, "polls", nil)
//var polls []Poll
//err := c.Find(bson.M{}).All(&polls)

//  **Help Needed Here**

})

router.Run(":" + port)
}

func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()

c := session.DB("store").C("polls")

index := mgo.Index{
Key: []string{"id"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}]

Thomas Boerger
@tboerger
Apr 30 2017 16:15
and beside that you can also edit a previous message...
kebeeyong
@kebeeyong
Apr 30 2017 16:16
@tboerger or a link
do i share a link on stackoverflow
or github?
Thomas Boerger
@tboerger
Apr 30 2017 16:18
https://gist.github.com/ should work well enough
kebeeyong
@kebeeyong
Apr 30 2017 16:18
thanks
thats the link on gist.github.com
Thomas Boerger
@tboerger
Apr 30 2017 16:22
and it’s still poor formatted -.-
kebeeyong
@kebeeyong
Apr 30 2017 16:22
lemme retry
better now i guess
Thomas Boerger
@tboerger
Apr 30 2017 16:24
first of all, you don’T need ErrorWithJSON or ResponseWithJSON
kebeeyong
@kebeeyong
Apr 30 2017 16:24
ok
Thomas Boerger
@tboerger
Apr 30 2017 16:25
gin got c.JSON
and than you need to define different routes for the crud actions
kebeeyong
@kebeeyong
Apr 30 2017 16:27
ok
Thomas Boerger
@tboerger
Apr 30 2017 16:27
https://github.com/kleister/kleister-api/blob/master/api/key.go these are the handlers used by these routes
kebeeyong
@kebeeyong
Apr 30 2017 16:28
ok
den how do u interact with mongodb
mgo
can you please help me rewrite the code please i'm very new in go, still learning
thanks
Thomas Boerger
@tboerger
Apr 30 2017 17:01
If I'm writing that for you, you won't learn anything
kebeeyong
@kebeeyong
Apr 30 2017 17:02
i'm also trying something here too
Thomas Boerger
@tboerger
Apr 30 2017 17:03
Beside that I'm on mobile now
kebeeyong
@kebeeyong
Apr 30 2017 17:03
ok
kebeeyong
@kebeeyong
Apr 30 2017 17:14
just want to learn how to build a restapi in go with gin-gonic and mongodb
David Marchbanks
@ptdave20
Apr 30 2017 17:20
Pending on what identity method you want to use, like jwt or rolling your own. I write middleware to do my checks and apply it to my API routes appropriately
Thomas Boerger
@tboerger
Apr 30 2017 17:20
I'm still searching for something properly for acl :)
David Marchbanks
@ptdave20
Apr 30 2017 17:24
Honestly, jwt is good for transmitting data you will commonly used between calls. There is a few out there. Last I checked you had to write your own middleware wrapper
Thomas Boerger
@tboerger
Apr 30 2017 17:31
yeah i’m not talking about jwt… i’m talking about properly managing access to specific resources.
access control
i will search at https://golanglibs.com/search?q=acl now :P