Skip to content

Commit 5dbfb83

Browse files
committed
Use new method of obtaining login users
1 parent c708108 commit 5dbfb83

File tree

4 files changed

+98
-45
lines changed

4 files changed

+98
-45
lines changed

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,24 @@ Create a `env.json` file with the following content **(this file can also be obt
6363
},
6464
"alternative_servers": [
6565
// If you want to send CVs to multiple servers you can add additional servers here
66+
// {"server_location": "http://localhost:4000","api_key_id": "aa","api_key": "bbb"}
6667
],
67-
"login_users": [
68-
{"username": "scraping-site-username", "password": "scraping-site-password"}
68+
}
69+
```
70+
71+
#### `2.1.` Mocking RTCV
72+
73+
When developing a scraper you might want to mock RT-CV so you don't have to relay on another service.
74+
75+
You can mock RT-CV by changing your env to this:
76+
77+
```js
78+
{
79+
"mock_mode": true,
80+
"mock_users": [
81+
// Here you place the login users for the site you are scraping that will be used as mock data
82+
{"username": "scraping-site-username", "password": "scraping-site-password"},
6983
],
70-
// For production, set mock_mode to false
71-
// "mock_mode": false
7284
}
7385
```
7486
@@ -99,7 +111,7 @@ Sends a cv to rtcv and remembers the reference number
99111
100112
### `$SCRAPER_ADDRESS/users`
101113
102-
Returns the login users from the `env.json`
114+
Returns the login scraper users from RT-CV
103115
104116
- Body: None
105117
- Resp: **true** / **false** the login users from env.json
@@ -127,6 +139,14 @@ Check if a reference number is in the cache
127139
- Body: The reference number
128140
- Resp: **true** / **false**
129141
142+
## `env.json` is in another dir or has another name?
143+
144+
You can change the credentials file location using this shell variable
145+
146+
```sh
147+
export RTCV_SCRAPER_CLIENT_ENV_FILE=some/other/env/file.json
148+
```
149+
130150
## `env.json` as env variable?
131151
132152
If you are using containers you might want to use the contents of your `env.json` as env variable instaid of binding it to the container as volume.

examples/deno/env.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@
44
"api_key_id": "aa",
55
"api_key": "bbb"
66
},
7-
"alternative_servers": [],
8-
"login_users": [
9-
{
10-
"username": "scraping-site-username",
11-
"password": "scraping-site-password"
12-
}
13-
]
7+
"alternative_servers": []
148
}

main.go

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1516
type 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

2223
func (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

7486
func 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
}

webserver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/valyala/fasthttp"
1212
)
1313

14-
func startWebserver(env Env, api *API) string {
15-
loginUsers, err := json.Marshal(env.LoginUsers)
14+
func startWebserver(env Env, api *API, loginUsers []EnvUser) string {
15+
loginUsersJSON, err := json.Marshal(loginUsers)
1616
if err != nil {
1717
log.Fatal(err)
1818
}
@@ -78,7 +78,7 @@ func startWebserver(env Env, api *API) string {
7878

7979
ctx.Response.AppendBodyString("true")
8080
case "/users":
81-
ctx.Response.AppendBody(loginUsers)
81+
ctx.Response.AppendBody(loginUsersJSON)
8282
case "/set_cached_reference", "/set_short_cached_reference":
8383
refNr := string(ctx.Request.Body())
8484
if refNr == "" {

0 commit comments

Comments
 (0)