From 66012257388eb5d28a3217afe867ad0c636c5bdb Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 16 Apr 2011 16:18:31 -0700 Subject: [PATCH] camlistored Javascript discovery --- server/go/camlistored/ui.go | 37 ++++++++++++++++++++++++++-------- server/go/camlistored/ui/ui.js | 28 ++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/server/go/camlistored/ui.go b/server/go/camlistored/ui.go index ffb81dd80..a7ec095e3 100644 --- a/server/go/camlistored/ui.go +++ b/server/go/camlistored/ui.go @@ -19,6 +19,7 @@ package main import ( "fmt" "http" + "json" "os" "path/filepath" "regexp" @@ -60,12 +61,32 @@ func createUIHandler(conf jsonconfig.Obj) (h http.Handler, err os.Error) { return ui, nil } -func (ui *UIHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { - file := staticFilePattern.FindString(req.URL.Path) - if file != "" { - file = file[1:] - } else { - file = "index.html" - } - http.ServeFile(rw, req, filepath.Join(ui.FilesDir, file)) +func wantsDiscovery(req *http.Request) bool { + return req.Method == "GET" && + (req.Header.Get("Accept") == "text/x-camli-configuration" || + req.FormValue("camli.mode") == "config") +} + +func (ui *UIHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + rw.Header().Set("Vary", "Accept") + switch { + case wantsDiscovery(req): + ui.serveDiscovery(rw) + default: + file := staticFilePattern.FindString(req.URL.Path) + if file != "" { + file = file[1:] + } else { + file = "index.html" + } + http.ServeFile(rw, req, filepath.Join(ui.FilesDir, file)) + } +} + +func (ui *UIHandler) serveDiscovery(rw http.ResponseWriter) { + rw.Header().Set("Content-Type", "text/javascript") + json.NewEncoder(rw).Encode(map[string]interface{}{ + "blobRoot": ui.BlobRoot, + "searchRoot": ui.SearchRoot, + }) } diff --git a/server/go/camlistored/ui/ui.js b/server/go/camlistored/ui/ui.js index 248db515f..c9c5dedf6 100644 --- a/server/go/camlistored/ui/ui.js +++ b/server/go/camlistored/ui/ui.js @@ -1,3 +1,29 @@ +/* +Copyright 2011 Google Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + function discover() { - alert("TODO: discovery"); + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState != 4) { return; } + if (xhr.status != 200) { + console.log("no status 200; got " + xhr.status); + } + var disco = JSON.parse(xhr.responseText); + alert(JSON.stringify(disco)); + }; + xhr.open("GET", "./?camli.mode=config", true); + xhr.send(); }