-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
81 lines (68 loc) · 1.97 KB
/
app.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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"sync"
"github.com/hhankj2u/translators/pkg/cache"
"github.com/hhankj2u/translators/pkg/dicts"
"github.com/hhankj2u/translators/pkg/settings"
"github.com/atotto/clipboard"
)
type App struct {
dbConnections map[string]*sql.DB
}
func NewApp() *App {
// Initialize database or any other required setup
dbConnections := make(map[string]*sql.DB)
dbConnections[settings.WEBSTER] = cache.InitDB(settings.WEBSTER)
dbConnections[settings.CAMBRIDGE] = cache.InitDB(settings.CAMBRIDGE)
dbConnections[settings.SOHA] = cache.InitDB(settings.SOHA)
return &App{dbConnections: dbConnections}
}
func (a *App) Startup(ctx context.Context) {
log.Println("App is starting up!")
}
// SearchDictionary performs the dictionary search and returns the result HTML from all dictionaries
func (a *App) SearchDictionary(term string) (map[string]string, error) {
dictionaries := map[string]dicts.Dictionary{
settings.WEBSTER: dicts.WebsterDictionary{},
settings.CAMBRIDGE: dicts.CambridgeDictionary{},
settings.SOHA: dicts.SohaDictionary{},
}
results := make(map[string]string)
var mu sync.Mutex
var wg sync.WaitGroup
errChan := make(chan error, len(dictionaries))
for name, dictionary := range dictionaries {
wg.Add(1)
go func(name string, dictionary dicts.Dictionary) {
defer wg.Done()
con := a.dbConnections[name]
_, soup, err := dictionary.Search(con, term, false)
if err != nil {
errChan <- fmt.Errorf("error searching %s dictionary: %w", name, err)
return
}
html, err := soup.Html()
if err != nil {
errChan <- fmt.Errorf("error getting HTML from %s dictionary: %w", name, err)
return
}
mu.Lock()
results[name] = html
mu.Unlock()
}(name, dictionary)
}
wg.Wait()
close(errChan)
if len(errChan) > 0 {
return nil, <-errChan
}
return results, nil
}
// ReadClipboard reads the clipboard content
func (a *App) ReadClipboard() (string, error) {
return clipboard.ReadAll()
}