Finish replacing errorResponse with httpError
[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(response 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 response.Header().Set("Content-Type", "text/html")
47 io.Copy(response, fd)
48 }
49
50 func serviceHandler(response http.ResponseWriter, request *http.Request) {
51 if request.Method != "POST" {
52 io.WriteString(response, "Error: Not a POST request")
53 return
54 }
55
56 switch request.FormValue("action") {
57 case "list":
58 files, err := ListPath(request.FormValue("path"))
59 if err != nil {
60 httpError(response, err.Error(), http.StatusNotFound)
61 } else {
62 okResponse(response, files)
63 }
64 case "remove":
65 err := RemovePath(request.FormValue("path"))
66 if err != nil {
67 httpError(response, err.Error(), http.StatusNotFound)
68 } else {
69 data := map[string]int{
70 "error": 0,
71 }
72 okResponse(response, data)
73 }
74 case "move":
75 source := request.FormValue("source")
76 target := request.FormValue("target")
77 err := MovePath(source, target)
78 if err != nil {
79 httpError(response, err.Error(), http.StatusNotFound)
80 } else {
81 data := map[string]interface{}{
82 "path": target,
83 "error": 0,
84 }
85 okResponse(response, data)
86 }
87 case "mkdir":
88 path := request.FormValue("path")
89 err := MakeDir(path)
90 if err != nil {
91 httpError(response, err.Error(), http.StatusUnauthorized)
92 } else {
93 data := map[string]interface{}{
94 "path": path,
95 "error": 0,
96 }
97 okResponse(response, data)
98 }
99 case "tv_rename":
100 newPath, err := RenameTVEpisode(request.FormValue("path"))
101 if err != nil {
102 httpError(response, err.Error(), http.StatusBadRequest)
103 } else {
104 data := map[string]interface{}{
105 "path": *newPath,
106 "error": 0,
107 }
108 okResponse(response, data)
109 }
110 default:
111 httpError(response, fmt.Sprintf("Invalid action: '%s'", request.FormValue("action")),
112 http.StatusBadRequest)
113 }
114 }
115
116 func downloadHandler(response http.ResponseWriter, request *http.Request) {
117 valid, fullPath := IsValidPath(request.FormValue("path"))
118 if valid {
119 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
120 if info.IsDir() {
121 http.Error(response, "Path is a directory", http.StatusBadRequest)
122 } else {
123 http.ServeFile(response, request, fullPath)
124 }
125 } else {
126 http.NotFound(response, request)
127 }
128 }
129
130 func httpError(rw http.ResponseWriter, message string, code int) {
131 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
132 rw.WriteHeader(code)
133 rw.Header().Set("Content-Type", "text/plain")
134 fmt.Fprint(rw, message)
135 }
136
137 func okResponse(rw http.ResponseWriter, data interface{}) {
138 rw.Header().Set("Content-Type", "application/json")
139 jsonData, err := json.Marshal(data)
140 if err != nil {
141 httpError(rw, "Internal error: " + err.Error(), 500)
142 } else {
143 rw.Write(jsonData)
144 }
145 }
146
147 func RunBackEnd(c *config.Configuration) {
148 gConfig = c
149
150 mux := http.NewServeMux()
151 mux.HandleFunc("/", indexHandler)
152 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
153 mux.HandleFunc("/service", serviceHandler)
154 mux.HandleFunc("/download", downloadHandler)
155
156 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
157 fmt.Printf("error %v", error)
158 }