-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
242 lines (205 loc) · 4.96 KB
/
main.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"context"
"database/sql"
"embed"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
_ "github.com/mattn/go-sqlite3"
"github.com/notsobad/w2r/worddb"
)
var (
DbName = ".word.sqlite" // in $HOME directory
Version = "0.1"
//go:embed words.html
WordsHTML embed.FS
)
// struct to store word database
type WordDB struct {
// database connection
Db *sql.DB
Ctx context.Context
}
func isValidWord(s string) bool {
match, _ := regexp.MatchString("^[a-z]+$", s)
return match
}
func getDb() *sql.DB {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
return nil
}
// 拼接文件路径
DbPath := filepath.Join(homeDir, DbName)
db, err := sql.Open("sqlite3", DbPath)
if err != nil {
log.Fatal(err)
}
return db
}
// get multiple words from arguments, split
func filterWords(s string) []string {
// split s with ',', and trim every word, check if it's valid word
words := strings.Split(s, ",")
results := make([]string, 0) // Initialize results as an empty string slice
for _, word := range words {
word = strings.TrimSpace(word)
word = strings.ToLower(word)
if isValidWord(word) {
results = append(results, word)
}
}
return results
}
// init database
func (w *WordDB) Init() {
db := w.Db
sqlStmt := `
CREATE TABLE word (
word TEXT PRIMARY KEY,
zh_trans TEXT,
added_count INTEGER,
lookup_count INTEGER
);
`
_, err := db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return
}
log.Printf("init database")
}
// add word to database
func (w *WordDB) AddWord(word string) {
queries := worddb.New(w.Db)
count, _ := queries.CountWord(w.Ctx, word)
if count == 0 {
_, err := queries.CreateWord(w.Ctx, worddb.CreateWordParams{Word: word, ZhTrans: sql.NullString{}})
if err != nil {
log.Fatal(err)
}
log.Printf("add word '%s'", word)
} else {
err := queries.AddWordCount(w.Ctx, word)
if err != nil {
log.Fatal(err)
}
log.Printf("word '%s' already in database, added_count++", word)
}
}
// show summary
func (w *WordDB) ShowSummary() {
queries := worddb.New(w.Db)
words, _ := queries.Listword(w.Ctx)
fmt.Printf("%15s %10s %12s %-12s\n", "Word", "Added Count", "Lookup Count", "Translation")
for _, word := range words {
lookupCount := word.LookupCount.Int64
if !word.LookupCount.Valid {
lookupCount = 0
}
zhTrans := ""
if word.ZhTrans.Valid {
zhTrans = word.ZhTrans.String
}
fmt.Printf("%15s %10d %12d %-12s\n",
word.Word, word.AddedCount.Int64, lookupCount, zhTrans)
}
}
// delete word from database
func (w *WordDB) DelWord(word string) {
db := w.Db
queries := worddb.New(db)
err := queries.DeleteWord(w.Ctx, word)
if err != nil {
log.Fatal(err)
}
log.Printf("del word '%s'", word)
}
// create a http service to show all words, and generate links to online dictionary
func (w *WordDB) RunWebServer(port int) {
tmpl, err := template.ParseFS(WordsHTML, "words.html")
if err != nil {
// handle error
log.Fatal(err)
}
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
queries := worddb.New(w.Db)
words, _ := queries.Listword(w.Ctx)
err := tmpl.Execute(rw, words)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
// add /word to show single word
http.HandleFunc("/word/", func(rw http.ResponseWriter, r *http.Request) {
word := strings.TrimPrefix(r.URL.Path, "/word/")
word = strings.TrimSuffix(word, "/")
if word == "" {
http.Error(rw, "word not found", http.StatusNotFound)
return
}
// redirect to online dictionary
http.Redirect(rw, r, "https://dictionary.cambridge.org/dictionary/english-chinese-simplified/"+word, http.StatusMovedPermanently)
})
log.Printf("Start web server at http://127.0.0.1:%d", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), nil))
}
func main() {
init := flag.Bool("init", false, "init database")
add := flag.String("a", "", "add new word")
show := flag.Bool("s", false, "show summary")
del := flag.String("d", "", "del word")
daemon := flag.Bool("D", false, "run webserver")
port := flag.Int("p", 8080, "webserver port")
showVersion := flag.Bool("v", false, "show version")
flag.Parse()
// show help when run with no argument
if flag.NFlag() == 0 {
flag.Usage()
return
}
if showVersion != nil && *showVersion {
fmt.Printf("word version: %s\n", Version)
return
}
db := getDb()
defer db.Close()
w := WordDB{Db: db}
w.Ctx = context.Background()
if daemon != nil && *daemon {
// port must be between 0~65535
if *port <= 0 || *port > 65535 {
log.Fatal("port must be between 0~65535")
}
w.RunWebServer(*port)
return
}
if init != nil && *init {
w.Init()
return
}
if add != nil && *add != "" {
words := filterWords(*add)
for _, word := range words {
w.AddWord(word)
}
return
}
if del != nil && *del != "" {
w.DelWord(*del)
return
}
if show != nil && *show {
w.ShowSummary()
return
}
}