]>
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 errorResponse(connection
, "Unhandled action")
54 func errorResponse(connection
*http
.Conn
, message
string) {
55 message
= strings
.Replace(message
, paths
.JailRoot
, "/", -1)
56 response
:= map[string] string {
60 json_data
, err
:= json
.Marshal(response
)
62 connection
.SetHeader("Content-Type", "text/json")
64 io
.WriteString(connection
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
66 connection
.Write(json_data
)
70 func okResponse(connection
*http
.Conn
, data
interface{}) {
71 connection
.SetHeader("Content-Type", "text/json")
72 json_data
, err
:= json
.Marshal(data
)
74 errorResponse(connection
, "Internal encoding error")
76 connection
.Write(json_data
)
80 func RunFrontEnd(port
int) {
81 mux
:= http
.NewServeMux()
82 mux
.HandleFunc("/", indexHandler
)
83 mux
.Handle("/fe/", http
.FileServer(kFrontEndFiles
, "/fe/"))
84 mux
.HandleFunc("/service", serviceHandler
)
86 error
:= http
.ListenAndServe(fmt
.Sprintf(":%d", port
), mux
)
87 fmt
.Printf("error %v", error
)