]>
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.
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 source
:= request
.FormValue("source")
61 target
:= request
.FormValue("target")
62 err
:= paths
.Move(source
, target
)
64 errorResponse(connection
, err
.String())
66 response
:= map[string] string {
69 okResponse(connection
, response
)
72 errorResponse(connection
, "Unhandled action")
76 func errorResponse(connection
*http
.Conn
, message
string) {
77 message
= strings
.Replace(message
, paths
.JailRoot
, "/", -1)
78 response
:= map[string] string {
82 json_data
, err
:= json
.Marshal(response
)
84 connection
.SetHeader("Content-Type", "text/json")
86 io
.WriteString(connection
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
88 connection
.Write(json_data
)
92 func okResponse(connection
*http
.Conn
, data
interface{}) {
93 connection
.SetHeader("Content-Type", "text/json")
94 json_data
, err
:= json
.Marshal(data
)
96 errorResponse(connection
, "Internal encoding error")
98 connection
.Write(json_data
)
102 func RunFrontEnd(port
int) {
103 mux
:= http
.NewServeMux()
104 mux
.HandleFunc("/", indexHandler
)
105 mux
.Handle("/fe/", http
.FileServer(kFrontEndFiles
, "/fe/"))
106 mux
.HandleFunc("/service", serviceHandler
)
108 error
:= http
.ListenAndServe(fmt
.Sprintf(":%d", port
), mux
)
109 fmt
.Printf("error %v", error
)