func (c *content) render(w io.Writer, root string, cfg *config.Config, prod bool) error { t, err := template.ParseFiles(filepath.Join(root, c.template)) if err != nil { return err } json, err := cfg.ToJsonString() if err != nil { return err } var src template.HTML if prod { s, err := sourceForPrd(root, c.sources) if err != nil { return err } src = s } else { src = sourceForDev(c.sources) } return t.Execute(w, map[string]interface{}{ "ReactVersion": ReactVersion, "jQueryVersion": jQueryVersion, "ReposAsJson": json, "Source": src, }) }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) info_log = log.New(os.Stdout, "", log.LstdFlags) error_log = log.New(os.Stderr, "", log.LstdFlags) flagConf := flag.String("conf", "config.json", "") flagAddr := flag.String("addr", ":6080", "") flagRoot := flag.String("root", "", "") flagProd := flag.Bool("prod", false, "") flagStale := flag.Bool("use-existing-stale-index", false, "DEV: Do not talk to git via pull or clone (requires an existing index)") flag.Parse() // In prod mode, we will need jsx. if *flagProd { if err := checkForJsx(); err != nil { panic("You need to install jsx. (npm install -g react-tools)") } } if err := findRoot(flagRoot); err != nil { panic(err) } var cfg config.Config if err := cfg.LoadFromFile(*flagConf); err != nil { panic(err) } idx, err := makeSearchers(&cfg, *flagStale) if err != nil { info_log.Println("Some repos failed to index, see output above") } else { info_log.Println("All indexes built!") } formattedAddress := *flagAddr if 0 == strings.Index(*flagAddr, ":") { formattedAddress = "localhost" + *flagAddr } info_log.Printf("running server at http://%s...\n", formattedAddress) if err := runHttp(*flagAddr, *flagRoot, *flagProd, &cfg, idx); err != nil { panic(err) } }
func makeSearchers( cfg *config.Config, useStaleIndex bool) (map[string]*searcher.Searcher, error) { // Ensure we have a dbpath if _, err := os.Stat(cfg.DbPath); err != nil { if err := os.MkdirAll(cfg.DbPath, os.ModePerm); err != nil { return nil, err } } validRepos := map[string]*config.Repo{} // Now build and initialize a searcher for each repo. // TODO(knorton): These could be done in parallel. m := map[string]*searcher.Searcher{} var err error for name, repo := range cfg.Repos { for _, branch := range repo.Branches { path := filepath.Join(cfg.DbPath, name) var s *searcher.Searcher if useStaleIndex { s, err = searcher.NewFromExisting(path, repo, branch) } else { s, err = searcher.New(path, repo, branch) } if err == nil { m[strings.ToLower(name)+"@"+strings.ToLower(branch)] = s validRepos[name] = repo } } } if err != nil { err = errors.New("One or more repos failed to index") } // Update the config to only include repos we have successfully indexed cfg.Repos = validRepos return m, err }