Skip to content

Commit 4c0806d

Browse files
committed
Add basic example
1 parent 20daf21 commit 4c0806d

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ configurations
5353
1. Call `LocalEnv()`. Set the environment variable `CONFIG_SERVER_URLS`. It is a comma separated list of all the
5454
base URLs
5555
2. Call `Local(baseUrls ...string)`. Provide the array of base URLs of Config Servers.
56+
* If the config server is protected with basic auth, call `Basic` with the username and password.
5657
* For running in Cloud Foundry, ensure a Config Server is bounded to the application. `VCAP_SERVICES` will be provided
5758
as an environment variables with the credentials to access the Config Server
5859
* For connecting to a Config Server via OAuth2 and not deployed to Cloud Foundry, an OAuth2 Client can be created
@@ -80,6 +81,8 @@ func main() {
8081
configClient, err := cloudconfigclient.New(cloudconfigclient.LocalEnv(&http.Client{}))
8182
// Or
8283
configClient, err = cloudconfigclient.New(cloudconfigclient.Local(&http.Client{}, "http://localhost:8888"))
84+
// or to create a Client for a Spring Config Server using Basic Authentication
85+
configClient, err = cloudconfigclient.New(cloudconfigclient.Basic(&http.Client{}, "username", "password" "http://localhost:8888"))
8386
// or to create a Client for a Spring Config Server in Cloud Foundry
8487
configClient, err = cloudconfigclient.New(cloudconfigclient.DefaultCFService())
8588
// or to create a Client for a Spring Config Server with OAuth2

examples/basic/main.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/Piszmog/cloudconfigclient/v2"
10+
)
11+
12+
func main() {
13+
// ensure you have the Config Server running locally...
14+
client, err := cloudconfigclient.New(cloudconfigclient.Basic(&http.Client{}, "username", "password", "http://localhost:8888"))
15+
if err != nil {
16+
log.Fatalln(err)
17+
}
18+
19+
// load a config file
20+
configuration, err := client.GetConfiguration("test-app", "local")
21+
if err != nil {
22+
log.Fatalln(err)
23+
}
24+
// we can either unmarshal into a struct
25+
var configData configStruct
26+
if err = configuration.Unmarshal(&configData); err != nil {
27+
log.Fatalln("failed to find cloud property file")
28+
}
29+
30+
// or manually access the values
31+
localProp, err := configuration.GetPropertySource("application-local.yml")
32+
if err != nil {
33+
log.Fatalln("failed to find local property file")
34+
}
35+
fmt.Printf("local property file: %+v\n", localProp)
36+
// handle all config properties
37+
configuration.HandlePropertySources(func(propertySource cloudconfigclient.PropertySource) {
38+
if strings.HasSuffix(propertySource.Name, "test-app.properties") {
39+
// TODO save off values
40+
}
41+
})
42+
43+
// load a specific file (e.g. json/txt)
44+
var f map[string]string
45+
// if 'fooDir' has been added to 'searchPaths' in SCS v3.x, then pass "" (blank) for directory
46+
if err = client.GetFile("fooDir", "bar.json", &f); err != nil {
47+
log.Fatalln(err)
48+
}
49+
fmt.Printf("file from default branch: %+v\n", f)
50+
51+
// load a specific file (e.g. json/txt)
52+
var b map[string]string
53+
// if 'fooDir' has been added to 'searchPaths' in SCS v3.x, then pass "" (blank) for directory
54+
if err = client.GetFileFromBranch("develop", "fooDir", "bar.json", &b); err != nil {
55+
log.Fatalln(err)
56+
}
57+
fmt.Printf("file from specific branch: %+v\n", b)
58+
}
59+
60+
type configStruct struct {
61+
Field1 string `json:"field1"`
62+
}

examples/cloudfoundry/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
func main() {
1212
// ensure you have set the environment variable 'VCAP_SERVICES' that contains the information to connect to the
1313
// Config Server
14-
1514
client, err := cloudconfigclient.New(cloudconfigclient.DefaultCFService())
1615
if err != nil {
1716
log.Fatalln(err)

examples/local/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
func main() {
1313
// ensure you have the Config Server running locally...
14-
1514
client, err := cloudconfigclient.New(cloudconfigclient.Local(&http.Client{}, "http://localhost:8888"))
1615
if err != nil {
1716
log.Fatalln(err)

examples/oauth/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
func main() {
1212
// ensure you have the Config Server running locally (or in the cloud) and configured for OAuth2
13-
1413
client, err := cloudconfigclient.New(cloudconfigclient.OAuth2("config server uri", "client id",
1514
"client secret", "access token uri"))
1615
if err != nil {

0 commit comments

Comments
 (0)