2 // Armadillo File Manager
3 // Copyright (c) 2010, 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.
23 var dir, file = path.Split(path.Clean(os.Getenv("_")))
24 var kFrontEndFiles string = path.Join(dir, "fe")
26 func indexHandler(connection *http.Conn, request *http.Request) {
27 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"), os.O_RDONLY, 0)
29 fmt.Print("Error opening file ", err.String(), "\n")
32 io.Copy(connection, fd)
35 func serviceHandler(connection *http.Conn, request *http.Request) {
36 if request.Method != "POST" {
37 io.WriteString(connection, "Error: Not a POST request")
41 switch request.FormValue("action") {
43 files, err := paths.List(request.FormValue("path"))
45 errorResponse(connection, err.String())
47 okResponse(connection, files)
50 err := paths.Remove(request.FormValue("path"))
52 errorResponse(connection, err.String())
54 response := map[string] int {
57 okResponse(connection, response)
60 errorResponse(connection, "Unhandled action")
64 func errorResponse(connection *http.Conn, message string) {
65 message = strings.Replace(message, paths.JailRoot, "/", -1)
66 response := map[string] string {
70 json_data, err := json.Marshal(response)
72 connection.SetHeader("Content-Type", "text/json")
74 io.WriteString(connection, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
76 connection.Write(json_data)
80 func okResponse(connection *http.Conn, data interface{}) {
81 connection.SetHeader("Content-Type", "text/json")
82 json_data, err := json.Marshal(data)
84 errorResponse(connection, "Internal encoding error")
86 connection.Write(json_data)
90 func RunFrontEnd(port int) {
91 mux := http.NewServeMux()
92 mux.HandleFunc("/", indexHandler)
93 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
94 mux.HandleFunc("/service", serviceHandler)
96 error := http.ListenAndServe(fmt.Sprintf(":%d", port), mux)
97 fmt.Printf("error %v", error)