]>
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")
27 func indexHandler(connection
*http
.Conn
, request
*http
.Request
) {
28 fd
, err
:= os
.Open(path
.Join(kFrontEndFiles
, "index.html"), os
.O_RDONLY
, 0)
30 fmt
.Print("Error opening file ", err
.String(), "\n")
33 io
.Copy(connection
, fd
)
36 func serviceHandler(connection
*http
.Conn
, request
*http
.Request
) {
37 if request
.Method
!= "POST" {
38 io
.WriteString(connection
, "Error: Not a POST request")
42 switch request
.FormValue("action") {
44 files
, err
:= paths
.List(request
.FormValue("path"))
46 errorResponse(connection
, err
.String())
48 okResponse(connection
, files
)
51 err
:= paths
.Remove(request
.FormValue("path"))
53 errorResponse(connection
, err
.String())
55 response
:= map[string] int {
58 okResponse(connection
, response
)
61 source
:= request
.FormValue("source")
62 target
:= request
.FormValue("target")
63 err
:= paths
.Move(source
, target
)
65 errorResponse(connection
, err
.String())
67 response
:= map[string] string {
70 okResponse(connection
, response
)
73 errorResponse(connection
, "Unhandled action")
77 func errorResponse(connection
*http
.Conn
, message
string) {
78 message
= strings
.Replace(message
, paths
.JailRoot
, "/", -1)
79 response
:= map[string] string {
83 json_data
, err
:= json
.Marshal(response
)
85 connection
.SetHeader("Content-Type", "text/json")
87 io
.WriteString(connection
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
89 connection
.Write(json_data
)
93 func okResponse(connection
*http
.Conn
, data
interface{}) {
94 connection
.SetHeader("Content-Type", "text/json")
95 json_data
, err
:= json
.Marshal(data
)
97 errorResponse(connection
, "Internal encoding error")
99 connection
.Write(json_data
)
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
)
109 error
:= http
.ListenAndServe(fmt
.Sprintf(":%d", config
.Port
), mux
)
110 fmt
.Printf("error %v", error
)