-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgingorm.go
More file actions
31 lines (28 loc) · 833 Bytes
/
gingorm.go
File metadata and controls
31 lines (28 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package gg
import (
"hppr.dev/gg/mdl"
"github.com/gin-gonic/gin"
)
// SetModel configures the gin context to have references to the model and model schema
func SetModel(model interface{}) gin.HandlerFunc {
return func(ctx *gin.Context) {
schemaMap := getSchemaMap(ctx)
schema := schemaMap.GetSchema(model)
ctx.Set("ModelSchema", schema)
ctx.Set("MarshalType", mdl.CreateMarshalType(model, schema))
ctx.Set("Model", model)
ctx.Next()
}
}
// Middleware configures gin to use the gingorm middleware
func Middleware(cfg Config) gin.HandlerFunc {
db, _ := cfg.OpenDB()
schemaMap := setupSchemaMap(cfg, db)
defaultOutput := getOutputFunction(cfg.DefaultOutputFormat)
return func(ctx *gin.Context) {
ctx.Set("DB", db)
ctx.Set("SchemaMap", schemaMap)
ctx.Set("DefaultOutput", defaultOutput)
ctx.Next()
}
}