/* Copyright 2015 The Camlistore Authors. 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. */ package server import ( "encoding/json" "fmt" "html/template" "net/http" "sync" "camlistore.org/pkg/blobserver" "camlistore.org/pkg/httputil" "camlistore.org/pkg/jsonconfig" "camlistore.org/pkg/types/clientconfig" ) const helpHTML string = ` Help

Help

Web User Interface

Search bar predicates.

Client Configuration

You will need to use the following client configuration in order to access this server using the Camlistore command line tools.

{{ . }}

Anything Else?

See the Camlistore online documentation and community contacts.

` // HelpHandler publishes information related to accessing the server type HelpHandler struct { clientConfig *clientconfig.Config // generated from serverConfig serverConfig jsonconfig.Obj // low-level config goTemplate *template.Template // for rendering } // setServerConfigOnce guards operation within SetServerConfig var setServerConfigOnce sync.Once // SetServerConfig enables the handler to receive the server config // before InitHandler, which generates a client config from the server config, is called. func (hh *HelpHandler) SetServerConfig(config jsonconfig.Obj) { setServerConfigOnce.Do(func() { hh.serverConfig = config }) } func init() { blobserver.RegisterHandlerConstructor("help", newHelpFromConfig) } func (hh *HelpHandler) InitHandler(hl blobserver.FindHandlerByTyper) error { if hh.serverConfig == nil { return fmt.Errorf("HelpHandler's serverConfig must be set before calling its InitHandler") } clientConfig, err := clientconfig.GenerateClientConfig(hh.serverConfig) if err != nil { return fmt.Errorf("error generating client config: %v", err) } hh.clientConfig = clientConfig tmpl, err := template.New("help").Parse(helpHTML) if err != nil { return fmt.Errorf("error creating template: %v", err) } hh.goTemplate = tmpl return nil } func newHelpFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (h http.Handler, err error) { return &HelpHandler{}, nil } func (hh *HelpHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { suffix := httputil.PathSuffix(req) if !httputil.IsGet(req) { http.Error(rw, "Illegal help method.", http.StatusMethodNotAllowed) return } switch suffix { case "": hh.serveHelpHTML(rw, req) default: http.Error(rw, "Illegal help path.", http.StatusNotFound) } } func (hh *HelpHandler) serveHelpHTML(rw http.ResponseWriter, req *http.Request) { jsonBytes, err := json.MarshalIndent(hh.clientConfig, "", " ") if err != nil { httputil.ServeError(rw, req, fmt.Errorf("could not serialize client config JSON: %v", err)) return } hh.goTemplate.Execute(rw, string(jsonBytes)) }