]>
src.bluestatic.org Git - armadillo.git/blob - src/server.go
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.
24 var dir
, file
= path
.Split(path
.Clean(os
.Getenv("_")))
25 var kFrontEndFiles
string = path
.Join(dir
, "fe")
26 var gConfig
*config
.Configuration
= nil
28 func indexHandler(connection
*http
.Conn
, request
*http
.Request
) {
29 fd
, err
:= os
.Open(path
.Join(kFrontEndFiles
, "index.html"), os
.O_RDONLY
, 0)
31 fmt
.Print("Error opening file ", err
.String(), "\n")
34 io
.Copy(connection
, fd
)
37 func serviceHandler(connection
*http
.Conn
, request
*http
.Request
) {
38 if request
.Method
!= "POST" {
39 io
.WriteString(connection
, "Error: Not a POST request")
43 switch request
.FormValue("action") {
45 files
, err
:= paths
.List(request
.FormValue("path"))
47 errorResponse(connection
, err
.String())
49 okResponse(connection
, files
)
52 err
:= paths
.Remove(request
.FormValue("path"))
54 errorResponse(connection
, err
.String())
56 response
:= map[string] int {
59 okResponse(connection
, response
)
62 source
:= request
.FormValue("source")
63 target
:= request
.FormValue("target")
64 err
:= paths
.Move(source
, target
)
66 errorResponse(connection
, err
.String())
68 response
:= map[string] string {
71 okResponse(connection
, response
)
74 errorResponse(connection
, "Unhandled action")
78 func proxyHandler(connection
*http
.Conn
, request
*http
.Request
) {
79 url
:= request
.FormValue("url")
84 for i
:= range gConfig
.ProxyURLs
{
85 allowedURL
:= gConfig
.ProxyURLs
[i
]
86 io
.WriteString(connection
, allowedURL
)
90 func errorResponse(connection
*http
.Conn
, message
string) {
91 message
= strings
.Replace(message
, paths
.JailRoot
, "/", -1)
92 response
:= map[string] string {
96 json_data
, err
:= json
.Marshal(response
)
98 connection
.SetHeader("Content-Type", "text/json")
100 io
.WriteString(connection
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
102 connection
.Write(json_data
)
106 func okResponse(connection
*http
.Conn
, data
interface{}) {
107 connection
.SetHeader("Content-Type", "text/json")
108 json_data
, err
:= json
.Marshal(data
)
110 errorResponse(connection
, "Internal encoding error")
112 connection
.Write(json_data
)
116 func RunFrontEnd(config
*config
.Configuration
) {
117 mux
:= http
.NewServeMux()
118 mux
.HandleFunc("/", indexHandler
)
119 mux
.Handle("/fe/", http
.FileServer(kFrontEndFiles
, "/fe/"))
120 mux
.HandleFunc("/service", serviceHandler
)
121 mux
.HandleFunc("/proxy", proxyHandler
)
125 error
:= http
.ListenAndServe(fmt
.Sprintf(":%d", config
.Port
), mux
)
126 fmt
.Printf("error %v", error
)