Created
February 9, 2020 07:12
-
-
Save shakdwipeea/e8fbfe042e409d30cf84ddea77443a0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; rough draft of nng rest server in chez scheme | |
;; some things dont work | |
(library (server) | |
(export ) | |
(import (chezscheme) | |
(prelude) | |
(ffi)) | |
(define l (load-shared-object "/usr/local/lib/libnng.so")) | |
(define-syntax define-nng-procedure | |
(lambda (stx) | |
(define kebab-case->underscore-case | |
(lambda (str) | |
(list->string (map (lambda (ch) | |
(if (char=? ch #\-) #\_ ch)) | |
(string->list str))))) | |
(syntax-case stx () | |
((_ proc-name (args ...)) | |
(with-syntax ((c-proc-name (datum->syntax #'proc-name | |
(kebab-case->underscore-case | |
(symbol->string (syntax->datum #'proc-name))))) | |
((vargs ...) (map (lambda (arg) (datum->syntax #'proc-name (gensym))) | |
#'(args ...)))) | |
#'(define proc-name | |
(lambda (vargs ...) | |
(let* ((f (foreign-procedure c-proc-name (args ...) int)) | |
(val (f vargs ...))) | |
(unless (fx=? val 0) | |
(error "failed to call nng proc. returned val: " c-proc-name | |
(number->string val))))))))))) | |
(define-foreign-struct nng-url | |
((rawurl . uptr) | |
(scheme . uptr) | |
(userinfo . uptr) | |
(host . uptr) | |
(hostname . uptr) | |
(port . uptr) | |
(path . uptr) | |
(query . uptr) | |
(fragment . uptr) | |
(requri . uptr))) | |
(define-ftype nng-url* (* nng-url)) | |
(define-ftype nng-http-server uptr) | |
(define-ftype nng-http-handler uptr) | |
(define-ftype nng-aio uptr) | |
(define-ftype handler-callback (function ((* nng-aio)) void)) | |
(define-nng-procedure nng-url-parse ((* uptr) string)) | |
(define-nng-procedure nng-http-server-hold ((* nng-http-server) (* nng-url))) | |
(define-nng-procedure nng-http-handler-alloc ((* nng-http-handler) uptr uptr)) | |
(define-nng-procedure nng-http-handler-set-method ((* nng-http-handler) string)) | |
(define-syntax callback | |
(syntax-rules () | |
((_ f (<args> ...) <ret>) | |
(let ([code (foreign-callable f (<args> ...) <ret>)]) | |
(lock-object code) | |
(foreign-callable-entry-point code))))) | |
(define *url* "http://127.0.0.1:8006/api/contact-us") | |
(define acquire-server-instance | |
(lambda (endpoint) | |
(let ((url (make-foreign-object nng-url)) | |
(server (make-foreign-object nng-http-server))) | |
(nng-url-parse url endpoint) | |
(nng-http-server-hold server (make-ftype-pointer nng-url | |
(pointer-ref-value url))) | |
server))) | |
(define url (let ((u (make-foreign-object nng-url))) | |
(nng-url-parse u *url*) | |
(make-ftype-pointer nng-url (pointer-ref-value u)))) | |
(define server (let ((s (make-foreign-object nng-http-server))) | |
(nng-http-server-hold s url) | |
s)) | |
(define contact-handler | |
(lambda (nng-aio-ptr) | |
(with-output-to-file "new.txt" | |
(displayln ":asasasoioiio") | |
(lambda () (displayln "hello world=asss")) | |
'replace) | |
(void))) | |
(define handler | |
(let ((handler (make-foreign-object nng-http-handler))) | |
(nng-http-handler-alloc handler | |
(nng-url-path url) | |
(callback contact-handler ((* nng-aio)) void)) | |
handler)) | |
;; fails with NNG_EBUSY | |
(nng-http-handler-set-method handler "POST")) | |
#!eof | |
(library-directories '("./thunderchez" )) | |
(load "server.scm") | |
(import (server)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment