Questions tagged [go-gin]

Gin is a HTTP web framework written in Go.

It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Docs

GitHub

755 questions
14
votes
2 answers

How to render static files within Gin router?

I want to serve a JSON file with gin server. And set some customize values in the HTML file. Use JavaScript in it to call the JSON file. My application structure: . ├── main.go └── templates ├── index.html └── web.json I put these basic…
02040402
  • 729
  • 3
  • 6
  • 21
13
votes
1 answer

How to set mock gin.Context for BindJSON

I'm setting up testing in Go. I use go-sqlmock to test mysql connection and Go Gin as framework. Now I try to test mysql insert logic. The problem is I need to set mock gin.Context which is used for BindJSON later. But I can't set this gin.Context…
jpskgc.v5
  • 249
  • 1
  • 5
  • 9
13
votes
1 answer

How to write a stream API using gin-gonic server in golang? Tried c.Stream didnt work

I wanted to create one streaming API using gin-gonic server in golang. func StreamData(c *gin.Context) { chanStream := make(chan int, 10) go func() {for i := 0; i < 5; i++ { chanStream <- i time.Sleep(time.Second * 1) …
kunalag
  • 131
  • 1
  • 1
  • 3
12
votes
2 answers

Unit Testing With Gin-Gonic

My project is split into three main components: controllers, services, and models. When a route is queried via the URI, the controllers are called, which then call the services to interact with the models, which then interact with the database via…
Evan Bloemer
  • 1,051
  • 2
  • 11
  • 31
12
votes
3 answers

Disable request logging for a particular Go-Gin route

I have a bunch of routes and start off gin with gin.Default() (enabling logging and recovery for all routes by default). But there is one route (i.e. /health) that gets pinged every 5 seconds. What's a straightforward way to disable request logging…
Paul Lam
  • 1,729
  • 1
  • 15
  • 25
12
votes
2 answers

Golang Gin-Gonic Split Routes into Multiple Files

I am looking to split my routes.go into multiple files so that each group is in its own package. Can someone point me to an example of some code where someone has done this with Gin? i.e. package auth ... auth = route.Group("/auth"){ …
spdrman
  • 1,372
  • 2
  • 13
  • 18
12
votes
3 answers

How to return html on Gin?

I'm trying to render a HTML that's already on a string instead of rendering a template on Gin framework. The c.HTML function on GET("/") function expects a template to be rendered. But on POST("/markdown") I've rendered that HTML on a string…
AndreDurao
  • 5,600
  • 7
  • 41
  • 61
11
votes
1 answer

How to unit test a Go Gin handler function?

I have a controller function like this.... func GetMaterialByFilter(c *gin.Context) { queryParam := weldprogs.QueryParam{} c.BindQuery(&queryParam) materialByFilter, getErr := services.WeldprogService.GetMaterialByFilter(&queryParam) …
Hsn
  • 1,168
  • 2
  • 16
  • 39
11
votes
3 answers

How to use gin as a server to write prometheus exporter metrics

This is the official prometheus golang-client example: package main import ( "log" "net/http" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) var cpuTemp =…
gophergfer
  • 261
  • 4
  • 9
11
votes
3 answers

Programmatically set an url parameter in gin context for testing purpose

I am writing some test suites for gin middlewares. I found a solution to test them without having to run a full router engine, by creating a gin context like this : w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) The goal is to test my…
KawaLo
  • 1,783
  • 3
  • 16
  • 34
11
votes
1 answer

How to group routes in gin?

I want to group my routes in different files, so the main file won't be very messy. I want something like this in their own files: v1 := router.Group("/v1") { v1.Group("users", usersRoutes) v1.Group("pictures", picturesRoutes) …
akko
  • 559
  • 3
  • 8
  • 17
11
votes
2 answers

unsupported Scan, storing driver.Value type []uint8 into type *[]string

I have implemented rest api using golang, gin and gorp Employee structure: type Employee struct { Id int64 `db:"id" json:"id"` Firstname string `db:"firstname" json:"firstname"` Lastname string `db:"lastname" json:"lastname"` …
Prashant4224
  • 1,551
  • 1
  • 14
  • 21
11
votes
3 answers

Golang gin gonic web framework proxy route to another backend

How to reverse proxy web requests for a few routes to another backend in Gin Gonic web golang framework Is there a way to directly forward in the Handle function as shown below? router := gin.New() router.Handle("POST", "/api/v1/endpoint1",…
alpha_cod
  • 1,933
  • 5
  • 25
  • 43
10
votes
1 answer

How to set data in gin request context?

I have a very simple piece of code. One single middleware is applied to all routes. In this middleware, the header field 'x-sentinel-tenant' is read. If it is blank, an error is returned. If it has some value, then that value is to be extracted and…
Harshil
  • 436
  • 1
  • 7
  • 16
10
votes
3 answers

Go Gin framework to log in JSON

I am setting up a small API using Go Gin, however I am unable to convince the logger to output JSON. By default it's a key/value string, but I need it as json. How can I achieve that? I feel it should be easily support but I've been struggling with…
Andrei Dascalu
  • 1,059
  • 2
  • 14
  • 26
1
2
3
50 51