]>
src.bluestatic.org Git - armadillo.git/blob - src/server.go
12 const kFrontEndFiles
= "/Users/rsesek/Projects/armadillo/out/fe/"
14 func indexHandler(connection
*http
.Conn
, request
*http
.Request
) {
15 fd
, err
:= os
.Open(path
.Join(kFrontEndFiles
, "index.html"), os
.O_RDONLY
, 0)
17 fmt
.Print("Error opening file ", err
.String(), "\n")
20 io
.Copy(connection
, fd
)
23 func serviceHandler(connection
*http
.Conn
, request
*http
.Request
) {
24 if request
.Method
!= "POST" {
25 io
.WriteString(connection
, "Error: Not a POST request")
29 switch request
.FormValue("action") {
31 okResponse(connection
, "Request received")
35 errorResponse(connection
, "Unhandled action")
38 func errorResponse(connection
*http
.Conn
, message
string) {
39 response
:= map[string] string {
43 json_data
, err
:= json
.Marshal(response
)
45 connection
.SetHeader("Content-Type", "text/json")
47 io
.WriteString(connection
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
49 connection
.Write(json_data
)
53 func okResponse(connection
*http
.Conn
, data
interface{}) {
54 connection
.SetHeader("Content-Type", "text/json")
55 json_data
, err
:= json
.Marshal(data
)
57 errorResponse(connection
, "Internal encoding error")
59 connection
.Write(json_data
)
64 mux
:= http
.NewServeMux()
65 mux
.HandleFunc("/", indexHandler
)
66 mux
.Handle("/fe/", http
.FileServer(kFrontEndFiles
, "/fe/"))
67 mux
.HandleFunc("/service", serviceHandler
)
69 error
:= http
.ListenAndServe(":8084", mux
)
70 fmt
.Printf("error %v", error
)