refactor(server): move utilities into pkg

like camlistore lets move these utilities into a `pkg` prefix.
This commit is contained in:
Brandon Philips
2014-01-27 15:00:10 -08:00
parent 0e50d9787a
commit 69922340f6
3 changed files with 9 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
package server package http
import ( import (
"encoding/json" "encoding/json"
@@ -6,12 +6,11 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"github.com/coreos/etcd/log" "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) decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&data); err != nil && err != io.EOF { if err := decoder.Decode(&data); err != nil && err != io.EOF {
log.Warnf("Malformed json request: %v", err) log.Warnf("Malformed json request: %v", err)
@@ -20,7 +19,7 @@ func decodeJsonRequest(req *http.Request, data interface{}) error {
return nil 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 originalURL := req.URL
redirectURL, _ := url.Parse(hostname) 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()) log.Debugf("Redirect to %s", redirectURL.String())
http.Redirect(w, req, redirectURL.String(), http.StatusTemporaryRedirect) 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
}

View File

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

View File

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