comparison pkg/controllers/json.go @ 486:b2dc9c2f69e0 metamorph-for-all

First stab to use the metamorphic db to do all database stuff.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 24 Aug 2018 13:56:06 +0200
parents c1047fd04a3a
children 8a0737aa6ab6
comparison
equal deleted inserted replaced
485:7a8644e9e50e 486:b2dc9c2f69e0
17 Result interface{} 17 Result interface{}
18 } 18 }
19 19
20 type JSONHandler struct { 20 type JSONHandler struct {
21 Input func() interface{} 21 Input func() interface{}
22 Handle func(interface{}, *http.Request, *sql.DB) (JSONResult, error) 22 Handle func(interface{}, *http.Request, *sql.Conn) (JSONResult, error)
23 NoConn bool
23 } 24 }
24 25
25 type JSONError struct { 26 type JSONError struct {
26 Code int 27 Code int
27 Message string 28 Message string
44 } 45 }
45 46
46 var jr JSONResult 47 var jr JSONResult
47 var err error 48 var err error
48 49
49 if token, ok := auth.GetToken(req); ok { 50 if token, ok := auth.GetToken(req); ok && !j.NoConn {
50 err = auth.ConnPool.Do(token, func(db *sql.DB) (err error) { 51 var session *auth.Session
51 jr, err = j.Handle(input, req, db) 52 if session, err = auth.ConnPool.Do(token); err != nil {
52 return err 53 var conn *sql.Conn
53 }) 54 if conn, err = auth.MetamorphConn(req.Context(), session.User); err != nil {
55 defer conn.Close()
56 jr, err = j.Handle(input, req, conn)
57 }
58 }
54 } else { 59 } else {
55 jr, err = j.Handle(input, req, nil) 60 jr, err = j.Handle(input, req, nil)
56 } 61 }
57 62
58 if err != nil { 63 if err != nil {