Enable query params to be defined on a connection

This commit is contained in:
Tobie Morgan Hitchcock 2021-04-15 23:02:35 +01:00
parent d79ae103ad
commit 4849eb4486
5 changed files with 59 additions and 0 deletions

View file

@ -138,6 +138,16 @@ func Process(fib *fibre.Context, ast *sql.Query, vars map[string]interface{}) (o
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
// so that we can assign it to the context
// and detect any websocket notifications.

View file

@ -71,6 +71,21 @@ func (r *rpc) Live(c *fibre.Context, class string) (interface{}, error) {
// 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) {
return db.Execute(c, sql, vars)
}

29
web/sess.go Normal file
View 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)
}
}
}

View file

@ -23,6 +23,7 @@ const (
varKeyTb = "TB"
varKeyId = "ID"
varKeyAuth = "auth"
varKeyVars = "vars"
varKeyUser = "user"
varKeyPass = "pass"
varKeyUniq = "uniq"

View file

@ -76,6 +76,10 @@ func Setup(opts *cnf.Options) (err error) {
// Setup authentication
s.Use(sess())
// Setup authentication
s.Use(auth())
// Setup live queries