From 3ddc81270e9431ee7cb6e494669e417a62f72f69 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 13 Oct 2012 18:45:01 -0400 Subject: [PATCH] Break up the /service handler into individual HTTP handlers. --- frontend/main.js | 3 +- server/server.go | 123 ++++++++++++++++++++++++----------------------- 2 files changed, 63 insertions(+), 63 deletions(-) diff --git a/frontend/main.js b/frontend/main.js index 69acada..61be7f6 100644 --- a/frontend/main.js +++ b/frontend/main.js @@ -39,9 +39,8 @@ armadillo.App = function() { * @return {jqXHR} The jQuery XHR object. */ armadillo.App.prototype.sendRequest = function(action, data, callback) { - data.action = action; return $.ajax({ - url: 'service', + url: 'service/' + action, type: 'POST', data: data, success: callback, diff --git a/server/server.go b/server/server.go index 643df86..af5c236 100644 --- a/server/server.go +++ b/server/server.go @@ -35,7 +35,7 @@ func init() { kFrontEndFiles = path.Join(path.Dir(path.Dir(thisFile)), "frontend") } -func indexHandler(response http.ResponseWriter, request *http.Request) { +func indexHandler(rw http.ResponseWriter, request *http.Request) { fd, err := os.Open(path.Join(kFrontEndFiles, "index.html")) if err != nil { fmt.Print("Error opening file ", err.Error(), "\n") @@ -43,73 +43,70 @@ func indexHandler(response http.ResponseWriter, request *http.Request) { } defer fd.Close() - response.Header().Set("Content-Type", "text/html") - io.Copy(response, fd) + rw.Header().Set("Content-Type", "text/html") + io.Copy(rw, fd) } -func serviceHandler(response http.ResponseWriter, request *http.Request) { - if request.Method != "POST" { - io.WriteString(response, "Error: Not a POST request") - return +func listService(rw http.ResponseWriter, req *http.Request) { + files, err := ListPath(req.FormValue("path")) + if err != nil { + httpError(rw, err.Error(), http.StatusNotFound) + } else { + okResponse(rw, files) } +} - switch request.FormValue("action") { - case "list": - files, err := ListPath(request.FormValue("path")) - if err != nil { - httpError(response, err.Error(), http.StatusNotFound) - } else { - okResponse(response, files) - } - case "remove": - err := RemovePath(request.FormValue("path")) - if err != nil { - httpError(response, err.Error(), http.StatusNotFound) - } else { - data := map[string]int{ - "error": 0, - } - okResponse(response, data) +func removeService(rw http.ResponseWriter, req *http.Request) { + err := RemovePath(req.FormValue("path")) + if err != nil { + httpError(rw, err.Error(), http.StatusNotFound) + } else { + data := map[string]int{ + "error": 0, } - case "move": - source := request.FormValue("source") - target := request.FormValue("target") - err := MovePath(source, target) - if err != nil { - httpError(response, err.Error(), http.StatusNotFound) - } else { - data := map[string]interface{}{ - "path": target, - "error": 0, - } - okResponse(response, data) + okResponse(rw, data) + } +} + +func moveService(rw http.ResponseWriter, req *http.Request) { + source := req.FormValue("source") + target := req.FormValue("target") + err := MovePath(source, target) + if err != nil { + httpError(rw, err.Error(), http.StatusNotFound) + } else { + data := map[string]interface{}{ + "path": target, + "error": 0, } - case "mkdir": - path := request.FormValue("path") - err := MakeDir(path) - if err != nil { - httpError(response, err.Error(), http.StatusUnauthorized) - } else { - data := map[string]interface{}{ - "path": path, - "error": 0, - } - okResponse(response, data) + okResponse(rw, data) + } +} + +func mkdirService(rw http.ResponseWriter, req *http.Request) { + path := req.FormValue("path") + err := MakeDir(path) + if err != nil { + httpError(rw, err.Error(), http.StatusUnauthorized) + } else { + data := map[string]interface{}{ + "path": path, + "error": 0, } - case "tv_rename": - newPath, err := RenameTVEpisode(request.FormValue("path")) - if err != nil { - httpError(response, err.Error(), http.StatusBadRequest) - } else { - data := map[string]interface{}{ - "path": *newPath, - "error": 0, - } - okResponse(response, data) + okResponse(rw, data) + } +} + +func tvRenameService(rw http.ResponseWriter, req *http.Request) { + newPath, err := RenameTVEpisode(req.FormValue("path")) + if err != nil { + httpError(rw, err.Error(), http.StatusBadRequest) + } else { + data := map[string]interface{}{ + "path": *newPath, + "error": 0, } - default: - httpError(response, fmt.Sprintf("Invalid action: '%s'", request.FormValue("action")), - http.StatusBadRequest) + okResponse(rw, data) } } @@ -150,7 +147,11 @@ func RunBackEnd(c *config.Configuration) { mux := http.NewServeMux() mux.HandleFunc("/", indexHandler) mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles)))) - mux.HandleFunc("/service", serviceHandler) + mux.HandleFunc("/service/list", listService) + mux.HandleFunc("/service/move", moveService) + mux.HandleFunc("/service/remove", removeService) + mux.HandleFunc("/service/mkdir", mkdirService) + mux.HandleFunc("/service/tv_rename", tvRenameService) mux.HandleFunc("/download", downloadHandler) error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux) -- 2.22.5