]>
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.
22 var dir
, file
= path
.Split(path
.Clean(os
.Getenv("_")))
23 var kFrontEndFiles
string = path
.Join(dir
, "fe")
25 func indexHandler(connection
*http
.Conn
, request
*http
.Request
) {
26 fd
, err
:= os
.Open(path
.Join(kFrontEndFiles
, "index.html"), os
.O_RDONLY
, 0)
28 fmt
.Print("Error opening file ", err
.String(), "\n")
31 io
.Copy(connection
, fd
)
34 func serviceHandler(connection
*http
.Conn
, request
*http
.Request
) {
35 if request
.Method
!= "POST" {
36 io
.WriteString(connection
, "Error: Not a POST request")
40 switch request
.FormValue("action") {
42 files
, err
:= paths
.List(request
.FormValue("path"))
44 errorResponse(connection
, err
.String())
46 okResponse(connection
, files
)
49 errorResponse(connection
, "Unhandled action")
53 func errorResponse(connection
*http
.Conn
, message
string) {
54 response
:= map[string] string {
58 json_data
, err
:= json
.Marshal(response
)
60 connection
.SetHeader("Content-Type", "text/json")
62 io
.WriteString(connection
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
64 connection
.Write(json_data
)
68 func okResponse(connection
*http
.Conn
, data
interface{}) {
69 connection
.SetHeader("Content-Type", "text/json")
70 json_data
, err
:= json
.Marshal(data
)
72 errorResponse(connection
, "Internal encoding error")
74 connection
.Write(json_data
)
79 mux
:= http
.NewServeMux()
80 mux
.HandleFunc("/", indexHandler
)
81 mux
.Handle("/fe/", http
.FileServer(kFrontEndFiles
, "/fe/"))
82 mux
.HandleFunc("/service", serviceHandler
)
84 error
:= http
.ListenAndServe(":8084", mux
)
85 fmt
.Printf("error %v", error
)