@@ -8,18 +8,30 @@ import (
88 "log"
99 "os"
1010 "os/exec"
11+ "sync"
1112 "syscall"
1213)
1314
1415// Env contains the structure of the .env file
1516type Env struct {
1617 PrimaryServer EnvServer `json:"primary_server"`
1718 AlternativeServers []EnvServer `json:"alternative_servers"`
18- LoginUsers []EnvUser `json:"login_users"`
1919 MockMode bool `json:"mock_mode"`
20+ MockUsers []EnvUser `json:"mock_users"`
2021}
2122
2223func (e * Env ) validate () error {
24+ if e .MockMode {
25+ if len (e .MockUsers ) == 0 {
26+ fmt .Println ("\" mock_users\" is empty in env.json, most scrapers require at least one user to login." )
27+ fmt .Println ("For documentation about mocking see https://github.com/script-development/rtcv_scraper_client" )
28+ if e .MockUsers == nil {
29+ e .MockUsers = []EnvUser {}
30+ }
31+ }
32+ return nil
33+ }
34+
2335 err := e .PrimaryServer .validate ()
2436 if err != nil {
2537 return fmt .Errorf ("primary_server.%s" , err .Error ())
@@ -73,6 +85,11 @@ type EnvUser struct {
7385
7486func main () {
7587 envFilename := "env.json"
88+ alternativeFileName := os .Getenv ("RTCV_SCRAPER_CLIENT_ENV_FILE" )
89+ if alternativeFileName != "" {
90+ envFilename = alternativeFileName
91+ }
92+
7693 envEnvName := "RTCV_SCRAPER_CLIENT_ENV"
7794 envFile , err := ioutil .ReadFile (envFilename )
7895 if err != nil {
@@ -97,16 +114,13 @@ func main() {
97114 }
98115
99116 api := NewAPI ()
100- useAddress := startWebserver (env , api )
101117
102118 credentials := []SetCredentialsArg {env .PrimaryServer .toCredArg (true )}
103119 for _ , server := range env .AlternativeServers {
104120 credentials = append (credentials , server .toCredArg (false ))
105121 }
106122
107- // Turn on mock mode by default
108- api .SetMockMode ()
109-
123+ loginUsers := []EnvUser {}
110124 if ! env .MockMode {
111125 err = api .SetCredentials (credentials )
112126 if err != nil {
@@ -115,13 +129,17 @@ func main() {
115129
116130 fmt .Println ("credentials set" )
117131 fmt .Println ("testing connections.." )
118- testServerConnections (api )
132+ loginUsers = testServerConnections (api , credentials [ 0 ]. APIKeyID )
119133 fmt .Println ("connected to RTCV" )
120134 } else {
135+ api .SetMockMode ()
136+ loginUsers = env .MockUsers
121137 fmt .Println ("In mock mode" )
122- fmt .Println ("You can turn this off in by setting mock_mode to false in your env.json " )
138+ fmt .Println ("You can turn this off in `env.json` by setting ` mock_mode` to false" )
123139 }
124140
141+ useAddress := startWebserver (env , api , loginUsers )
142+
125143 fmt .Println ("running scraper.." )
126144
127145 if len (os .Args ) <= 1 {
@@ -147,32 +165,53 @@ func main() {
147165 }
148166}
149167
150- func testServerConnections (api * API ) {
168+ func testServerConnections (api * API , apiKeyID string ) []EnvUser {
169+ var wg sync.WaitGroup
170+
151171 for _ , conn := range api .connections {
152- err := conn .Get ("/api/v1/health" , nil )
153- if err != nil {
154- log .Fatal (err )
155- }
172+ wg .Add (1 )
173+ go func (conn serverConn ) {
174+ err := conn .Get ("/api/v1/health" , nil )
175+ if err != nil {
176+ log .Fatal (err )
177+ }
156178
157- apiKeyInfo := struct {
158- Roles []struct {
159- Role uint64 `json:"role"`
160- } `json:"roles"`
161- }{}
162- err = conn .Get ("/api/v1/auth/keyinfo" , & apiKeyInfo )
163- if err != nil {
164- log .Fatal (err )
165- }
179+ apiKeyInfo := struct {
180+ Roles []struct {
181+ Role uint64 `json:"role"`
182+ } `json:"roles"`
183+ }{}
184+ err = conn .Get ("/api/v1/auth/keyinfo" , & apiKeyInfo )
185+ if err != nil {
186+ log .Fatal (err )
187+ }
166188
167- hasScraperRole := false
168- for _ , role := range apiKeyInfo .Roles {
169- if role .Role == 1 {
170- hasScraperRole = true
171- break
189+ hasScraperRole := false
190+ for _ , role := range apiKeyInfo .Roles {
191+ if role .Role == 1 {
192+ hasScraperRole = true
193+ break
194+ }
172195 }
173- }
174- if ! hasScraperRole {
175- log .Fatal ("provided key does not have scraper role (nr 1)" )
176- }
196+ if ! hasScraperRole {
197+ log .Fatal ("provided key does not have scraper role (nr 1)" )
198+ }
199+ wg .Done ()
200+ }(conn )
177201 }
202+
203+ scraperUsers := struct {
204+ Users []EnvUser `json:"users"`
205+ }{}
206+ err := api .connections [0 ].Get ("/api/v1/scraperUsers/" + apiKeyID , & scraperUsers )
207+
208+ // Wait for the connections above to complete checking before we do this error check but do the request already so we don't have to wait for that
209+ // If one of the connections has an error they will throw
210+ wg .Wait ()
211+
212+ if err != nil {
213+ log .Fatal (err )
214+ }
215+
216+ return scraperUsers .Users
178217}
0 commit comments