camlistored Javascript discovery

This commit is contained in:
Brad Fitzpatrick 2011-04-16 16:18:31 -07:00
parent aadf3dbf82
commit 6601225738
2 changed files with 56 additions and 9 deletions

View File

@ -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,
})
}

View File

@ -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();
}