Skip to content
This repository has been archived by the owner on May 14, 2019. It is now read-only.

Commit

Permalink
minor cleanup of parameter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Tschuy committed Sep 9, 2016
1 parent c5e497e commit 0b2a0ba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
10 changes: 5 additions & 5 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func fail(err string) {
os.Exit(2) // default go flag error code
}

func getValidUrlOrExit(givenUrl string) string {
func mustHostOnlyURL(givenUrl string) string {
u, err := url.Parse(givenUrl)

if err != nil {
fail(fmt.Sprintf("Invalid url given: %v", err))
}

if len(u.Path) > 1 || (len(u.Path) == 1 && u.Path != "/") {
if len(u.Path) != 0 && u.Path != "/" {
fail(fmt.Sprintf("Expected url without path (%v)", u.Path))
}

Expand Down Expand Up @@ -64,10 +64,10 @@ func init() {

func main() {
log.SetFlags(0)
viper.Set("etcd", getValidUrlOrExit(viper.GetString("etcd")))
viper.Set("host", getValidUrlOrExit(viper.GetString("host")))
etcdHost := mustHostOnlyURL(viper.GetString("etcd"))
discHost := mustHostOnlyURL(viper.GetString("host"))

handling.Setup()
handling.Setup(etcdHost, discHost)

err := http.ListenAndServe(viper.GetString("addr"), nil)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions handlers/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"github.com/coreos/discovery.etcd.io/handlers/httperror"
"github.com/coreos/etcd/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/viper"
"golang.org/x/net/context"
)

var newCounter *prometheus.CounterVec
var cfg *client.Config
var discHost string

func init() {
newCounter = prometheus.NewCounterVec(
Expand All @@ -44,16 +44,17 @@ func generateCluster() string {
return hex.EncodeToString(b)
}

func Setup() {
func Setup(etcdHost, disc string) {
cfg = &client.Config{
Endpoints: []string{viper.GetString("etcd")},
Endpoints: []string{etcdHost},
Transport: client.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}

u, _ := url.Parse(viper.GetString("etcd"))
u, _ := url.Parse(etcdHost)
currentLeader.Set(u.Host)
discHost = disc
}

func setupToken(size int) (string, error) {
Expand Down Expand Up @@ -113,6 +114,6 @@ func NewTokenHandler(w http.ResponseWriter, r *http.Request) {

log.Println("New cluster created", token)

fmt.Fprintf(w, "%s/%s", bytes.TrimRight([]byte(viper.GetString("host")), "/"), token)
fmt.Fprintf(w, "%s/%s", bytes.TrimRight([]byte(discHost), "/"), token)
newCounter.WithLabelValues("200", r.Method).Add(1)
}
4 changes: 2 additions & 2 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/gorilla/mux"
)

func Setup() {
handlers.Setup()
func Setup(etcdHost, discHost string) {
handlers.Setup(etcdHost, discHost)
r := mux.NewRouter()

r.HandleFunc("/", handlers.HomeHandler)
Expand Down

0 comments on commit 0b2a0ba

Please sign in to comment.