Skip to content

Commit

Permalink
Merge pull request #3 from Piszmog/develop
Browse files Browse the repository at this point in the history
Merge develop
  • Loading branch information
Piszmog committed Sep 5, 2018
2 parents b3f68e2 + 4f37227 commit f272fd5
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 46 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ Below is an example usage of the library to retrieve a file from the Config Serv
* For local config client, ensure `CONFIG_SERVER_URLS` is set
* `CONFIG_SERVER_URLS` is a comma separated list of all the base URLs
* For running in Cloud Foundry, ensure a Config Server is bounded to the application. `VCAP_SERVICES` will be provided as an environment variables with the credentials to access the Config Server
* If not running in Cloud Foundry but still want to connect to a Config Server via OAuth2, manually set the `VCAP_SERVICES` -- example value in `client/oauth2_test.go`
* For connecting to a Config Server via OAuth2 and not deployed to Cloud Foundry, an OAuth2 Client can be created with `CreateOAuth2Client(credentials []cfservices.Credentials)`

```go
package main

import (
"fmt"
"github.com/Piszmog/cfservices"
"github.com/Piszmog/cloudconfigclient/client"
)

Expand All @@ -47,6 +48,17 @@ func main() {
configClient, err := client.CreateLocalClient()
// or to create a Client for a Spring Config Server in Cloud Foundry
configClient, err := client.CreateCloudClient()
// or to create a Client for a Spring Config Server with OAuth2
credentials := cfservices.Credentials{
Uri: "config server uri",
ClientSecret: "client secret",
ClientId: "client id",
AccessTokenUri: "access token uri",
}
creds := make([]cfservices.Credentials, 1)
creds[0] = credentials
configClient, err := client.CreateOAuth2Client(creds)

if err != nil {
panic(err)
}
Expand Down Expand Up @@ -146,11 +158,11 @@ Spring's Config Server allows two ways to retrieve files from a backing reposito
| URL Path |
| :---: |
|`/<appName>/<profiles>/<directory>/<file>?useDefaultLabel=true`|
|`/<appName>/<profiles>/<branch>/<directory>/<file>?useDefaultLabel=true`|
|`/<appName>/<profiles>/<branch>/<directory>/<file>`|

* When retrieving a file from the Config Server's default branch, the file must not exist at the root of the repository.

The functions available to retrieve resource files are, `GetFile(directory string, file string, interfaceType interface{}) error` and
The functions available to retrieve resource files are, `GetFile(directory string, file string, interfaceType interface{})` and
`GetFileFromBranch(branch string, directory string, file string, interfaceType interface{})`.

* The `interfaceType` is the object to deserialize the file to
4 changes: 2 additions & 2 deletions client/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package client

import (
"github.com/Piszmog/cfservices"
"github.com/Piszmog/cloudconfigclient/net"
"github.com/Piszmog/httpclient"
"github.com/pkg/errors"
"os"
"strings"
Expand All @@ -24,7 +24,7 @@ func CreateLocalClient() (*ConfigClient, error) {
configClients := make([]CloudClient, len(serviceCredentials.Credentials))
for index, cred := range serviceCredentials.Credentials {
configUri := cred.Uri
client := net.CreateDefaultHttpClient()
client := httpclient.CreateDefaultHttpClient()
configClients[index] = Client{configUri: configUri, httpClient: client}
}
return &ConfigClient{Clients: configClients}, nil
Expand Down
9 changes: 7 additions & 2 deletions client/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ func CreateCloudClientForService(name string) (*ConfigClient, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to create cloud client")
}
configClients := make([]CloudClient, len(serviceCredentials.Credentials))
for index, cred := range serviceCredentials.Credentials {
return CreateOAuth2Client(serviceCredentials.Credentials)
}

// CreateOAuth2Client creates a ConfigClient to access Config Servers from an array of credentials.
func CreateOAuth2Client(credentials []cfservices.Credentials) (*ConfigClient, error) {
configClients := make([]CloudClient, len(credentials))
for index, cred := range credentials {
configUri := cred.Uri
client, err := net.CreateOAuth2Client(&cred)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/Piszmog/cloudconfigclient

require (
github.com/Piszmog/cfservices v1.3.1
github.com/Piszmog/httpclient v1.0.1
github.com/golang/protobuf v1.1.0 // indirect
github.com/pkg/errors v0.8.0
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/Piszmog/cfservices v1.3.1 h1:xiX4kjYJUxJYUBDgUJ+OgdqUsLX48yFq8YMZs/gdGyE=
github.com/Piszmog/cfservices v1.3.1/go.mod h1:uxLe5gKNnODE9Bt1aJd5WjaeTg8/Qr6Dibd1wFgfAwk=
github.com/Piszmog/httpclient v1.0.1 h1:LBgUxwb2zrwv/c1qx0D4lSwmz28ZNf7k91EsNbclJk0=
github.com/Piszmog/httpclient v1.0.1/go.mod h1:FcqjAm4HhYGYfQaFY8kZ2Y+SGiO51s4H3K6GEtAHOn4=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24 h1:mEsFm194MmS9vCwxFy+zwu0EU7ZkxxMD1iH++vmGdUY=
Expand Down
32 changes: 0 additions & 32 deletions net/net.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package net

import (
"net"
"net/http"
"strings"
"time"
)

// CreateUrl creates a full URL from the specified base URL and the array of URI variables.
Expand All @@ -22,32 +19,3 @@ func CreateUrl(baseUrl string, uriVariables ...string) string {
func JoinProfiles(profiles []string) string {
return strings.Join(profiles, ",")
}

// CreateDefaultHttpClient creates a default http.Client.
//
// Timeout set to 5 seconds, keep alive set to 30 seconds, TLS handshake timeout set to 5 seconds, and idleConnection set to
// 90 seconds.
func CreateDefaultHttpClient() *http.Client {
return CreateHttpClient(5*time.Second, 30*time.Second, 5*time.Second, 90*time.Second)
}

// CreateHttpClient creates a http.Client from the specified timeouts and keep alive.
//
// The client also has the maximum number of idle connections set to 100 and number of connections per host as 100.
func CreateHttpClient(timeout time.Duration, keepAlive time.Duration, tlsHandshakeTimeout time.Duration, idleConnection time.Duration) *http.Client {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: keepAlive,
DualStack: true,
}).DialContext,
TLSHandshakeTimeout: tlsHandshakeTimeout,
IdleConnTimeout: idleConnection,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
}
return &http.Client{
Transport: transport,
Timeout: timeout,
}
}
7 changes: 0 additions & 7 deletions net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,3 @@ func TestJoinProfiles(t *testing.T) {
t.Errorf("profiles were not append")
}
}

func TestCreateDefaultHttpClient(t *testing.T) {
client := CreateDefaultHttpClient()
if client == nil {
t.Errorf("failed to create configuration client")
}
}

0 comments on commit f272fd5

Please sign in to comment.