]>
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 httpError(response
, err
.Error(), http
.StatusNotFound
)
62 okResponse(response
, files
)
65 err
:= RemovePath(request
.FormValue("path"))
67 httpError(response
, err
.Error(), http
.StatusNotFound
)
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 httpError(response
, err
.Error(), http
.StatusNotFound
)
81 data
:= map[string]interface{}{
85 okResponse(response
, data
)
88 path
:= request
.FormValue("path")
91 httpError(response
, err
.Error(), http
.StatusUnauthorized
)
93 data
:= map[string]interface{}{
97 okResponse(response
, data
)
100 newPath
, err
:= RenameTVEpisode(request
.FormValue("path"))
102 httpError(response
, err
.Error(), http
.StatusBadRequest
)
104 data
:= map[string]interface{}{
108 okResponse(response
, data
)
111 httpError(response
, fmt
.Sprintf("Invalid action: '%s'", request
.FormValue("action")),
112 http
.StatusBadRequest
)
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 httpError(rw http
.ResponseWriter
, message
string, code
int) {
131 message
= strings
.Replace(message
, gConfig
.JailRoot
, "/", -1)
133 rw
.Header().Set("Content-Type", "text/plain")
134 fmt
.Fprint(rw
, message
)
137 func okResponse(rw http
.ResponseWriter
, data
interface{}) {
138 rw
.Header().Set("Content-Type", "application/json")
139 jsonData
, err
:= json
.Marshal(data
)
141 httpError(rw
, "Internal error: " + err
.Error(), 500)
147 func RunBackEnd(c
*config
.Configuration
) {
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
)
156 error
:= http
.ListenAndServe(fmt
.Sprintf(":%d", gConfig
.Port
), mux
)
157 fmt
.Printf("error %v", error
)