Shorten variable name

This commit is contained in:
Clayton Coleman 2014-07-29 18:08:22 -04:00
parent 8b511832ff
commit c7d311896e

View File

@ -111,7 +111,7 @@ func (server *APIServer) handleVersionReq(w http.ResponseWriter, req *http.Reque
} }
// HTTP Handler interface // HTTP Handler interface
func (server *APIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (s *APIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer func() { defer func() {
if x := recover(); x != nil { if x := recover(); x != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -129,35 +129,35 @@ func (server *APIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
).Log() ).Log()
// Dispatch via our mux. // Dispatch via our mux.
server.mux.ServeHTTP(w, req) s.mux.ServeHTTP(w, req)
} }
// ServeREST handles requests to all our RESTStorage objects. // ServeREST handles requests to all our RESTStorage objects.
func (server *APIServer) ServeREST(w http.ResponseWriter, req *http.Request) { func (s *APIServer) ServeREST(w http.ResponseWriter, req *http.Request) {
if !strings.HasPrefix(req.URL.Path, server.prefix) { if !strings.HasPrefix(req.URL.Path, s.prefix) {
notFound(w, req) notFound(w, req)
return return
} }
requestParts := strings.Split(req.URL.Path[len(server.prefix):], "/")[1:] requestParts := strings.Split(req.URL.Path[len(s.prefix):], "/")[1:]
if len(requestParts) < 1 { if len(requestParts) < 1 {
notFound(w, req) notFound(w, req)
return return
} }
storage := server.storage[requestParts[0]] storage := s.storage[requestParts[0]]
if storage == nil { if storage == nil {
httplog.LogOf(w).Addf("'%v' has no storage object", requestParts[0]) httplog.LogOf(w).Addf("'%v' has no storage object", requestParts[0])
notFound(w, req) notFound(w, req)
return return
} }
server.handleREST(requestParts, req, w, storage) s.handleREST(requestParts, req, w, storage)
} }
// write writes an API object in wire format. // write writes an API object in wire format.
func (server *APIServer) write(statusCode int, object interface{}, w http.ResponseWriter) { func (s *APIServer) write(statusCode int, object interface{}, w http.ResponseWriter) {
output, err := api.Encode(object) output, err := api.Encode(object)
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@ -166,10 +166,10 @@ func (server *APIServer) write(statusCode int, object interface{}, w http.Respon
} }
// writeRawJSON writes a non-API object in JSON. // writeRawJSON writes a non-API object in JSON.
func (server *APIServer) writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) { func (s *APIServer) writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
output, err := json.Marshal(object) output, err := json.Marshal(object)
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@ -177,20 +177,20 @@ func (server *APIServer) writeRawJSON(statusCode int, object interface{}, w http
w.Write(output) w.Write(output)
} }
func (server *APIServer) error(err error, w http.ResponseWriter) { func (s *APIServer) error(err error, w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Internal Error: %#v", err) fmt.Fprintf(w, "Internal Error: %#v", err)
} }
func (server *APIServer) readBody(req *http.Request) ([]byte, error) { func (s *APIServer) readBody(req *http.Request) ([]byte, error) {
defer req.Body.Close() defer req.Body.Close()
return ioutil.ReadAll(req.Body) return ioutil.ReadAll(req.Body)
} }
// finishReq finishes up a request, waiting until the operation finishes or, after a timeout, creating an // finishReq finishes up a request, waiting until the operation finishes or, after a timeout, creating an
// Operation to receive the result and returning its ID down the writer. // Operation to receive the result and returning its ID down the writer.
func (server *APIServer) finishReq(out <-chan interface{}, sync bool, timeout time.Duration, w http.ResponseWriter) { func (s *APIServer) finishReq(out <-chan interface{}, sync bool, timeout time.Duration, w http.ResponseWriter) {
op := server.ops.NewOperation(out) op := s.ops.NewOperation(out)
if sync { if sync {
op.WaitFor(timeout) op.WaitFor(timeout)
} }
@ -208,9 +208,9 @@ func (server *APIServer) finishReq(out <-chan interface{}, sync bool, timeout ti
status = stat.Code status = stat.Code
} }
} }
server.write(status, obj, w) s.write(status, obj, w)
} else { } else {
server.write(http.StatusAccepted, obj, w) s.write(http.StatusAccepted, obj, w)
} }
} }
@ -225,7 +225,7 @@ func parseTimeout(str string) time.Duration {
return 30 * time.Second return 30 * time.Second
} }
// handleREST is the main dispatcher for the server. It switches on the HTTP method, and then // handleREST is the main dispatcher for a storage object. It switches on the HTTP method, and then
// on path length, according to the following table: // on path length, according to the following table:
// Method Path Action // Method Path Action
// GET /foo list // GET /foo list
@ -234,11 +234,11 @@ func parseTimeout(str string) time.Duration {
// PUT /foo/bar update 'bar' // PUT /foo/bar update 'bar'
// DELETE /foo/bar delete 'bar' // DELETE /foo/bar delete 'bar'
// Returns 404 if the method/pattern doesn't match one of these entries // Returns 404 if the method/pattern doesn't match one of these entries
// The server accepts several query parameters: // The s accepts several query parameters:
// sync=[false|true] Synchronous request (only applies to create, update, delete operations) // sync=[false|true] Synchronous request (only applies to create, update, delete operations)
// timeout=<duration> Timeout for synchronous requests, only applies if sync=true // timeout=<duration> Timeout for synchronous requests, only applies if sync=true
// labels=<label-selector> Used for filtering list operations // labels=<label-selector> Used for filtering list operations
func (server *APIServer) handleREST(parts []string, req *http.Request, w http.ResponseWriter, storage RESTStorage) { func (s *APIServer) handleREST(parts []string, req *http.Request, w http.ResponseWriter, storage RESTStorage) {
sync := req.URL.Query().Get("sync") == "true" sync := req.URL.Query().Get("sync") == "true"
timeout := parseTimeout(req.URL.Query().Get("timeout")) timeout := parseTimeout(req.URL.Query().Get("timeout"))
switch req.Method { switch req.Method {
@ -247,15 +247,15 @@ func (server *APIServer) handleREST(parts []string, req *http.Request, w http.Re
case 1: case 1:
selector, err := labels.ParseSelector(req.URL.Query().Get("labels")) selector, err := labels.ParseSelector(req.URL.Query().Get("labels"))
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
list, err := storage.List(selector) list, err := storage.List(selector)
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
server.write(http.StatusOK, list, w) s.write(http.StatusOK, list, w)
case 2: case 2:
item, err := storage.Get(parts[1]) item, err := storage.Get(parts[1])
if IsNotFound(err) { if IsNotFound(err) {
@ -263,10 +263,10 @@ func (server *APIServer) handleREST(parts []string, req *http.Request, w http.Re
return return
} }
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
server.write(http.StatusOK, item, w) s.write(http.StatusOK, item, w)
default: default:
notFound(w, req) notFound(w, req)
} }
@ -275,9 +275,9 @@ func (server *APIServer) handleREST(parts []string, req *http.Request, w http.Re
notFound(w, req) notFound(w, req)
return return
} }
body, err := server.readBody(req) body, err := s.readBody(req)
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
obj, err := storage.Extract(body) obj, err := storage.Extract(body)
@ -286,7 +286,7 @@ func (server *APIServer) handleREST(parts []string, req *http.Request, w http.Re
return return
} }
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
out, err := storage.Create(obj) out, err := storage.Create(obj)
@ -295,10 +295,10 @@ func (server *APIServer) handleREST(parts []string, req *http.Request, w http.Re
return return
} }
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
server.finishReq(out, sync, timeout, w) s.finishReq(out, sync, timeout, w)
case "DELETE": case "DELETE":
if len(parts) != 2 { if len(parts) != 2 {
notFound(w, req) notFound(w, req)
@ -310,18 +310,18 @@ func (server *APIServer) handleREST(parts []string, req *http.Request, w http.Re
return return
} }
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
server.finishReq(out, sync, timeout, w) s.finishReq(out, sync, timeout, w)
case "PUT": case "PUT":
if len(parts) != 2 { if len(parts) != 2 {
notFound(w, req) notFound(w, req)
return return
} }
body, err := server.readBody(req) body, err := s.readBody(req)
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
obj, err := storage.Extract(body) obj, err := storage.Extract(body)
@ -330,7 +330,7 @@ func (server *APIServer) handleREST(parts []string, req *http.Request, w http.Re
return return
} }
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
out, err := storage.Update(obj) out, err := storage.Update(obj)
@ -339,17 +339,17 @@ func (server *APIServer) handleREST(parts []string, req *http.Request, w http.Re
return return
} }
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }
server.finishReq(out, sync, timeout, w) s.finishReq(out, sync, timeout, w)
default: default:
notFound(w, req) notFound(w, req)
} }
} }
func (server *APIServer) handleOperationRequest(w http.ResponseWriter, req *http.Request) { func (s *APIServer) handleOperationRequest(w http.ResponseWriter, req *http.Request) {
opPrefix := server.operationPrefix() opPrefix := s.operationPrefix()
if !strings.HasPrefix(req.URL.Path, opPrefix) { if !strings.HasPrefix(req.URL.Path, opPrefix) {
notFound(w, req) notFound(w, req)
return return
@ -366,12 +366,12 @@ func (server *APIServer) handleOperationRequest(w http.ResponseWriter, req *http
} }
if len(parts) == 0 { if len(parts) == 0 {
// List outstanding operations. // List outstanding operations.
list := server.ops.List() list := s.ops.List()
server.write(http.StatusOK, list, w) s.write(http.StatusOK, list, w)
return return
} }
op := server.ops.Get(parts[0]) op := s.ops.Get(parts[0])
if op == nil { if op == nil {
notFound(w, req) notFound(w, req)
return return
@ -379,14 +379,14 @@ func (server *APIServer) handleOperationRequest(w http.ResponseWriter, req *http
obj, complete := op.StatusOrResult() obj, complete := op.StatusOrResult()
if complete { if complete {
server.write(http.StatusOK, obj, w) s.write(http.StatusOK, obj, w)
} else { } else {
server.write(http.StatusAccepted, obj, w) s.write(http.StatusAccepted, obj, w)
} }
} }
func (server *APIServer) handleWatch(w http.ResponseWriter, req *http.Request) { func (s *APIServer) handleWatch(w http.ResponseWriter, req *http.Request) {
prefix := server.watchPrefix() prefix := s.watchPrefix()
if !strings.HasPrefix(req.URL.Path, prefix) { if !strings.HasPrefix(req.URL.Path, prefix) {
notFound(w, req) notFound(w, req)
return return
@ -395,7 +395,7 @@ func (server *APIServer) handleWatch(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" || len(parts) < 1 { if req.Method != "GET" || len(parts) < 1 {
notFound(w, req) notFound(w, req)
} }
storage := server.storage[parts[0]] storage := s.storage[parts[0]]
if storage == nil { if storage == nil {
notFound(w, req) notFound(w, req)
} }
@ -408,7 +408,7 @@ func (server *APIServer) handleWatch(w http.ResponseWriter, req *http.Request) {
watching, err = watcher.WatchAll() watching, err = watcher.WatchAll()
} }
if err != nil { if err != nil {
server.error(err, w) s.error(err, w)
return return
} }