Remove connection wide variables

This commit is contained in:
Tobie Morgan Hitchcock 2018-05-09 23:09:51 +01:00
parent 0615896c0e
commit c8980e2eac
7 changed files with 29 additions and 47 deletions

View file

@ -203,13 +203,6 @@ func Process(fib *fibre.Context, ast *sql.Query, vars map[string]interface{}) (o
ctx = context.WithValue(ctx, ctxKeyId, id) ctx = context.WithValue(ctx, ctxKeyId, id)
// Assign any global connection variables
// to the context so that we can retrieve
// the variables from within any queries.
keep := fib.Get(varKeyKeep)
ctx = context.WithValue(ctx, ctxKeyKeep, keep)
// Assign the authentication data to the // Assign the authentication data to the
// context so that we can log the auth kind // context so that we can log the auth kind
// and the auth variable data to the request. // and the auth variable data to the request.

View file

@ -117,7 +117,6 @@ func (e *executor) execute(ctx context.Context, ast *sql.Query) {
"id": ctx.Value(ctxKeyId), "id": ctx.Value(ctxKeyId),
"kind": ctx.Value(ctxKeyKind), "kind": ctx.Value(ctxKeyKind),
"vars": ctx.Value(ctxKeyVars), "vars": ctx.Value(ctxKeyVars),
"keep": ctx.Value(ctxKeyKeep),
}) })
if stm, ok := stm.(sql.AuthableStatement); ok { if stm, ok := stm.(sql.AuthableStatement); ok {

View file

@ -61,9 +61,6 @@ func (s *socket) ctx(ns, db string) (ctx context.Context) {
ctx = context.WithValue(ctx, ctxKeyNs, ns) ctx = context.WithValue(ctx, ctxKeyNs, ns)
ctx = context.WithValue(ctx, ctxKeyDb, db) ctx = context.WithValue(ctx, ctxKeyDb, db)
keep := s.fibre.Get(ctxKeyKeep)
ctx = context.WithValue(ctx, ctxKeyKeep, keep)
auth := s.fibre.Get(ctxKeyAuth).(*cnf.Auth) auth := s.fibre.Get(ctxKeyAuth).(*cnf.Auth)
ctx = context.WithValue(ctx, ctxKeyAuth, auth.Data) ctx = context.WithValue(ctx, ctxKeyAuth, auth.Data)
ctx = context.WithValue(ctx, ctxKeyKind, auth.Kind) ctx = context.WithValue(ctx, ctxKeyKind, auth.Kind)

View file

@ -49,7 +49,6 @@ const (
ctxKeyVars = "vars" ctxKeyVars = "vars"
ctxKeySubs = "subs" ctxKeySubs = "subs"
ctxKeySpec = "spec" ctxKeySpec = "spec"
ctxKeyKeep = "keep"
ctxKeyAuth = "auth" ctxKeyAuth = "auth"
ctxKeyKind = "kind" ctxKeyKind = "kind"
ctxKeyScope = "scope" ctxKeyScope = "scope"
@ -62,7 +61,6 @@ const (
varKeyIp = "ip" varKeyIp = "ip"
varKeyEnv = "ENV" varKeyEnv = "ENV"
varKeyAuth = "auth" varKeyAuth = "auth"
varKeyKeep = "keep"
varKeyThis = "this" varKeyThis = "this"
varKeyScope = "scope" varKeyScope = "scope"
varKeyValue = "value" varKeyValue = "value"
@ -108,5 +106,5 @@ var (
// paramSearchKeys specifies the order in which context // paramSearchKeys specifies the order in which context
// variables should be checked for any specified value. // variables should be checked for any specified value.
paramSearchKeys = []string{ctxKeySpec, ctxKeySubs, ctxKeyVars, ctxKeyKeep} paramSearchKeys = []string{ctxKeySpec, ctxKeySubs, ctxKeyVars}
) )

View file

@ -31,30 +31,12 @@ import (
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/abcum/surreal/util/data"
) )
var ignore = func() error { var ignore = func() error {
return nil return nil
} }
const (
varKeyIp = "ip"
varKeyNs = "NS"
varKeyDb = "DB"
varKeySc = "SC"
varKeyTk = "TK"
varKeyUs = "US"
varKeyTb = "TB"
varKeyId = "ID"
varKeyAuth = "auth"
varKeyKeep = "keep"
varKeyUser = "user"
varKeyPass = "pass"
varKeyOrigin = "origin"
)
func cidr(ip net.IP, networks []*net.IPNet) bool { func cidr(ip net.IP, networks []*net.IPNet) bool {
for _, network := range networks { for _, network := range networks {
if network.Contains(ip) { if network.Contains(ip) {
@ -68,13 +50,6 @@ func auth() fibre.MiddlewareFunc {
return func(h fibre.HandlerFunc) fibre.HandlerFunc { return func(h fibre.HandlerFunc) fibre.HandlerFunc {
return func(c *fibre.Context) (err error) { return func(c *fibre.Context) (err error) {
// Initialise any session level variables
// which will be valid across all requests
// which are made over this connection.
vars := new(data.Doc)
c.Set(varKeyKeep, vars)
// Initialise the connection authentication // Initialise the connection authentication
// information which will store whether the // information which will store whether the
// connection has authenticated or not. // connection has authenticated or not.

View file

@ -19,8 +19,6 @@ import (
"github.com/abcum/surreal/cnf" "github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/db" "github.com/abcum/surreal/db"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/data"
"github.com/abcum/surreal/util/rand"
) )
type rpc struct{} type rpc struct{}
@ -29,8 +27,6 @@ type rpc struct{}
// Methods for authentication // Methods for authentication
// -------------------------------------------------- // --------------------------------------------------
func (r *rpc) Uniq(c *fibre.Context) (interface{}, error) {
return rand.String(64), nil
func (r *rpc) Ping(c *fibre.Context) (interface{}, error) { func (r *rpc) Ping(c *fibre.Context) (interface{}, error) {
return "OK", nil return "OK", nil
} }
@ -75,10 +71,6 @@ 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) {
return c.Get("keep").(*data.Doc).Set(val, key)
}
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)
} }

28
web/vars.go Normal file
View file

@ -0,0 +1,28 @@
// 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
const (
varKeyNs = "NS"
varKeyDb = "DB"
varKeySc = "SC"
varKeyTk = "TK"
varKeyUs = "US"
varKeyTb = "TB"
varKeyId = "ID"
varKeyAuth = "auth"
varKeyUser = "user"
varKeyPass = "pass"
)