]>
src.bluestatic.org Git - armadillo.git/blob - src/server.go
13 const kFrontEndFiles
= "/Users/rsesek/Projects/armadillo/out/fe/"
15 func indexHandler(connection
*http
.Conn
, request
*http
.Request
) {
16 fd
, err
:= os
.Open(path
.Join(kFrontEndFiles
, "index.html"), os
.O_RDONLY
, 0)
18 fmt
.Print("Error opening file ", err
.String(), "\n")
21 io
.Copy(connection
, fd
)
24 func serviceHandler(connection
*http
.Conn
, request
*http
.Request
) {
25 if request
.Method
!= "POST" {
26 io
.WriteString(connection
, "Error: Not a POST request")
30 switch request
.FormValue("action") {
32 files
, err
:= paths
.List(request
.FormValue("path"))
34 errorResponse(connection
, err
.String())
36 okResponse(connection
, files
)
41 errorResponse(connection
, "Unhandled action")
44 func errorResponse(connection
*http
.Conn
, message
string) {
45 response
:= map[string] string {
49 json_data
, err
:= json
.Marshal(response
)
51 connection
.SetHeader("Content-Type", "text/json")
53 io
.WriteString(connection
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
55 connection
.Write(json_data
)
59 func okResponse(connection
*http
.Conn
, data
interface{}) {
60 connection
.SetHeader("Content-Type", "text/json")
61 json_data
, err
:= json
.Marshal(data
)
63 errorResponse(connection
, "Internal encoding error")
65 connection
.Write(json_data
)
70 mux
:= http
.NewServeMux()
71 mux
.HandleFunc("/", indexHandler
)
72 mux
.Handle("/fe/", http
.FileServer(kFrontEndFiles
, "/fe/"))
73 mux
.HandleFunc("/service", serviceHandler
)
75 error
:= http
.ListenAndServe(":8084", mux
)
76 fmt
.Printf("error %v", error
)