Proof-of-concept of a simple DSL for specifying servlets.
This commit is contained in:
		
							parent
							
								
									03c094214b
								
							
						
					
					
						commit
						186fe2d0ec
					
				
					 3 changed files with 33 additions and 5 deletions
				
			
		|  | @ -33,7 +33,10 @@ | ||||||
| 
 | 
 | ||||||
|   (import scheme |   (import scheme | ||||||
| 	  spiffy | 	  spiffy | ||||||
| 	  (chicken format)) | 	  (chicken format) | ||||||
|  | 	  bar-db | ||||||
|  | 	  json | ||||||
|  | 	  (chicken port)) | ||||||
| 
 | 
 | ||||||
|   (define-syntax try-match-lambda1 |   (define-syntax try-match-lambda1 | ||||||
|     (syntax-rules () |     (syntax-rules () | ||||||
|  | @ -73,8 +76,16 @@ | ||||||
|       ((_ (name . args) . body) |       ((_ (name . args) . body) | ||||||
|        (define name (try-match-lambda args . body))))) |        (define name (try-match-lambda args . body))))) | ||||||
| 
 | 
 | ||||||
|  |   (define (alist->json-string alst) | ||||||
|  |     (with-output-to-string | ||||||
|  |       (lambda () | ||||||
|  | 	(json-write | ||||||
|  | 	 (list->vector alst))))) | ||||||
|  | 
 | ||||||
|   (define-try-match (account-barcode-info "barcode" (barcode string->number)) |   (define-try-match (account-barcode-info "barcode" (barcode string->number)) | ||||||
|     (send-response #:body (format "API call [barcode] ~A" barcode))) |     (send-response | ||||||
|  |      #:body (alist->json-string | ||||||
|  | 	     `((amount . ,(bd-barcode-lookup barcode)))))) | ||||||
| 
 | 
 | ||||||
|   (define api-servlets |   (define api-servlets | ||||||
|     (list account-barcode-info)) |     (list account-barcode-info)) | ||||||
|  |  | ||||||
|  | @ -29,20 +29,36 @@ | ||||||
|     bar-db |     bar-db | ||||||
|     ( |     ( | ||||||
|      bar-db-init! |      bar-db-init! | ||||||
|  |      bd-barcode-lookup | ||||||
|      ) |      ) | ||||||
| 
 | 
 | ||||||
|   (import scheme |   (import scheme | ||||||
| 	  (chicken base) | 	  (chicken base) | ||||||
| 	  postgresql) | 	  postgresql | ||||||
|  | 	  sql-null) | ||||||
| 
 | 
 | ||||||
|   (define bar-db-conn (make-parameter #f)) |   (define bd-conn (make-parameter #f)) | ||||||
| 
 | 
 | ||||||
|   (define (bar-db-init! name host user pass) |   (define (bar-db-init! name host user pass) | ||||||
|     (bar-db-conn |     (bd-conn | ||||||
|      (connect |      (connect | ||||||
|       `((dbname . ,name) |       `((dbname . ,name) | ||||||
| 	(host . ,host) | 	(host . ,host) | ||||||
| 	(user . ,user) | 	(user . ,user) | ||||||
| 	(password . ,pass))))) | 	(password . ,pass))))) | ||||||
| 
 | 
 | ||||||
|  |   (define (sql-null->symbol v) | ||||||
|  |     (if (sql-null? v) | ||||||
|  | 	'null | ||||||
|  | 	v)) | ||||||
|  | 
 | ||||||
|  |   (define (bd-barcode-lookup bc) | ||||||
|  |     (sql-null->symbol | ||||||
|  |      (car | ||||||
|  |       (row-values | ||||||
|  |        (query | ||||||
|  | 	(bd-conn) | ||||||
|  | 	"select sum(amount*case side when 'credit' then -1 else 1 end) amt from transaction_splits where account=$1" | ||||||
|  | 	bc))))) | ||||||
|  | 
 | ||||||
|   ) |   ) | ||||||
|  |  | ||||||
|  | @ -56,6 +56,7 @@ trap chicken_cleanup INT QUIT | ||||||
| chicken_install openssl | chicken_install openssl | ||||||
| chicken_install spiffy | chicken_install spiffy | ||||||
| chicken_install postgresql | chicken_install postgresql | ||||||
|  | chicken_install json | ||||||
| 
 | 
 | ||||||
| # Normal termination cleanup | # Normal termination cleanup | ||||||
| chicken_cleanup | chicken_cleanup | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue