added a small helper function
Dieser Commit ist enthalten in:
Ursprung
b5800f09c2
Commit
8d9b870820
|
@ -3,6 +3,7 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -25,6 +26,14 @@ var _ ConnGet = NewMiddleware().GetConn
|
||||||
var _ middleware.Middleware = &Middleware{}
|
var _ middleware.Middleware = &Middleware{}
|
||||||
var _ middleware.PostSetupMiddleware = &Middleware{}
|
var _ middleware.PostSetupMiddleware = &Middleware{}
|
||||||
|
|
||||||
|
// GetConnection is an simple helper function that returns an connection to the db
|
||||||
|
func GetConnection(c *gin.Context, db string) (*pgxpool.Conn, error) {
|
||||||
|
if co, ok := c.Get(ContextKey); ok {
|
||||||
|
return co.(ConnGet)(db), nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("No db.Middleware set up. ")
|
||||||
|
}
|
||||||
|
|
||||||
// Middleware return a handler that sets the db into the context of every request.
|
// Middleware return a handler that sets the db into the context of every request.
|
||||||
// uri is an url in the form dbtype:connectargs
|
// uri is an url in the form dbtype:connectargs
|
||||||
type Middleware struct {
|
type Middleware struct {
|
||||||
|
|
Laden…
In neuem Issue referenzieren