Add addr and port options

Evgeny PisemskySat Feb 22 10:45:55+0300 2025

0cae302

Add addr and port options

README.md

1414
Execute one of the following commands in the root of repository:
1515
1616
```
17-
guile -L modules --no-auto-compile scripts/pastebin <DATA_DIR>
18-
GUILE_LOAD_PATH=modules GUILE_AUTO_COMPILE=0 scripts/pastebin <DATA_DIR>
17+
guile -L modules --no-auto-compile scripts/pastebin [OPTIONS] <DATA_DIR>
18+
GUILE_LOAD_PATH=modules GUILE_AUTO_COMPILE=0 scripts/pastebin [OPTIONS] <DATA_DIR>
1919
```
2020
21-
Then navigate your web browser at `http://localhost:8080`.
21+
Then navigate your web browser at `http://localhost:8080`. By default,
22+
pastebin listens on all IPv4 addresses (`0.0.0.0`) and runs on port
23+
`8080`. These can be changed by passing the command line options
24+
`--addr` and `--port`, respectively.
2225
2326
`<DATA_DIR>` is a writable filesystem location you want to use to
24-
store pastes.  If it doesn't exist, it will be created.
27+
store pastes. If it doesn't exist, it will be created.
2528
26-
It was tested on Guile 2.2.7 and 3.0.7.  Many thanks to the Guile
27-
development team for such great software.
29+
It was tested on Guile 3.0.9. Many thanks to the Guile development
30+
team for such great software.
2831
2932
## API
3033

modules/pastebin/main.scm

66
(define-module (pastebin main))
77
88
(use-modules (pastebin httpserver)
9+
             (ice-9 getopt-long)
910
             (web server))
1011
1112
(export run-pastebin)
1213
1314
(define (run-pastebin args)
14-
  (let ((data-dir (cadr args)))
15+
  (let* ((option-spec '((addr (value #t))
16+
                        (port (value #t))))
17+
         (options (getopt-long args option-spec))
18+
         (data-dir (car (option-ref options '() '())))
19+
         (addr-str (option-ref options 'addr "0.0.0.0"))
20+
         (port-str (option-ref options 'port "8080")))
1521
    (if (not (file-exists? data-dir))
1622
        (mkdir data-dir))
17-
    (run-server (make-pastebin-handler data-dir) 'http '(#:addr 0))))
23+
    (run-server (make-pastebin-handler data-dir) 'http
24+
                (list #:addr (inet-pton AF_INET addr-str)
25+
                      #:port (string->number port-str)))))