From 8f00bede78e2585426a16e8160a40970c4c89e3c Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 30 Jan 2015 23:49:13 -0500 Subject: Make /package a JSON only route. * guix-web/controller.scm (extract-package-name): New procedure. (controller): Only render JSON for '/package' routes. --- guix-web/controller.scm | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/guix-web/controller.scm b/guix-web/controller.scm index b3104fe..fa7dfc3 100644 --- a/guix-web/controller.scm +++ b/guix-web/controller.scm @@ -17,7 +17,9 @@ (define-module (guix-web controller) #:use-module (ice-9 match) + #:use-module (ice-9 regex) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) #:use-module (web request) #:use-module (gnu packages) #:use-module (guix-web render) @@ -26,6 +28,12 @@ #:use-module (guix-web package) #:export (controller)) +(define extract-package-name + (let ((regexp (make-regexp "^(.*)\\.json$"))) + (lambda (path-part) + (and=> (regexp-exec regexp path-part) + (cut match:substring <> 1))))) + (define controller (match-lambda ((GET) @@ -34,15 +42,8 @@ (render-html (all-packages))) ((GET "packages.json") (render-json (all-packages-json))) - ((GET "packages" name) - (match (string-split name #\.) - ((name ext) - (render-json - (if (string=? ext "json") - (view-package-json name) - '()))) - ((name) - (render-html (view-package name))))) + ((GET "package" (= extract-package-name (? string? name))) + (render-json (view-package-json name))) ((POST "packages" name "install") (let ((package (car (find-packages-by-name name)))) (if (package-install package) -- cgit v1.2.3