Skip to content

Commit

Permalink
WEBSITE: Hack to allow websites to work on Android with libgambit
Browse files Browse the repository at this point in the history
  • Loading branch information
mgorges committed Dec 3, 2023
1 parent fc6364a commit 74f3a10
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions modules/website/website.scm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
string-replace-substring
exception->string log:exception-handler
log-error log-system
make-safe-thread
make-safe-thread app:android?
u8vector->base64-string
system-directory system-pathseparator string-contains
))
Expand Down Expand Up @@ -127,10 +127,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(define (website:server db port address)
(let ((accept-port (open-tcp-server (list server-address: address port-number: port reuse-address: #t))))
(let loop () (let ((connection (read accept-port)))
(if (not (eof-object? connection))
(begin (thread-start! (make-safe-thread (lambda ()
(if (not (eof-object? connection))
(begin
;; With Gambit 4.9.2 (libgambit) Android doesn't like a thread in a thread, so it won't run otherwise
(if app:android?
(website:serve db connection)
(thread-start! (make-safe-thread (lambda ()
(current-exception-handler log:exception-handler)
(website:serve db connection) )))
)
(loop)))))))

(define (website:trim-string s)
Expand Down Expand Up @@ -267,9 +272,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(define website:db (make-table))
(define (website-getdb) website:db)

(define (website-serve db port . bind) (thread-start! (make-safe-thread (lambda ()
(current-exception-handler log:exception-handler)
(website:server (if db db website:db) port (if (or (null? bind) (not (eq? (car bind) 'localonly))) "*" "127.0.0.1"))))))
(define (website-serve db port . bind)
(thread-start! (make-safe-thread (lambda ()
(current-exception-handler log:exception-handler)
(website:server (if db db website:db) port (if (or (null? bind) (not (eq? (car bind) 'localonly))) "*" "127.0.0.1")))))
;; It is unclear why this is needed, but without it Gambit 4.9.2 (libgambit) will not run the thread on Android at all
(if app:android? (thread-sleep! 0.01))
)

(define (website-addhook db document proc)
(table-set! (if db db website:db)
Expand Down

0 comments on commit 74f3a10

Please sign in to comment.