Browse Source

refactor(server): move utilities into pkg

like camlistore lets move these utilities into a `pkg` prefix.
Brandon Philips 12 years ago
parent
commit
69922340f6
3 changed files with 9 additions and 18 deletions
  1. 3 15
      pkg/http/http.go
  2. 3 1
      server/peer_server_handlers.go
  3. 3 2
      server/server.go

+ 3 - 15
server/util.go → pkg/http/http.go

@@ -1,4 +1,4 @@
-package server
+package http
 
 import (
 	"encoding/json"
@@ -6,12 +6,11 @@ import (
 	"io"
 	"net/http"
 	"net/url"
-	"strings"
 
 	"github.com/coreos/etcd/log"
 )
 
-func decodeJsonRequest(req *http.Request, data interface{}) error {
+func DecodeJsonRequest(req *http.Request, data interface{}) error {
 	decoder := json.NewDecoder(req.Body)
 	if err := decoder.Decode(&data); err != nil && err != io.EOF {
 		log.Warnf("Malformed json request: %v", err)
@@ -20,7 +19,7 @@ func decodeJsonRequest(req *http.Request, data interface{}) error {
 	return nil
 }
 
-func redirect(hostname string, w http.ResponseWriter, req *http.Request) {
+func Redirect(hostname string, w http.ResponseWriter, req *http.Request) {
 	originalURL := req.URL
 	redirectURL, _ := url.Parse(hostname)
 
@@ -32,14 +31,3 @@ func redirect(hostname string, w http.ResponseWriter, req *http.Request) {
 	log.Debugf("Redirect to %s", redirectURL.String())
 	http.Redirect(w, req, redirectURL.String(), http.StatusTemporaryRedirect)
 }
-
-// trimsplit slices s into all substrings separated by sep and returns a
-// slice of the substrings between the separator with all leading and trailing
-// white space removed, as defined by Unicode.
-func trimsplit(s, sep string) []string {
-	trimmed := strings.Split(s, sep)
-	for i := range trimmed {
-		trimmed[i] = strings.TrimSpace(trimmed[i])
-	}
-	return trimmed
-}

+ 3 - 1
server/peer_server_handlers.go

@@ -7,8 +7,10 @@ import (
 	"time"
 
 	etcdErr "github.com/coreos/etcd/error"
+	uhttp "github.com/coreos/etcd/pkg/http"
 	"github.com/coreos/etcd/log"
 	"github.com/coreos/etcd/store"
+
 	"github.com/coreos/etcd/third_party/github.com/coreos/raft"
 	"github.com/coreos/etcd/third_party/github.com/gorilla/mux"
 )
@@ -149,7 +151,7 @@ func (ps *PeerServer) EtcdURLHttpHandler(w http.ResponseWriter, req *http.Reques
 func (ps *PeerServer) JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
 	command := &JoinCommand{}
 
-	err := decodeJsonRequest(req, command)
+	err := uhttp.DecodeJsonRequest(req, command)
 	if err != nil {
 		w.WriteHeader(http.StatusInternalServerError)
 		return

+ 3 - 2
server/server.go

@@ -15,6 +15,7 @@ import (
 	"github.com/coreos/etcd/log"
 	"github.com/coreos/etcd/metrics"
 	"github.com/coreos/etcd/mod"
+	uhttp "github.com/coreos/etcd/pkg/http"
 	"github.com/coreos/etcd/server/v1"
 	"github.com/coreos/etcd/server/v2"
 	"github.com/coreos/etcd/store"
@@ -244,7 +245,7 @@ func (s *Server) Dispatch(c raft.Command, w http.ResponseWriter, req *http.Reque
 		default:
 			url, _ = ps.registry.ClientURL(leader)
 		}
-		redirect(url, w, req)
+		uhttp.Redirect(url, w, req)
 
 		return nil
 	}
@@ -295,7 +296,7 @@ func (s *Server) GetLeaderStatsHandler(w http.ResponseWriter, req *http.Request)
 		return etcdErr.NewError(300, "", s.Store().Index())
 	}
 	hostname, _ := s.registry.ClientURL(leader)
-	redirect(hostname, w, req)
+	uhttp.Redirect(hostname, w, req)
 	return nil
 }