-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunserver.go
More file actions
89 lines (70 loc) · 2.08 KB
/
runserver.go
File metadata and controls
89 lines (70 loc) · 2.08 KB
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
83
84
85
86
87
88
89
package main
import (
"bytes"
"flag"
"fmt"
"jumblesolver/dictionary"
"jumblesolver/srvr"
"log"
"net/http"
"os"
"time"
)
func main() {
dictionaryFileName := flag.String("d", "/usr/share/dict/words", "file name full of words")
stopWordsFileName := flag.String("s", "", "file name full of words to ignore in dictionaries")
portString := flag.String("p", "8012", "TCP port on which to listen")
debug := flag.Bool("v", false, "verbose output per request")
dump := flag.Bool("D", false, "dump final dictionary on stdout")
flag.Parse()
stopWords := readStopWords(*stopWordsFileName)
fmt.Printf("Stop words file %s has %d entries\n", *stopWordsFileName, len(stopWords))
buffer, err := os.ReadFile(*dictionaryFileName)
if err != nil {
log.Fatalf("Problem reading dictionary file %q: %v\n", *dictionaryFileName, err)
}
before := time.Now()
dict := dictionary.Build(buffer, stopWords)
dict.Dedupe()
if *dump {
dict.Dump(os.Stdout)
return
}
fmt.Printf("Dictionary file %s has %d keys, construction %v\n", *dictionaryFileName, len(dict), time.Since(before))
srv := &srvr.Srvr{
Router: http.NewServeMux(),
FindWords: dict,
Debug: *debug,
}
srv.Routes()
s := &http.Server{
Addr: ":" + *portString,
Handler: srv.Router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
fmt.Printf("Jumbled-word solver listening on TCP port %s\n", *portString)
log.Fatal(s.ListenAndServe())
}
// readStopWords has the name of a file as its formal argument,
// returns a map of strings to bool, if a string appears,
// don't put it in the dictionary.
func readStopWords(stopWordsFileName string) map[string]bool {
if stopWordsFileName == "" {
return make(map[string]bool)
}
buffer, err := os.ReadFile(stopWordsFileName)
if err != nil {
log.Fatalf("Problem reading stop words file %q: %v\n", stopWordsFileName, err)
}
lines := bytes.Split(buffer, []byte{'\n'})
removes := make(map[string]bool)
for i := range lines {
if len(lines[i]) == 0 {
continue
}
removes[string(lines[i])] = true
}
return removes
}