Enable query params to be defined on a connection
This commit is contained in:
parent
d79ae103ad
commit
4849eb4486
5 changed files with 59 additions and 0 deletions
10
db/db.go
10
db/db.go
|
@ -138,6 +138,16 @@ func Process(fib *fibre.Context, ast *sql.Query, vars map[string]interface{}) (o
|
||||||
vars = make(map[string]interface{})
|
vars = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch any variables which have been set
|
||||||
|
// on the connection. These might have been
|
||||||
|
// set on the WebSocket or using HTTP Headers.
|
||||||
|
|
||||||
|
if sess := fib.Get(ctxKeyVars); sess != nil {
|
||||||
|
for key, val := range sess.(map[string]interface{}) {
|
||||||
|
vars[key] = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get the unique id for this connection
|
// Get the unique id for this connection
|
||||||
// so that we can assign it to the context
|
// so that we can assign it to the context
|
||||||
// and detect any websocket notifications.
|
// and detect any websocket notifications.
|
||||||
|
|
15
web/rpc.go
15
web/rpc.go
|
@ -71,6 +71,21 @@ func (r *rpc) Live(c *fibre.Context, class string) (interface{}, error) {
|
||||||
// Methods for static queries
|
// Methods for static queries
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
|
|
||||||
|
func (r *rpc) Let(c *fibre.Context, key string, val interface{}) (interface{}, error) {
|
||||||
|
switch val := val.(type) {
|
||||||
|
case *fibre.RPCNull:
|
||||||
|
vars := c.Get("vars").(map[string]interface{})
|
||||||
|
delete(vars, key)
|
||||||
|
c.Set("vars", vars)
|
||||||
|
return vars, nil
|
||||||
|
default:
|
||||||
|
vars := c.Get("vars").(map[string]interface{})
|
||||||
|
vars[key] = val
|
||||||
|
c.Set("vars", vars)
|
||||||
|
return vars, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *rpc) Query(c *fibre.Context, sql string, vars map[string]interface{}) (interface{}, error) {
|
func (r *rpc) Query(c *fibre.Context, sql string, vars map[string]interface{}) (interface{}, error) {
|
||||||
return db.Execute(c, sql, vars)
|
return db.Execute(c, sql, vars)
|
||||||
}
|
}
|
||||||
|
|
29
web/sess.go
Normal file
29
web/sess.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
// Copyright © 2016 Abcum Ltd
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/abcum/fibre"
|
||||||
|
)
|
||||||
|
|
||||||
|
func sess() fibre.MiddlewareFunc {
|
||||||
|
return func(h fibre.HandlerFunc) fibre.HandlerFunc {
|
||||||
|
return func(c *fibre.Context) (err error) {
|
||||||
|
vars := make(map[string]interface{})
|
||||||
|
c.Set(varKeyVars, vars)
|
||||||
|
return h(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ const (
|
||||||
varKeyTb = "TB"
|
varKeyTb = "TB"
|
||||||
varKeyId = "ID"
|
varKeyId = "ID"
|
||||||
varKeyAuth = "auth"
|
varKeyAuth = "auth"
|
||||||
|
varKeyVars = "vars"
|
||||||
varKeyUser = "user"
|
varKeyUser = "user"
|
||||||
varKeyPass = "pass"
|
varKeyPass = "pass"
|
||||||
varKeyUniq = "uniq"
|
varKeyUniq = "uniq"
|
||||||
|
|
|
@ -76,6 +76,10 @@ func Setup(opts *cnf.Options) (err error) {
|
||||||
|
|
||||||
// Setup authentication
|
// Setup authentication
|
||||||
|
|
||||||
|
s.Use(sess())
|
||||||
|
|
||||||
|
// Setup authentication
|
||||||
|
|
||||||
s.Use(auth())
|
s.Use(auth())
|
||||||
|
|
||||||
// Setup live queries
|
// Setup live queries
|
||||||
|
|
Loading…
Reference in a new issue