// Returns a handler method for running a module outside of the command // interface. func (server WebCmdServer) BareModuleHandler(command string, m modules.Module) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, req *http.Request) { logRequest(req) template_content, err := resources.Load(BARE_MODULE_FILE) if err != nil { server.PrintError(w, err) return } var bareModuleTemplate = template.New("Bare module template") bareModuleTemplate, err = bareModuleTemplate.Parse(string(template_content)) if err != nil { server.PrintError(w, err) return } body, err := m.RunEvent(req) if err != nil { server.PrintError(w, err) return } query := req.FormValue("q") p := page{ Title: m.Name(), Body: body, Path: command, Command: command, QueryString: query} bareModuleTemplate.Execute(w, &p) } }
// Serves a video player wrapper around a file (via the request URL). The video // type is set as t. If transcode is true, the video URL will point to the // transcode handler, otherwise the raw file handler. func (f *FileHandler) ServeVideoPlayer(t string, transcode bool, w http.ResponseWriter, r *http.Request) { template_content, err := resources.Load(VIDEO_TEMPLATE_FILE) if err != nil { f.FallbackHandler.ServeHTTP(w, r) } var videoTemplate = template.New("Video template") videoTemplate, err = videoTemplate.Parse(string(template_content)) if err != nil { f.FallbackHandler.ServeHTTP(w, r) } // If copyable params are set, replicate them to the destination. base_params := url.Values{} if len(r.FormValue(PARAM_SEEK)) > 0 { base_params.Set(PARAM_SEEK, r.FormValue(PARAM_SEEK)) } transcode_params := base_params transcode_params.Set(PARAM_MODE, MODE_TRANSCODE) transcode_url := "?" + transcode_params.Encode() raw_params := base_params raw_params.Set(PARAM_MODE, MODE_RAW) raw_url := "?" + raw_params.Encode() var v *videoData if transcode { v = &videoData{Url: transcode_url, DownloadUrl: raw_url, TranscodeUrl: transcode_url, Type: t} } else { v = &videoData{Url: raw_url, DownloadUrl: raw_url, Type: t} } videoTemplate.Execute(w, v) }
// Returns the base handler method for running a page. Based on the query // or posted data, may run modules within the command interface, and delegate // to them for producing the page content. func (server WebCmdServer) RootHandler() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, req *http.Request) { logRequest(req) source := req.FormValue("source") query := strings.TrimSpace(req.FormValue("q")) var message string var command string var body template.HTML var err error if source == "" || (source == "query" && (query == "" || strings.ToLower(query) == "help")) { body, err = ModuleList(server.modules) } else if source == "query" { queryPieces := strings.SplitN(query, " ", 2) if len(queryPieces) == 1 { queryPieces = append(queryPieces, "") } if module, has := server.modules[queryPieces[0]]; has { command = queryPieces[0] body, err = module.RunCommand(queryPieces[0], queryPieces[1]) } else { message = "Module not found for query. Try again?" } } else { if module, has := server.modules[source]; has { command = source body, err = module.RunEvent(req) } else { message = "Requested module not found. Try a query instead!" } } if err != nil { log.Println(err) message = err.Error() } template_content, err := resources.Load(ROOT_TEMPLATE_FILE) if err != nil { server.PrintError(w, err) return } var rootTemplate = template.New("Root template") rootTemplate, err = rootTemplate.Parse(string(template_content)) if err != nil { server.PrintError(w, err) return } p := page{ Title: "root", QueryString: query, Message: message, Body: body, Command: command} rootTemplate.Execute(w, &p) } }
// Produces a listing in HTML. func (m *StaticContentModule) List() (template.HTML, error) { template_content, err := resources.Load(STATIC_CONTENT_TEMPLATE_FILE) if err != nil { return "", err } var staticContentTemplate = template.New("Static Content template") staticContentTemplate, err = staticContentTemplate.Parse(string(template_content)) if err != nil { return "", err } var w HTMLWriter staticContentTemplate.Execute(&w, m.server.Roots()) return w.HTML(), nil }
// Composes the control interface form HTML, with an optional message printed. func (m *GSModule) ComposeForm(message string) (template.HTML, error) { template_content, err := resources.Load(GS_TEMPLATE_FILE) if err != nil { return "", err } var gsTemplate = template.New("GS template") gsTemplate, err = gsTemplate.Parse(string(template_content)) if err != nil { return "", err } p := &page{Message: message} var w HTMLWriter gsTemplate.Execute(&w, p) return w.HTML(), nil }
// Composes a nice list of all installed modules and their handlers, in HTML. func ModuleList(m map[string]modules.Module) (template.HTML, error) { template_content, err := resources.Load(MODULE_LIST_FILE) if err != nil { return template.HTML(""), err } var moduleListTemplate = template.New("Module list template") moduleListTemplate, err = moduleListTemplate.Parse(string(template_content)) if err != nil { return template.HTML(""), err } commandsByName := make(map[string][]string) for command, module := range m { if _, has := commandsByName[module.Name()]; !has { commandsByName[module.Name()] = make([]string, 0) } commandsByName[module.Name()] = append(commandsByName[module.Name()], command) } moduleList := &moduleList{make([]moduleListItem, 0)} names := make([]string, len(commandsByName)) i := 0 for k, _ := range commandsByName { names[i] = k i++ } sort.Strings(names) for _, name := range names { sort.Strings(commandsByName[name]) moduleList.Modules = append(moduleList.Modules, moduleListItem{name, commandsByName[name]}) } var w modules.HTMLWriter moduleListTemplate.Execute(&w, &moduleList) return w.HTML(), nil }