pongo2 - disabling template cache

If you use Gin framework and pongo2 template engine together, you should run gin in debug mode for disable template caching.

gin.SetMode(gin.DebugMode)

You no longer have to re-compile app when you make changes in templates.

Happy coding.


If you learn only methods, you’ll be tied to your methods. But if you learn principles, you can devise your own methods.

Ralph Waldo Emerson


Gorm Delete query

Gorm'da Delete queryleri atarken dikkat edilmesi gereken bir nokta:

db.DB.Where(Booking{ID: bookingID}).Delete(&Booking{})

Yukaridaki ornekte oldugu gibi where kismina struct verilince eger bookingID 0 ise Delete from bookings querysi calistiriyor. Mazallah yanlislikla butun tabloyu silebilirsiniz :)

Onun yerine soyle yapmak lazim:

db.DB.Where("id = ?", bookingID}).Delete(&Booking{})
# Delete from bookings where id = 0;

chromedp abort request

chromedp ile crawling yaparken actigimiz sayfada bazi dosyalarin indirilmesini engellemek isteyebiliriz. Bu sayede resim, css ve javascript dosyalarinin indirilmesini engelleyerek bandwidth'den kar edebilir, analytics.js gibi dosyalari engelleyerek farkedilirligimizi azaltabiliriz.

chromedp.ListenTarget(*ctx, BlockRequestURLIfMatch(*ctx, "analytics.js$"))
err := chromedp.Run(
    *ctx,
    fetch.Enable(),
    chromedp.Navigate("....")
)


func BlockRequestURLIfMatch(ctx context.Context, pattern string) func(event interface{}) {
    return func(event interface{}) {
        switch ev := event.(type) {
        case *fetch.EventRequestPaused:
            go func() {
                c := chromedp.FromContext(ctx)
                ctx := cdp.WithExecutor(ctx, c.Target)
                re := regexp.MustCompile(pattern)
                if re.FindString(ev.Request.URL) != "" {
                    fetch.FailRequest(ev.RequestID, network.ErrorReasonBlockedByClient).Do(ctx)
                } else {
                    fetch.ContinueRequest(ev.RequestID).Do(ctx)
                }
            }()
        }
    }
}


Gorm disable auto create/update

Gorm default davranis olarak bir modeli kaydettigimizde otomatik bagli oldugu modeli de kaydeder. Asagidaki gibi association gorm taglerini ekliyerek bunu engelleyebiliyoruz. Ben hem create, hem update'de bu ozelligi kapatmayi tercih ediyorum.

type Booking struct {
    AccountID     int
    Account       Account ` gorm:"foreignkey:AccountID;association_autoupdate:false;association_autocreate:false"`
}