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")
}
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)
}
}
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)