http.scm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;;; http.scm -- HTTP API
  2. ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
  3. ;;;
  4. ;;; This file is part of Cuirass.
  5. ;;;
  6. ;;; Cuirass is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; Cuirass is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Cuirass. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (cuirass http)
  19. #:use-module (cuirass database)
  20. #:use-module (cuirass utils)
  21. #:use-module (ice-9 hash-table)
  22. #:use-module (ice-9 match)
  23. #:use-module (json)
  24. #:use-module (web request)
  25. #:use-module (web response)
  26. #:use-module (web server)
  27. #:use-module (web uri)
  28. #:export (spec->json-string
  29. run-cuirass-server))
  30. ;;;
  31. ;;; JSON format.
  32. ;;;
  33. (define (object->json-scm obj)
  34. "Prepare OBJ for JSON usage."
  35. (cond ((string? obj) obj)
  36. ((number? obj) obj)
  37. ((boolean? obj) obj)
  38. ((null? obj) obj)
  39. ((symbol? obj) (symbol->string obj))
  40. ((keyword? obj) (object->json-scm (keyword->symbol obj)))
  41. ((alist? obj) (alist->hash-table (map object->json-scm obj)))
  42. ((pair? obj) (cons (object->json-scm (car obj))
  43. (object->json-scm (cdr obj))))
  44. (else (object->string obj))))
  45. (define* (spec->json-string spec #:key pretty)
  46. "Return SPEC as a JSON object."
  47. (scm->json-string (object->json-scm spec) #:pretty pretty))
  48. ;;;
  49. ;;; Web server.
  50. ;;;
  51. (define (request-path-components request)
  52. (split-and-decode-uri-path (uri-path (request-uri request))))
  53. (define (url-handler request body db)
  54. (define* (respond response #:key body (db db))
  55. (values response body db))
  56. (match (request-path-components request)
  57. (((or "jobsets" "specifications") . rest)
  58. (respond '((content-type . (application/json)))
  59. #:body (spec->json-string (car (db-get-specifications db)))))
  60. (_
  61. (respond (build-response #:code 404)
  62. #:body (string-append "Resource not found: "
  63. (uri->string (request-uri request)))))))
  64. (define* (run-cuirass-server db #:key (port 8080))
  65. (format (current-error-port) "listening on port ~A~%" port)
  66. (run-server url-handler
  67. 'http ;server implementation
  68. `(#:port ,port) ;implementation parameters
  69. db)) ;state