Extract out some database functionality to another module

This commit is contained in:
Christopher Baines 2019-04-14 10:04:19 +01:00
parent b352b5040a
commit 2e45bb0696
6 changed files with 54 additions and 27 deletions

View file

@ -0,0 +1,40 @@
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
(define-module (guix-data-service database)
#:use-module (squee)
#:export (with-postgresql-connection))
;; TODO This isn't exported for some reason
(define pg-conn-finish
(@@ (squee) pg-conn-finish))
(define (with-postgresql-connection f)
(define paramstring
(or (getenv "GUIX_DATA_SERVICE_DATABASE_PARAMSTRING")
"dbname=guix_data_service user=guix_data_service"))
(let* ((conn (connect-to-postgres-paramstring paramstring)))
(with-throw-handler
#t
(lambda ()
(let ((result (f conn)))
(pg-conn-finish conn)
result))
(lambda (key . args)
(pg-conn-finish conn)))))

View file

@ -21,30 +21,14 @@
#:use-module (web http)
#:use-module (web request)
#:use-module (web uri)
#:use-module (squee)
#:use-module (fibers web server)
#:use-module (guix-data-service database)
#:use-module (guix-data-service web controller)
#:use-module (guix-data-service web util)
#:export (start-guix-data-service-web-server))
;; TODO This isn't exported for some reason
(define pg-conn-finish
(@@ (squee) pg-conn-finish))
(define (with-postgresql-connection paramstring f)
(let* ((conn (connect-to-postgres-paramstring paramstring)))
(with-throw-handler
#t
(lambda ()
(let ((result (f conn)))
(pg-conn-finish conn)
result))
(lambda (key . args)
(pg-conn-finish conn)))))
(define (run-controller controller request body)
(with-postgresql-connection
"dbname=guix_data_service"
(lambda (conn)
((controller request body conn)
(cons (request-method request)