-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (116 loc) · 3.55 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
package main
import (
"os"
"os/signal"
"strconv"
"syscall"
"github.com/bazuker/browserbro/pkg/fs"
localFS "github.com/bazuker/browserbro/pkg/fs/local"
"github.com/bazuker/browserbro/pkg/manager"
"github.com/bazuker/browserbro/pkg/plugins"
"github.com/bazuker/browserbro/pkg/plugins/googlesearch"
"github.com/bazuker/browserbro/pkg/plugins/screenshot"
"github.com/gin-gonic/gin"
"github.com/go-rod/rod"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type config struct {
ServerAddress string
BrowserServerID int
BrowserServiceURL string
BrowserMonitorEnabled bool
UserDataDir string
FileStoreBasePath string
}
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
gin.SetMode(gin.ReleaseMode)
cfg := config{
ServerAddress: ":10001",
UserDataDir: "/tmp/rod/user-data/browserBro_userData",
FileStoreBasePath: "/tmp/browserBro_files",
BrowserServerID: 1,
BrowserServiceURL: "ws://localhost:7317",
BrowserMonitorEnabled: true,
}
readConfigFromEnvironment(&cfg)
fileStore, err := localFS.New(localFS.Config{
BasePath: cfg.FileStoreBasePath,
})
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize file store")
return
}
browser := rod.New()
allPlugins := initPlugins(browser, fileStore)
m, err := manager.New(manager.Config{
ServerAddress: cfg.ServerAddress,
FileStore: fileStore,
Browser: browser,
BrowserUserDataDir: cfg.UserDataDir,
BrowserServerID: cfg.BrowserServerID,
BrowserServiceURL: cfg.BrowserServiceURL,
BrowserMonitorEnabled: cfg.BrowserMonitorEnabled,
Plugins: allPlugins,
})
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize manager")
return
}
log.Info().Msg("running API server on " + cfg.ServerAddress)
if err := m.Run(); err != nil {
log.Fatal().Err(err).Msg("error running the API server")
}
quit := make(chan os.Signal, 1)
// Signal notification for Interrupt (Ctrl+C) and SIGTERM (Termination signal from the OS)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
if err := m.Stop(); err != nil {
log.Fatal().Err(err).Msg("error stopping the API server")
}
}
func readConfigFromEnvironment(cfg *config) {
serverAddress := os.Getenv("BROWSERBRO_SERVER_ADDRESS")
if serverAddress != "" {
cfg.ServerAddress = serverAddress
}
browserServerID := os.Getenv("BROWSERBRO_BROWSER_SERVER_ID")
if browserServerID != "" {
i, err := strconv.Atoi(browserServerID)
if err != nil {
log.Fatal().Err(err).
Msg("failed to parse 'BROWSERBRO_BROWSER_SERVER_ID' environment variable")
return
}
cfg.BrowserServerID = i
}
browserServiceURL := os.Getenv("BROWSERBRO_BROWSER_SERVICE_URL")
if browserServiceURL != "" {
cfg.BrowserServiceURL = browserServiceURL
}
browserMonitorEnabled := os.Getenv("BROWSERBRO_BROWSER_MONITOR_ENABLED")
if browserMonitorEnabled != "" {
b, err := strconv.ParseBool(browserMonitorEnabled)
if err != nil {
log.Fatal().Err(err).
Msg("failed to parse 'BROWSERBRO_BROWSER_MONITOR_ENABLED' environment variable")
return
}
cfg.BrowserMonitorEnabled = b
}
userDataDir := os.Getenv("BROWSERBRO_BROWSER_USER_DATA_DIR")
if userDataDir != "" {
cfg.UserDataDir = userDataDir
}
fileStoreBasePath := os.Getenv("BROWSERBRO_FILE_STORE_BASE_PATH")
if fileStoreBasePath != "" {
cfg.FileStoreBasePath = fileStoreBasePath
}
}
func initPlugins(browser *rod.Browser, fileStore fs.FileStore) []plugins.Plugin {
return []plugins.Plugin{
googlesearch.New(browser),
screenshot.New(browser, fileStore),
}
}