Пример #1
0
func main() {
	flag.Parse()

	if *index != "" {
		searcher = search.NewSearcher(*index, *prefix)
	}

	r := mux.NewRouter()

	r.PathPrefix("/src").Methods("GET").
		Handler(http.StripPrefix("/src", http.FileServer(http.Dir(*src))))

	r.PathPrefix("/api/search").Methods("POST").HandlerFunc(searchHandler)

	// Single-page app URLs are always served with the index page.
	r.PathPrefix("/file/").Methods("GET").
		Handler(&SingleFile{path.Join(*app, "index.html")})
	r.Path("/search").Methods("GET").
		Handler(&SingleFile{path.Join(*app, "index.html")})

	r.PathPrefix("/").Methods("GET").
		Handler(http.FileServer(http.Dir(*app)))

	http.ListenAndServe(":8080", r)
}
Пример #2
0
func main() {
	flag.Usage = func() {
		fmt.Fprintln(os.Stderr, usageMessage)
		flag.PrintDefaults()
	}
	flag.Parse()

	if *index == "" {
		flag.Usage()
		os.Exit(1)
	}

	if len(flag.Args()) != 1 {
		flag.Usage()
		os.Exit(1)
	}

	if *cpuProfile != "" {
		f, err := os.Create(*cpuProfile)
		if err != nil {
			log.Fatal("could not create CPU profile: ", err)
		}
		if err := pprof.StartCPUProfile(f); err != nil {
			log.Fatal("could not start CPU profile: ", err)
		}
		defer pprof.StopCPUProfile()
	}

	if *heapProfile != "" {
		f, err := os.Create(*heapProfile)
		if err != nil {
			log.Fatal("could not create heap profile: ", err)
		}
		defer pprof.Lookup("heap").WriteTo(f, 0)
	}

	s := search.NewSearcher(*index, "")

	results, err := s.Search(search.Options{Regexp: flag.Arg(0)})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	for _, r := range results {
		for _, m := range r.Matches {
			snippet := m.SnippetBefore + m.SnippetMatch + m.SnippetAfter
			fmt.Printf("%s:%d: %s\n", r.Path, m.Start, snippet)
		}
	}
}