From d5e0370b59b8b64b25f4d65c895ed98f73502061 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 30 Aug 2010 13:23:42 -0400 Subject: [PATCH] Add some response and request helpers for the backend and frontend. --- src/server.go | 48 +++++++++++++++++++++++++++++++++++------ web_frontend/index.html | 5 +++++ web_frontend/main.js | 24 ++++++++++++++++++++- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/server.go b/src/server.go index e5a91b9..294c0fa 100644 --- a/src/server.go +++ b/src/server.go @@ -4,17 +4,13 @@ import ( "fmt" "http" "io" + "json" "os" "path" ) const kFrontEndFiles = "/Users/rsesek/Projects/armadillo/out/fe/" -func testHandler(connection *http.Conn, request *http.Request) { - fmt.Print("Got a request"); - io.WriteString(connection, "Hello world") -} - func indexHandler(connection *http.Conn, request *http.Request) { fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"), os.O_RDONLY, 0) if err != nil { @@ -24,11 +20,51 @@ func indexHandler(connection *http.Conn, request *http.Request) { io.Copy(connection, fd) } +func serviceHandler(connection *http.Conn, request *http.Request) { + if request.Method != "POST" { + io.WriteString(connection, "Error: Not a POST request") + return + } + + switch request.FormValue("action") { + case "list": + okResponse(connection, "Request received") + return + } + + errorResponse(connection, "Unhandled action") +} + +func errorResponse(connection *http.Conn, message string) { + response := map[string] string { + "error": "-1", + "message": message, + } + json_data, err := json.Marshal(response) + + connection.SetHeader("Content-Type", "text/json") + if err != nil { + io.WriteString(connection, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}") + } else { + connection.Write(json_data) + } +} + +func okResponse(connection *http.Conn, data interface{}) { + connection.SetHeader("Content-Type", "text/json") + json_data, err := json.Marshal(data) + if err != nil { + errorResponse(connection, "Internal encoding error") + } else { + connection.Write(json_data) + } +} + func RunFrontEnd() { mux := http.NewServeMux() mux.HandleFunc("/", indexHandler) mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/")) - mux.HandleFunc("/test", testHandler) + mux.HandleFunc("/service", serviceHandler) error := http.ListenAndServe(":8084", mux) fmt.Printf("error %v", error) diff --git a/web_frontend/index.html b/web_frontend/index.html index 93cacdc..71f866d 100644 --- a/web_frontend/index.html +++ b/web_frontend/index.html @@ -4,4 +4,9 @@ Armadillo File Manager + + + \ No newline at end of file diff --git a/web_frontend/main.js b/web_frontend/main.js index c869b93..4fe11ca 100644 --- a/web_frontend/main.js +++ b/web_frontend/main.js @@ -1,2 +1,24 @@ +goog.provide('armadillo'); -alert('foo'); +goog.require('goog.net.XhrIo'); +goog.require('goog.Uri.QueryData'); + +armadillo.Main = function() { + var callback = function(response) { + console.log('response = ' + response); + } + armadillo.Request('list', {}, callback); +}; + +/** + * Starts a new XHR service request from the backend. + * @param {string} action Action to perform. + * @param {Object} extra_data Extra data to add. + * @param {Function} callback XHR callback. + */ +armadillo.Request = function(action, extra_data, callback) { + var data = new goog.Uri.QueryData(); + data.set('action', 'list'); + data.extend(extra_data); + goog.net.XhrIo.send('/service', callback, 'POST', data); +}; -- 2.22.5