Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
53d881b0
Commit
53d881b0
authored
Nov 04, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
7298d3cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
0 deletions
+19
-0
application/libs/config/consul.py
application/libs/config/consul.py
+19
-0
No files found.
application/libs/config/consul.py
View file @
53d881b0
...
...
@@ -18,12 +18,31 @@ import yaml
class
ConsulConfig
:
def
__init__
(
self
,
host
=
'localhost'
,
port
=
8500
,
token
=
None
,
dc
=
'dc1'
):
"""
Initialize a ConsulConfig instance.
:param host: The host of the Consul server. Default is 'localhost'.
:param port: The port of the Consul server. Default is 8500.
:param token: The Consul token. Default is None.
:param dc: The datacenter to use. Default is 'dc1'.
"""
self
.
token
=
token
self
.
dc
=
dc
# Create a Consul client instance
self
.
client
=
consul
.
Consul
(
host
=
host
,
port
=
port
,
token
=
token
,
dc
=
dc
)
def
get
(
self
,
key
:
str
)
->
Any
:
"""
Retrieve the value of a key from the Consul KV store.
:param key: The key to retrieve.
:return: The value of the key.
:raises KeyError: If the key is not found in Consul.
"""
# Retrieve the key-value pair from Consul
_
,
data
=
self
.
client
.
kv
.
get
(
key
=
key
,
token
=
self
.
token
,
dc
=
self
.
dc
)
# If the key-value pair is not found, raise an exception
if
data
is
None
:
raise
KeyError
(
f
'Key
{
key
}
not found in Consul.'
)
# Load the value from the key-value pair as YAML
return
yaml
.
load
(
data
[
'Value'
],
Loader
=
yaml
.
FullLoader
)
Write
Preview
Markdown
is supported
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