-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rkt
82 lines (70 loc) · 2.71 KB
/
main.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#lang racket/base
(require racket/list)
(require racket/string)
(require racket/cmdline)
(require "corpus.rkt")
(require "gopher.rkt")
(define gopher21-version "0.6.0")
(define server-root (make-parameter "./"))
(define server-port (make-parameter 70))
(define search-tree-path (make-parameter #f))
(define disk-cache-path (make-parameter #f))
(define print-version (make-parameter #f))
(define arg-list
(command-line
#:once-any
[("-c") path
"Create a corpus in the file <path>, from files in <arg-list> directories"
(search-tree-path path)]
[("-s") root port
"Launch the server on <port>, serving from <root>.\n <arg-list> is a list of the form <corpus name>:<path to corpus file>:<corpus root dir path>"
(server-root root) (server-port (string->number port))]
[("-v")
"Print version number"
(print-version #t)]
#:once-each
[("-d") cache-path
"Create a disk cache in the file <cache-path>"
(disk-cache-path cache-path)]
[("-n") hname
"Set the server's hostname to <hname>"
(set-gopher-hostname hname)]
#:args arg-list
arg-list))
(define (parse-corpus-paths args)
(for ([arg args])
(let ([corpus-w-paths (string-split arg ":")])
(if (= (length corpus-w-paths) 3)
(begin
(printf "Loading corpus ~a~n" arg)
(gopher-add-corpus (first corpus-w-paths) (second corpus-w-paths) (third corpus-w-paths)))
(printf "Invalid corpus arg ~a, format is: <corpus name>:<path to corpus file>:<corpus root dir path>~n" arg)))))
(cond
[(print-version)
(printf "Version ~a~n" gopher21-version)]
[(search-tree-path)
(cond
[(empty? arg-list)
(error "Missing root directory of corpus.")]
[(> 1 (length arg-list))
(error "Too many arguments.")]
[else
(define corpus-root (car arg-list))
(printf "Creating corpus from contents of ~a~n" corpus-root)
(define st (build-corpus corpus-root))
(printf "Finished building corpus~n")
(if (disk-cache-path)
(begin
(printf "Creating disk cache in ~a~n" (disk-cache-path))
(let ([dst (create-disk-cached-tree st (disk-cache-path))])
(printf "Saving corpus to disk as ~a~n" (search-tree-path))
(save-corpus dst (search-tree-path))))
(begin
(printf "Saving corpus to disk as ~a~n" (search-tree-path))
(save-corpus st (search-tree-path))))])]
[else
(unless (empty? arg-list)
(parse-corpus-paths arg-list))
(printf "Starting server from ~a on port ~a...~n" (server-root) (server-port))
(define stop (gopher-serve-map (server-root) (server-port)))
(do-not-return)])