Create a Configuration struct and use that to RunFrontEnd().
[armadillo.git] / src / server.go
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010, 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 "fmt"
14 "http"
15 "io"
16 "json"
17 "os"
18 "path"
19 "strings"
20 "./config"
21 "./paths"
22 )
23
24 var dir, file = path.Split(path.Clean(os.Getenv("_")))
25 var kFrontEndFiles string = path.Join(dir, "fe")
26
27 func indexHandler(connection *http.Conn, request *http.Request) {
28 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"), os.O_RDONLY, 0)
29 if err != nil {
30 fmt.Print("Error opening file ", err.String(), "\n")
31 return
32 }
33 io.Copy(connection, fd)
34 }
35
36 func serviceHandler(connection *http.Conn, request *http.Request) {
37 if request.Method != "POST" {
38 io.WriteString(connection, "Error: Not a POST request")
39 return
40 }
41
42 switch request.FormValue("action") {
43 case "list":
44 files, err := paths.List(request.FormValue("path"))
45 if err != nil {
46 errorResponse(connection, err.String())
47 } else {
48 okResponse(connection, files)
49 }
50 case "remove":
51 err := paths.Remove(request.FormValue("path"))
52 if err != nil {
53 errorResponse(connection, err.String())
54 } else {
55 response := map[string] int {
56 "error" : 0,
57 }
58 okResponse(connection, response)
59 }
60 case "move":
61 source := request.FormValue("source")
62 target := request.FormValue("target")
63 err := paths.Move(source, target)
64 if err != nil {
65 errorResponse(connection, err.String())
66 } else {
67 response := map[string] string {
68 "path" : target,
69 }
70 okResponse(connection, response)
71 }
72 default:
73 errorResponse(connection, "Unhandled action")
74 }
75 }
76
77 func errorResponse(connection *http.Conn, message string) {
78 message = strings.Replace(message, paths.JailRoot, "/", -1)
79 response := map[string] string {
80 "error" : "-1",
81 "message" : message,
82 }
83 json_data, err := json.Marshal(response)
84
85 connection.SetHeader("Content-Type", "text/json")
86 if err != nil {
87 io.WriteString(connection, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
88 } else {
89 connection.Write(json_data)
90 }
91 }
92
93 func okResponse(connection *http.Conn, data interface{}) {
94 connection.SetHeader("Content-Type", "text/json")
95 json_data, err := json.Marshal(data)
96 if err != nil {
97 errorResponse(connection, "Internal encoding error")
98 } else {
99 connection.Write(json_data)
100 }
101 }
102
103 func RunFrontEnd(config *config.Configuration) {
104 mux := http.NewServeMux()
105 mux.HandleFunc("/", indexHandler)
106 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
107 mux.HandleFunc("/service", serviceHandler)
108
109 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
110 fmt.Printf("error %v", error)
111 }