Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Cristian
@cristianchaparroa
 path := removeParams(c)  // remove params in case of foo/:param 
....
func removeParams(c *gin.Context) string {
    url := c.Request.URL.String()

    fmt.Println(c.Params)
    for _, p := range c.Params {
        url = strings.Replace(url, p.Value, "", 1)
    }
    return url
}
sorushsaghari
@sorushsaghari
hi guys do you khnow what is the usage of C *gin.context in
type UserSerializer struct {
    C    *gin.Context
    UserModel
}
Phillip Couto
@phillipCouto
@sorushsaghari what package is that in?
First glance it is just attaching a reference to the structure for ease of use instead of passing in as an argument in any function calls on an instance of the structure.
Richard
@moos3
i have a question, if I wanted to generate a token or url that disappeared in 15 minutes after creation, how would I handle that time.Time call ?
Phillip Couto
@phillipCouto
@moos3 for me I usually consider two options. Creating a table in a database to track them if I want to be able to expire them sooner or you create a link that uses an HMAC to verify the parameters in the link are valid.
The key for the HMAC can only be know by the application so that a malicious user can't craft their own.
Henri Idrovo
@henriguy1210

Hi I'm wondering how people handle environment specific configurations with a gin app, I'd love to hear how the community here handles that!

I have a gin app that reads from a cassandra db. So I want to setup up three different configurations for cassandra(dev, test, prod). Thanks!

Richard
@moos3
@phillipCouto yeah I'm playing with tracking them now, just trying to figure out the HMAC
Iurii
@pyxru_gitlab
Hello every! First of all wanna say so much thank for gin, it is best of the best.
Second one, i create simple middleware package for check user on sing in or not, may be will be helpful for some one
Stanislav Valasek
@valasek
I made a starter kit using Gin and Quasar, material design Vue.js framework. PRs, new feature requests, issues, and your feedback are welcomed. https://github.com/valasek/quasar-starter-kit-go-gin
Piyush Bhangale
@officialpiyush
Hi
I had a question, how can we get the params from the url.
With this, I mean that i have a path /i/<code>, where <code is what i want to get
How can I do that?
sorushsaghari
@sorushsaghari
c.Params c.Query
Phillip Couto
@phillipCouto
@moos3 did you figure out HMAC?
DoctorNasa
@DoctorNasa
hi there
Ryan Luo
@Senmumu
hi there
Tiago Cardoso
@tiagoacardoso
Yo folks, is it possible to redirect a request to NoRoute handler?
Tiago Cardoso
@tiagoacardoso
any other way than use context Redirect to a route that doesn't exists?
Phillip Couto
@phillipCouto
@tiagoacardoso create the NoRoute handler as a separate function. Then in the code you want to redirect from just call your function and return to make sure nothing else runs.
Stefanos Chrs
@stefanoschrs
Hello, I have a question, is there any way to skip logging the 200 status requests? There is not much documentation on the logger middleware from what I searched. I'm looking for something like https://github.com/expressjs/morgan#skip
MZERO
@flymzero
test
MZERO
@flymzero
test
Phillip Couto
@phillipCouto
@stefanoschrs I would create a new router with a custom middle ware that checks status code and if not 200 passes the context to the logger:
I would provide a code sample but I can't from my device. If you want I can type one up later when i am on my laptop.
Gopher1980
@gopher1980
HI
I need your opinion of the my little module go for create CRUD on GORM. This module can work with mux or gingonic.
https://github.com/gopher1980/gormcrud
If you want help me, I will thank
Gopher1980
@gopher1980

other ide is use TableName
var suffix string

// Record is the one entity
type Record struct {
ID uint gorm:"primary_key" json:"id"
CreatedAt time.Time json:"created_at"
UpdatedAt time.Time json:"updated_at"
DeletedAt *time.Time json:"deleted_at"
Name string json:"name"
}

func (e Record) TableName() string {

return "record_" + suffix

}

and use suffix = "1"
db.AutoMigrate(&Record{})
suffix = "2"
db.AutoMigrate(&Record{})
I was wrong chat
Richard
@moos3
question, when using a logger middle ware like zerolog, how do I call it in my handlers when I want to log errors?
Rohit Roy Chowdhury
@roychowdhuryrohit-dev
How to validate if a json value was empty for a boolean field of a struct model?
John Napiorkowski
@jjn1056
I'm studying custom validation (https://gin-gonic.com/docs/examples/custom-validators/) and wondering if there's a best practice approach when you custom validation needs to access the database (for example to enforce uniqueness for a username, or require a password to be different than the last 3). Some examples I find via Google suggest just making the DB handler global and adding the SQL directly into the registered validation method. Does that seem like the approach to take? I'm still new to Golang and the idea of a global makes me itchy.
thanks for anything responses, thoughts.
John Napiorkowski
@jjn1056
a smaller question you might be able to answer, I can't seem to find online docs for the Gin API (for example c.ShouldBindWith). Is there an annotated API someplace or do you all just read the source code?
Riley J.
@the-rileyj
Reading the source code or using a search engine with the specific method is what I do
I am having issues with overwriting the 404 returned from NoRoute, I want it to be 200 and it will not set it correctly
Riley J.
@the-rileyj
nevermind, I got it figured out; I was writing the status after writing the content, which caused the 404. Writing the status first solved the problem
bugproof
@bugproof
is this benchmark accurate?
image.png
Joel Mislav Kunst
@7sedam7
hello, i have a more general golang question
Kaden Nelson
@KadenLNelson
func AWSSession() gin.HandlerFunc {
    return func(c *gin.Context) {
        session := session.Must(session.NewSession())

        c.Set("AWSSession", session)

        c.Next()
    }
}

I'm using using this middleware to add an aws session to the Gin context. The session doc reads

Sessions should be cached when possible, because creating a new Session will load all configuration values from the environment, and config files each time the Session is created.

Is there a better way to initialize this session instead of initializing it on each middleware/api call?

Bruce Wang
@BruceWangNo1

Hello everyone, my name is Yong Wang. And I am a graduate student from Sun Yat-sen University, China. I am about to graduate in June next year. So I have to work on my graduation project pretty soon. Some of my friends have already started. My supervisor is pretty open to what project I am into. I am proficient at Go. And I have a fair amount of working knowledge of API gateway from the github.com/fagongzi/gateway project and microservices when I was doing an internship at Tencent Games. I am looking to work on an open-source project where my work could eventually be concluded as my graduation thesis. There is one requirement which is that my work on the graduation project needs to have at least two novel approaches to some problems. Is it feasible? If so, would you please suggest some problems that I can work on?

Looking forward to your replies. Any feedbacks are welcome.

Sincerely,
Yong

Shaurya
@shaurya-xyz
Hi all, my name is shaurya, I was wondering if someone can explain to me how the validation of api calls made from client side once a user is authenticated. What I am trying to understand is given the backend is in go gin and the authentication system creates a session post login, user would have access to make api calls. Now in case of web app frontend which is opened in a browser would have the session assigned to it, wouldn't it be possible let's say by using a browser extension for rest api to query resources of a different user ? For example a query which returns notes of a user, with an endpoint /v1/mynotes with parameters userid can be manipulated of a different user and get all the notes ? How does this really work in real world applications ?
findridoy
@findridoy
Why gin engine not work outside function?
I tried it didn't work. Inside function it works.
Thanabodee Charoenpiriyakij
@wingyplus
@findridoy Could you provide an example?