The proper Content-Type for JSON is application/json.
[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 errorResponse(response, err.Error())
61 } else {
62 okResponse(response, files)
63 }
64 case "remove":
65 err := RemovePath(request.FormValue("path"))
66 if err != nil {
67 errorResponse(response, err.Error())
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 errorResponse(response, err.Error())
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 errorResponse(response, err.Error())
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 errorResponse(response, err.Error())
103 } else {
104 data := map[string]interface{}{
105 "path": *newPath,
106 "error": 0,
107 }
108 okResponse(response, data)
109 }
110 default:
111 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
112 errorResponse(response, "Unhandled action")
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 errorResponse(response http.ResponseWriter, message string) {
131 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
132 data := map[string]interface{}{
133 "error": -1,
134 "message": message,
135 }
136 json_data, err := json.Marshal(data)
137
138 response.Header().Set("Content-Type", "application/json")
139 if err != nil {
140 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
141 } else {
142 response.Write(json_data)
143 }
144 }
145
146 func okResponse(response http.ResponseWriter, data interface{}) {
147 response.Header().Set("Content-Type", "application/json")
148 json_data, err := json.Marshal(data)
149 if err != nil {
150 errorResponse(response, "Internal encoding error")
151 } else {
152 response.Write(json_data)
153 }
154 }
155
156 func RunBackEnd(c *config.Configuration) {
157 gConfig = c
158
159 mux := http.NewServeMux()
160 mux.HandleFunc("/", indexHandler)
161 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
162 mux.HandleFunc("/service", serviceHandler)
163 mux.HandleFunc("/download", downloadHandler)
164
165 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
166 fmt.Printf("error %v", error)
167 }