Break up the /service handler into individual HTTP handlers.
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 13 Oct 2012 22:45:01 +0000 (18:45 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 13 Oct 2012 22:45:01 +0000 (18:45 -0400)
frontend/main.js
server/server.go

index 69acada0d5c71aa44f3cad847dc8687f797f106a..61be7f63c3abf5ed243b18a2311298f576b001cf 100644 (file)
@@ -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,
index 643df86b4e67464d0e51f8a22778056c8eefc2bd..af5c2360b67acfe81258ddd72450dca704d4b008 100644 (file)
@@ -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)