Break up the /service handler into individual HTTP handlers.
[armadillo.git] / server / server.go
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010-2012, Robert Sesek <http://www.bluestatic.org>
4 //
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
8 //
9
10 package server
11
12 import (
13 "encoding/json"
14 "fmt"
15 "github.com/rsesek/armadillo/config"
16 "io"
17 "net/http"
18 "os"
19 "path"
20 "runtime"
21 "strings"
22 )
23
24 var (
25 kFrontEndFiles string
26 gConfig *config.Configuration
27 )
28
29 func init() {
30 _, thisFile, _, ok := runtime.Caller(0)
31 if !ok {
32 panic("unable to get file information from runtime.Caller so the frontend files cannot be found")
33 }
34 // thisFile = /armadillo/server/server.go, so compute /armadillo/frontend/
35 kFrontEndFiles = path.Join(path.Dir(path.Dir(thisFile)), "frontend")
36 }
37
38 func indexHandler(rw http.ResponseWriter, request *http.Request) {
39 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"))
40 if err != nil {
41 fmt.Print("Error opening file ", err.Error(), "\n")
42 return
43 }
44 defer fd.Close()
45
46 rw.Header().Set("Content-Type", "text/html")
47 io.Copy(rw, fd)
48 }
49
50 func listService(rw http.ResponseWriter, req *http.Request) {
51 files, err := ListPath(req.FormValue("path"))
52 if err != nil {
53 httpError(rw, err.Error(), http.StatusNotFound)
54 } else {
55 okResponse(rw, files)
56 }
57 }
58
59 func removeService(rw http.ResponseWriter, req *http.Request) {
60 err := RemovePath(req.FormValue("path"))
61 if err != nil {
62 httpError(rw, err.Error(), http.StatusNotFound)
63 } else {
64 data := map[string]int{
65 "error": 0,
66 }
67 okResponse(rw, data)
68 }
69 }
70
71 func moveService(rw http.ResponseWriter, req *http.Request) {
72 source := req.FormValue("source")
73 target := req.FormValue("target")
74 err := MovePath(source, target)
75 if err != nil {
76 httpError(rw, err.Error(), http.StatusNotFound)
77 } else {
78 data := map[string]interface{}{
79 "path": target,
80 "error": 0,
81 }
82 okResponse(rw, data)
83 }
84 }
85
86 func mkdirService(rw http.ResponseWriter, req *http.Request) {
87 path := req.FormValue("path")
88 err := MakeDir(path)
89 if err != nil {
90 httpError(rw, err.Error(), http.StatusUnauthorized)
91 } else {
92 data := map[string]interface{}{
93 "path": path,
94 "error": 0,
95 }
96 okResponse(rw, data)
97 }
98 }
99
100 func tvRenameService(rw http.ResponseWriter, req *http.Request) {
101 newPath, err := RenameTVEpisode(req.FormValue("path"))
102 if err != nil {
103 httpError(rw, err.Error(), http.StatusBadRequest)
104 } else {
105 data := map[string]interface{}{
106 "path": *newPath,
107 "error": 0,
108 }
109 okResponse(rw, data)
110 }
111 }
112
113 func downloadHandler(response http.ResponseWriter, request *http.Request) {
114 valid, fullPath := IsValidPath(request.FormValue("path"))
115 if valid {
116 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
117 if info.IsDir() {
118 http.Error(response, "Path is a directory", http.StatusBadRequest)
119 } else {
120 http.ServeFile(response, request, fullPath)
121 }
122 } else {
123 http.NotFound(response, request)
124 }
125 }
126
127 func httpError(rw http.ResponseWriter, message string, code int) {
128 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
129 rw.WriteHeader(code)
130 rw.Header().Set("Content-Type", "text/plain")
131 fmt.Fprint(rw, message)
132 }
133
134 func okResponse(rw http.ResponseWriter, data interface{}) {
135 rw.Header().Set("Content-Type", "application/json")
136 jsonData, err := json.Marshal(data)
137 if err != nil {
138 httpError(rw, "Internal error: " + err.Error(), 500)
139 } else {
140 rw.Write(jsonData)
141 }
142 }
143
144 func RunBackEnd(c *config.Configuration) {
145 gConfig = c
146
147 mux := http.NewServeMux()
148 mux.HandleFunc("/", indexHandler)
149 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
150 mux.HandleFunc("/service/list", listService)
151 mux.HandleFunc("/service/move", moveService)
152 mux.HandleFunc("/service/remove", removeService)
153 mux.HandleFunc("/service/mkdir", mkdirService)
154 mux.HandleFunc("/service/tv_rename", tvRenameService)
155 mux.HandleFunc("/download", downloadHandler)
156
157 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
158 fmt.Printf("error %v", error)
159 }