start-gauche-server.scm 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. (import
  2. (scheme base)
  3. (scheme read)
  4. (scheme write)
  5. (scheme process-context)
  6. (scheme load)
  7. (scheme cxr)
  8. (only (gauche base) make-keyword)
  9. (only (gauche base) string-split)
  10. (makiki))
  11. (include "./web-server.scm")
  12. (define default-port 43334)
  13. (define (main args)
  14. (define current-port
  15. (if (null? (cdr args))
  16. default-port
  17. (string->number (cadr args))))
  18. (start-http-server (make-keyword "port") current-port
  19. (make-keyword "error-log") #t))
  20. ;; front-end handler
  21. (define-http-handler (GET) "/"
  22. (lambda (req app)
  23. (respond/ok req (list 'sxml index-page))))
  24. (define-http-handler "/index.html"
  25. (lambda (req app)
  26. (respond/ok req (list 'sxml index-page))))
  27. ;; scripting handler
  28. (define-http-handler "/draw.js"
  29. (lambda (req app)
  30. (respond/ok req '(file "canvas.js"))))
  31. ;; new id handler
  32. (define-http-handler (POST) "/api/NewId"
  33. (lambda (req app)
  34. (respond/ok req (list 'json (string->number (api-new-id!))))))
  35. ;; push buffer handler
  36. (define-http-handler (POST) "/api/PushBuffer"
  37. (lambda (req app)
  38. (let ((body (utf8->string (read-request-body req))))
  39. (respond/ok req (list 'json (api-push-buffer! body))))))
  40. ;; push bulk buffer handler
  41. (define-http-handler (POST) "/api/PushBulkBuffer"
  42. (lambda (req app)
  43. (let ((body (utf8->string (read-request-body req))))
  44. (respond/ok req (list 'json (api-push-bulk-buffer! body))))))
  45. ;; pull buffer handler
  46. (define-http-handler (POST) "/api/PullBuffer"
  47. (lambda (req app)
  48. (let ((body (utf8->string (read-request-body req))))
  49. (respond/ok req (list 'json (api-pull-buffer! body))))))
  50. ;; clear buffer handler
  51. (define-http-handler (POST) "/api/ClearBuffer"
  52. (lambda (req app)
  53. (api-clear-buffer!)))
  54. (main (command-line))