Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
feudal
feudalClient
Commits
613c5183
Commit
613c5183
authored
Mar 16, 2018
by
Lukas Burgey
Browse files
Adapt client to fetch some config parameters from the backend
parent
134d183b
Changes
1
Hide whitespace changes
Inline
Side-by-side
client.go
View file @
613c5183
...
...
@@ -51,11 +51,20 @@ type (
OK
bool
`json:"ok"`
}
rabbitMQConfig
struct
{
ExchangeName
string
`json:"exchange"`
Vhost
string
`json:"vhost"`
}
FetchedConfig
struct
{
RabbitMQConfig
rabbitMQConfig
`json:"rabbitmq_config"`
Services
[]
service
`json:"services"`
}
config
struct
{
Host
string
`json:"host"`
Username
string
`json:"username"`
ExchangeName
string
`json:"exchangeName"`
Token
string
`json:"token"`
Password
string
`json:"password"`
Services
[]
service
`json:"services"`
FetchIntervalString
string
`json:"fetch_interval"`
// string parsed by time.ParseDuration
ReconnectTimeoutString
string
`json:"reconnect_timeout"`
// string parsed by time.ParseDuration
...
...
@@ -63,6 +72,9 @@ type (
DoneTasks
chan
task
FetchInterval
time
.
Duration
ReconnectTimeout
time
.
Duration
ExchangeName
string
// fetched from backend
Vhost
string
// fetched from backend
}
consumer
struct
{
...
...
@@ -126,13 +138,13 @@ serviceLoop:
return
}
func
(
c
*
config
)
fetchConfig
uredServices
()
(
services
[]
service
,
err
error
)
{
func
(
c
*
config
)
fetchConfig
()
(
err
error
)
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"https://"
+
c
.
Host
+
"/backend/clientapi/config"
,
nil
)
if
err
!=
nil
{
return
}
req
.
Header
.
Add
(
"Authorization"
,
fmt
.
Sprintf
(
"Token %s"
,
c
.
Token
)
)
req
.
SetBasicAuth
(
c
.
Username
,
c
.
Password
)
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
return
...
...
@@ -143,14 +155,32 @@ func (c *config) fetchConfiguredServices() (services []service, err error) {
if
err
!=
nil
{
return
}
err
=
json
.
Unmarshal
(
body
,
&
services
)
var
fetchedConfig
FetchedConfig
err
=
json
.
Unmarshal
(
body
,
&
fetchedConfig
)
if
err
!=
nil
{
log
.
Fatalf
(
"Unable to parse: %s %s"
,
err
,
body
)
return
}
c
.
ExchangeName
=
fetchedConfig
.
RabbitMQConfig
.
ExchangeName
c
.
Vhost
=
fetchedConfig
.
RabbitMQConfig
.
Vhost
permittedServices
:=
fetchedConfig
.
Services
log
.
Printf
(
"[Conf] Permitted services: %s"
,
permittedServices
)
if
len
(
c
.
Services
)
==
0
{
log
.
Printf
(
"[Conf] Config specifies no services. Using all permitted services"
)
c
.
Services
=
permittedServices
}
else
{
// if the config specifies services we check if they are all permitted
c
.
Services
=
filterPermitted
(
permittedServices
,
c
.
Services
)
}
log
.
Printf
(
"[Conf] Services: %s"
,
c
.
Services
)
return
}
func
read
Config
(
configFile
string
)
(
c
config
,
err
error
)
{
func
get
Config
(
configFile
string
)
(
c
config
,
err
error
)
{
log
.
Printf
(
"[Conf] Reading config file %s"
,
configFile
)
...
...
@@ -165,21 +195,10 @@ func readConfig(configFile string) (c config, err error) {
return
}
var
permittedServices
[]
service
if
permittedServices
,
err
=
c
.
fetchConfiguredServices
();
err
!=
nil
{
log
.
Printf
(
"[Conf] Error fetching configured services: %s"
,
err
)
}
log
.
Printf
(
"[Conf] Permitted services: %s"
,
permittedServices
)
if
len
(
c
.
Services
)
==
0
{
log
.
Printf
(
"[Conf] Config specifies no services. Using all permitted services"
)
c
.
Services
=
permittedServices
}
else
{
// if the config specifies services we check if they are all permitted
c
.
Services
=
filterPermitted
(
permittedServices
,
c
.
Services
)
if
err
=
c
.
fetchConfig
();
err
!=
nil
{
log
.
Printf
(
"[Conf] Error fetching remote config: %s"
,
err
)
return
}
log
.
Printf
(
"[Conf] Services: %s"
,
c
.
Services
)
if
c
.
FetchInterval
,
err
=
time
.
ParseDuration
(
c
.
FetchIntervalString
);
err
!=
nil
{
log
.
Printf
(
"[Conf] Error parsing fetch interval: %s"
,
err
)
return
...
...
@@ -314,6 +333,10 @@ func (c *consumer) reconnect() {
func
(
c
*
consumer
)
startConsuming
()
(
err
error
)
{
deliveries
,
err
:=
c
.
connect
()
if
err
!=
nil
{
log
.
Fatalf
(
"[Conn] Error connecting: %s"
,
err
)
return
}
// start the first consumer
go
c
.
consumer
(
deliveries
)
...
...
@@ -328,7 +351,7 @@ func (c *config) consumer() (cons *consumer) {
keys
=
append
(
keys
,
fmt
.
Sprintf
(
"service.%s"
,
serviceName
))
}
cons
=
&
consumer
{
uri
:
fmt
.
Sprintf
(
"amqps://%s:%s@%s"
,
c
.
Username
,
c
.
Token
,
c
.
Host
),
uri
:
fmt
.
Sprintf
(
"amqps://%s:%s@%s"
,
c
.
Username
,
c
.
Password
,
c
.
Host
),
exchange
:
c
.
ExchangeName
,
exchangeType
:
"topic"
,
bindingKeys
:
keys
,
...
...
@@ -392,7 +415,7 @@ func (c *config) ackTask(ti task) (ok bool, err error) {
return
}
req
.
Header
.
Add
(
"Authorization"
,
fmt
.
Sprintf
(
"Token %s"
,
c
.
Token
)
)
req
.
SetBasicAuth
(
c
.
Username
,
c
.
Password
)
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
return
...
...
@@ -449,7 +472,7 @@ func (c *config) fetchTasks() (err error) {
}
req
.
URL
.
RawQuery
=
q
.
Encode
()
req
.
Header
.
Add
(
"Authorization"
,
fmt
.
Sprintf
(
"Token %s"
,
c
.
Token
)
)
req
.
SetBasicAuth
(
c
.
Username
,
c
.
Password
)
// execute the request
resp
,
err
:=
client
.
Do
(
req
)
...
...
@@ -506,7 +529,7 @@ func main() {
kingpin
.
MustParse
(
app
.
Parse
(
os
.
Args
[
1
:
]))
// read the config file
c
,
err
:=
read
Config
(
*
configFile
)
c
,
err
:=
get
Config
(
*
configFile
)
if
err
!=
nil
{
log
.
Fatalf
(
"[Err] No valid config. Exiting"
)
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment