Browse Source

Use parameters for db conns, bug fixes

master
Christian Fischer 4 months ago
parent
commit
c7a26cf57e
4 changed files with 32 additions and 28 deletions
  1. +7
    -1
      server/db.rkt
  2. +1
    -1
      server/privileges.rkt
  3. +16
    -17
      server/resource.rkt
  4. +8
    -9
      server/rest.rkt

+ 7
- 1
server/db.rkt View File

@@ -4,13 +4,19 @@
redis)

(provide connect-redis
connect-sql)
connect-sql
redis-conn
mysql-conn)

;; This should be a racket parameter
(define (connect-redis)
(make-redis))

(define redis-conn (make-parameter (connect-redis)))

(define (connect-sql)
(mysql-connect #:user "gn2"
#:password "gn2"
#:database "db_webqtl_s"))

(define mysql-conn (make-parameter (connect-sql)))

+ 1
- 1
server/privileges.rkt View File

@@ -29,7 +29,7 @@
(define (run-action action data params)
;; TODO add check that action matches resource type by comparing
;; keys in params to req-params field in action
(action-fun action) data params)
((action-fun action) data params))

;; An action set is the hash-of-lists-of-functions that define the
;; actions available on a resource type. The hash level are the


+ 16
- 17
server/resource.rkt View File

@@ -53,8 +53,8 @@
(parse 'default_mask)
(parse 'group_masks))))

(define (get-resource dbc id)
(~> (redis-hash-ref dbc "resources" id)
(define (get-resource id)
(~> (redis-hash-ref (redis-conn) "resources" id)
(deserialize-resource)))


@@ -63,9 +63,9 @@
;; the appropriate access mask.

;; TODO take owner mask into account
(define (get-mask-for-user dbc resource user-id)
(define (get-mask-for-user resource user-id)
(let ([group-masks (resource-group-masks resource)]
[groups (get-groups-by-member dbc user-id)]
[groups (get-groups-by-member (redis-conn) user-id)]
[default-mask (resource-default-mask resource)])
(apply mask-join
(dict-ref resource-types (resource-type resource))
@@ -114,11 +114,10 @@
;; Return the action, as defined by a pair of a branch name and action
;; name, for a given resource, as accessible by the given user.
;; Returns #f if the user does not have access.
(define (access-action dbc res user-id action-pair)
(define (access-action res user-id action-pair)
(let ((branch-id (car action-pair))
(action-id (cdr action-pair))
(mask (get-mask-for-user dbc
res
(mask (get-mask-for-user res
user-id))
(action-set (dict-ref resource-types (resource-type res))))
(if (string=? (hash-ref mask branch-id #f)
@@ -161,18 +160,18 @@
(action "view"
(lambda (data
params)
(redis-bytes-get (dict-ref params 'dbc)
(redis-bytes-get (redis-conn)
(hash-ref data 'key)))
'(dbc)))
'()))

(define edit-metadata
(action "edit"
(lambda (data
params)
(redis-bytes-set! (dict-ref params 'dbc)
(redis-bytes-set! (redis-conn)
(hash-ref data 'key)
(dict-ref params 'value)))
'(dbc value)))
'(value)))

(define dataset-file-data
(list (cons "no-access" no-access-action)
@@ -192,8 +191,8 @@



(define (select-publish dbc dataset-id trait-name)
(query-rows dbc
(define (select-publish dataset-id trait-name)
(query-rows (mysql-conn)
"SELECT
PublishXRef.Id, InbredSet.InbredSetCode, Publication.PubMed_ID,
Phenotype.Pre_publication_description, Phenotype.Post_publication_description, Phenotype.Original_description,
@@ -233,8 +232,8 @@
(hasheq)))

;; TODO this should serialize into JSON to be sent by the REST API
(define (select-geno dbc dataset-name trait-name)
(query-rows dbc
(define (select-geno dataset-name trait-name)
(query-rows (mysql-conn)
"SELECT Geno.name, Geno.chr, Geno.mb, Geno.source2, Geno.sequence
FROM Geno, GenoFreeze, GenoXRef
WHERE GenoXRef.GenoFreezeId = GenoFreeze.Id AND
@@ -248,10 +247,10 @@
(action "view"
(lambda (data
params)
(select-geno (dict-ref params 'dbc)
(select-geno (mysql-conn)
(hash-ref data 'dataset)
(hash-ref data 'trait)))
'(dbc)))
'()))

(define dataset-geno-data
(list (cons "no-access" no-access-action)


+ 8
- 9
server/rest.rkt View File

@@ -16,8 +16,6 @@

;;;; Endpoints

(define redis-conn (connect-redis))

;; example endpoint
(define (age req)
(define binds (request-bindings/raw req))
@@ -52,13 +50,15 @@
"provide resource and user id"]
[(list (binding:form _ res-id)
(binding:form _ user-id))
(let* ((res (get-resource redis-conn res-id))
(let* ((res (get-resource res-id))
(mask (get-mask-for-user
redis-conn
(redis-conn)
res
(string->number
(bytes->string/utf-8 user-id)))))
(~> (apply-mask (resource-actions res) mask)
(~> (apply-mask (dict-ref resource-types
(resource-type res))
mask)
(masked-actions)
(jsexpr->bytes)))]))
(response/output
@@ -78,9 +78,8 @@
(binding:form _ user-id)
(binding:form _ branch)
(binding:form _ action))
(let* ((res (get-resource redis-conn res-id)))
(let ((action (access-action redis-conn
res
(let* ((res (get-resource res-id)))
(let ((action (access-action res
(string->number
(bytes->string/utf-8 user-id))
(cons (string->symbol
@@ -90,7 +89,7 @@
(run-action action
(resource-data res)
(hash))
"no access")))])
"no access")))]))
(response/output
(lambda (out)
(displayln message out))))


Loading…
Cancel
Save