]>
src.bluestatic.org Git - armadillo.git/blob - server/server.go
2 // Armadillo File Manager
3 // Copyright (c) 2010-2012, Robert Sesek <http://www.bluestatic.org>
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.
15 "github.com/rsesek/armadillo/config"
26 gConfig
*config
.Configuration
30 _
, thisFile
, _
, ok
:= runtime
.Caller(0)
32 panic("unable to get file information from runtime.Caller so the frontend files cannot be found")
34 // thisFile = /armadillo/server/server.go, so compute /armadillo/frontend/
35 kFrontEndFiles
= path
.Join(path
.Dir(path
.Dir(thisFile
)), "frontend")
38 func indexHandler(response http
.ResponseWriter
, request
*http
.Request
) {
39 fd
, err
:= os
.Open(path
.Join(kFrontEndFiles
, "index.html"))
41 fmt
.Print("Error opening file ", err
.Error(), "\n")
46 response
.Header().Set("Content-Type", "text/html")
50 func serviceHandler(response http
.ResponseWriter
, request
*http
.Request
) {
51 if request
.Method
!= "POST" {
52 io
.WriteString(response
, "Error: Not a POST request")
56 switch request
.FormValue("action") {
58 files
, err
:= ListPath(request
.FormValue("path"))
60 errorResponse(response
, err
.Error())
62 okResponse(response
, files
)
65 err
:= RemovePath(request
.FormValue("path"))
67 errorResponse(response
, err
.Error())
69 data
:= map[string]int{
72 okResponse(response
, data
)
75 source
:= request
.FormValue("source")
76 target
:= request
.FormValue("target")
77 err
:= MovePath(source
, target
)
79 errorResponse(response
, err
.Error())
81 data
:= map[string]interface{}{
85 okResponse(response
, data
)
88 path
:= request
.FormValue("path")
91 errorResponse(response
, err
.Error())
93 data
:= map[string]interface{}{
97 okResponse(response
, data
)
100 newPath
, err
:= RenameTVEpisode(request
.FormValue("path"))
102 errorResponse(response
, err
.Error())
104 data
:= map[string]interface{}{
108 okResponse(response
, data
)
111 fmt
.Printf("Invalid action: '%s'\n", request
.FormValue("action"))
112 errorResponse(response
, "Unhandled action")
116 func downloadHandler(response http
.ResponseWriter
, request
*http
.Request
) {
117 valid
, fullPath
:= IsValidPath(request
.FormValue("path"))
119 info
, _
:= os
.Lstat(fullPath
) // Error is already checked by |valid|.
121 http
.Error(response
, "Path is a directory", http
.StatusBadRequest
)
123 http
.ServeFile(response
, request
, fullPath
)
126 http
.NotFound(response
, request
)
130 func errorResponse(rw http
.ResponseWriter
, msg
string) {
131 // TODO: Replace errorResponse with httpError.
132 httpError(rw
, msg
, 400)
135 func httpError(rw http
.ResponseWriter
, message
string, code
int) {
136 message
= strings
.Replace(message
, gConfig
.JailRoot
, "/", -1)
139 rw
.Header().Set("Content-Type", "text/plain")
140 fmt
.Fprint(rw
, message
)
143 func okResponse(response http
.ResponseWriter
, data
interface{}) {
144 response
.Header().Set("Content-Type", "application/json")
145 json_data
, err
:= json
.Marshal(data
)
147 errorResponse(response
, "Internal encoding error")
149 response
.Write(json_data
)
153 func RunBackEnd(c
*config
.Configuration
) {
156 mux
:= http
.NewServeMux()
157 mux
.HandleFunc("/", indexHandler
)
158 mux
.Handle("/fe/", http
.StripPrefix("/fe/", http
.FileServer(http
.Dir(kFrontEndFiles
))))
159 mux
.HandleFunc("/service", serviceHandler
)
160 mux
.HandleFunc("/download", downloadHandler
)
162 error
:= http
.ListenAndServe(fmt
.Sprintf(":%d", gConfig
.Port
), mux
)
163 fmt
.Printf("error %v", error
)